I ran into the same issue and while Angular detected some missing imports, it didn't catch them all. So I ended up creating a small script to find them. Assuming you have your components in src/app/components you can create the following file checkImports.js:
import fs from 'fs';
import path from 'path';
// Base directory of components
const componentsDir = path.join(import.meta.dirname, 'src', 'app', 'components');
// Function to read HTML file and extract ion-* tags
const getIonTags = (htmlFile) => {
const content = fs.readFileSync(htmlFile, 'utf8');
const regex = /<(ion-[a-zA-Z0-9-]+)/g;
return [...content.matchAll(regex)].map(match => match[1]);
};
// Function to check if ion-* tags exist in the TypeScript file imports
const checkImports = (tsFile, ionTags) => {
const content = fs.readFileSync(tsFile, 'utf8');
const regex = /^(\s|\t)*imports:\s*\[([^\]]+)\]$/gm;
const match = regex.exec(content);
if (match) {
const imports = match[2].split(',').map(tag => tag.trim());
const missingTags = ionTags.filter(tag => !imports.includes(tag));
return missingTags;
} else {
return [];
}
};
const transformIonTag = (tag) => {
return tag
.split('-') // Split by hyphen
.map((word, index) =>
index === 0 ? word.charAt(0).toUpperCase() + word.slice(1) : word.charAt(0).toUpperCase() + word.slice(1)
) // Capitalize each word
.join(''); // Rejoin into a single string
};
// Function to scan all components
const scanComponents = async (baseDir) => {
const folders = await fs.promises.readdir(baseDir);
for (const folder of folders) {
const componentPath = path.join(baseDir, folder);
const htmlFile = path.join(componentPath, `${folder}.html`);
const tsFile = path.join(componentPath, `${folder}.ts`);
if (fs.existsSync(htmlFile) && fs.existsSync(tsFile)) {
const ionTags = getIonTags(htmlFile);
const result = checkImports(tsFile, ionTags.map(transformIonTag));
if(result.length) {
console.log(`Missing tags in ${tsFile}: `);
console.log(`${result}\n`);
}
}
}
};
// Start scanning
scanComponents(componentsDir);
Then just run node checkImports. It should output something like this:
Missing tags in xxx/src/app/components/account-form/account-form.ts:
IonRadioGroup,IonSelect
CUSTOM_ELEMENTS_SCHEMAorNO_ERRORS_SCHEMAin your app?