Skip to content

Commit

Permalink
fix: handle recursive imports (#125)
Browse files Browse the repository at this point in the history
Added tracking handler for imported files to avoid infinite loops with recursive imports.

Closes #114
  • Loading branch information
iam-medvedev authored Jul 1, 2024
1 parent 1dd2534 commit 1155aa3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
24 changes: 24 additions & 0 deletions __tests__/less-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('less-utils', () => {
const filePath = path.resolve(__dirname, '../example/styles/style.less');
const imports = getLessImports(filePath);

expect(imports).toHaveLength(5);
expect(imports).toEqual(
expect.arrayContaining([
expect.stringContaining('styles/style-2.less'),
Expand All @@ -22,6 +23,7 @@ describe('less-utils', () => {
const filePath = path.resolve(__dirname, '../example/styles/style.less');
const imports = getLessImports(filePath, ['../example', '../example/styles']);

expect(imports).toHaveLength(5);
expect(imports).toEqual(
expect.arrayContaining([
expect.stringContaining('styles/style-2.less'),
Expand All @@ -33,6 +35,28 @@ describe('less-utils', () => {
);
});

it('works with recursive imports', () => {
const filePath = path.resolve(__dirname, '../example/styles/recursive/a.less');
const imports = getLessImports(filePath);

expect(imports).toHaveLength(2);
expect(imports).toEqual(
expect.arrayContaining([
expect.stringContaining('styles/recursive/a.less'),
expect.stringContaining('styles/recursive/b.less'),
]),
);
});

it('do not process a file if it has already been processed', () => {
const filePath = path.resolve(__dirname, '../example/styles/recursive/a.less');
const visited = new Set<string>();
visited.add(filePath);

const imports = getLessImports(filePath, [], visited);
expect(imports).toHaveLength(0);
});

it('get imports paths fail', () => {
const filePath = path.resolve(__dirname, '../example/styles/unknown-file.less');
const imports = getLessImports(filePath);
Expand Down
5 changes: 5 additions & 0 deletions example/styles/recursive/a.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import 'b.less';

body {
color: red;
}
5 changes: 5 additions & 0 deletions example/styles/recursive/b.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import 'a.less';

body {
background-color: blue;
}
9 changes: 7 additions & 2 deletions src/less-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ const importCommentRegex = /(?:\/\*(?:[\s\S]*?)\*\/)|(\/\/(?:.*)$)/gm;
const extWhitelist = ['.css', '.less'];

/** Recursively get .less/.css imports from file */
export function getLessImports(filePath: string, paths: string[] = []): string[] {
export function getLessImports(filePath: string, paths: string[] = [], visited: Set<string> = new Set()): string[] {
try {
if (visited.has(filePath)) {
return [];
}
visited.add(filePath);

const dir = path.dirname(filePath);
const content = fs.readFileSync(filePath).toString('utf8');

Expand Down Expand Up @@ -45,7 +50,7 @@ export function getLessImports(filePath: string, paths: string[] = []): string[]
});

const recursiveImports = fileImports.reduce((result, el) => {
return [...result, ...getLessImports(el, paths)];
return [...result, ...getLessImports(el, paths, visited)];
}, fileImports);

const result = recursiveImports.filter((el) => extWhitelist.includes(path.extname(el).toLowerCase()));
Expand Down

0 comments on commit 1155aa3

Please sign in to comment.