in packages/ice/src/service/analyze.ts [97:142]
async function analyzeFile(filePath: string) {
analyzedSet.add(filePath);
let source = await fse.readFile(filePath, 'utf-8');
const lang = path.extname(filePath).slice(1);
let loader: Loader;
if (lang === 'ts' || lang === 'tsx') {
loader = lang;
}
try {
if (loader) {
// transform content first since es-module-lexer can't handle ts file
source = (await esbuild.transform(source, { loader })).code;
}
await init;
const imports = parse(source)[0];
await Promise.all(imports.map(async (importSpecifier) => {
return (async () => {
const importName = importSpecifier.n;
// filter source code
if (importName === 'ice') {
const importStr = source.substring(importSpecifier.ss, importSpecifier.se);
const regexpForIce = /import\s?(?:type)?\s?\{([\w*\s{},]*)\}\s+from\s+['"]ice['"]/;
const matched = importStr.match(regexpForIce);
if (matched) {
const [, specifierStr] = matched;
specifierStr.trim().split(',').forEach((importStr) => {
if (!importSet.has(importStr)) importSet.add(importStr);
});
}
} else if (analyzeRelativeImport) {
let importPath = importName;
if (!path.isAbsolute(importPath)) {
importPath = getImportPath(importPath, filePath, alias);
}
if (importPath && importPath.match(/\.(j|t)sx?$/) && fse.existsSync(importPath) && !analyzedSet.has(importPath)) {
await analyzeFile(importPath);
}
}
})();
}));
} catch (err) {
logger.briefError(`Optimize runtime failed when analyze ${filePath}`);
logger.debug(err);
throw err;
}
}