Merge pull request #484 from pbakaus/codex/simplify-import-graph

Simplify import graph scanning
This commit is contained in:
Paul Bakaus
2026-08-02 20:09:15 -07:00
committed by GitHub
2 changed files with 25 additions and 20 deletions
+11 -20
View File
@@ -31,6 +31,12 @@ const SCANNABLE_EXTENSIONS = new Set([
const HTML_EXTENSIONS = new Set(['.html', '.htm']);
const IMPORT_SPECIFIER_PATTERNS = [
/import\s+(?:[\s\S]*?from\s+)?['"]([^'"]+)['"]/g,
/@import\s+(?:url\(\s*)?['"]?([^'");\s]+)['"]?\s*\)?/g,
/@(?:use|forward)\s+['"]([^'"]+)['"]/g,
];
function walkDir(dir) {
const files = [];
let entries;
@@ -75,26 +81,11 @@ function buildImportGraph(files) {
const dir = path.dirname(file);
const imports = new Set();
// ES imports: import ... from '...' and import '...'
const esRe = /import\s+(?:[\s\S]*?from\s+)?['"]([^'"]+)['"]/g;
let m;
while ((m = esRe.exec(content)) !== null) {
const resolved = resolveImport(m[1], dir, fileSet);
if (resolved) imports.add(resolved);
}
// CSS @import
const cssRe = /@import\s+(?:url\(\s*)?['"]?([^'");\s]+)['"]?\s*\)?/g;
while ((m = cssRe.exec(content)) !== null) {
const resolved = resolveImport(m[1], dir, fileSet);
if (resolved) imports.add(resolved);
}
// SCSS @use / @forward
const scssRe = /@(?:use|forward)\s+['"]([^'"]+)['"]/g;
while ((m = scssRe.exec(content)) !== null) {
const resolved = resolveImport(m[1], dir, fileSet);
if (resolved) imports.add(resolved);
for (const pattern of IMPORT_SPECIFIER_PATTERNS) {
for (const match of content.matchAll(pattern)) {
const resolved = resolveImport(match[1], dir, fileSet);
if (resolved) imports.add(resolved);
}
}
graph.set(file, imports);
+14
View File
@@ -2797,6 +2797,20 @@ describe('buildImportGraph', () => {
expect(themeImports.has(path.join(MF, 'variables.sass'))).toBe(true);
});
test('resolves Sass @use and @forward', async () => {
await withStaticFixture({
'theme.scss': "@use './variables';\n@forward './tokens';\n",
'variables.scss': '$primary: rebeccapurple;\n',
'tokens.scss': '$spacing: 1rem;\n',
}, ({ dir }) => {
const theme = path.join(dir, 'theme.scss');
const variables = path.join(dir, 'variables.scss');
const tokens = path.join(dir, 'tokens.scss');
const graph = buildImportGraph([theme, variables, tokens]);
expect(graph.get(theme)).toEqual(new Set([variables, tokens]));
});
});
test('ignores bare/node_modules imports', () => {
const graph = buildImportGraph([
path.join(MF, 'App.tsx'),