Skip to content

Commit b47dea0

Browse files
committed
fix: imports within comments are no longer discoverable
1 parent 4ee1c64 commit b47dea0

File tree

4 files changed

+92
-43
lines changed

4 files changed

+92
-43
lines changed

bin/imported-components

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env node
22

3-
require('../dist/es5/scanners/scanForImports');
3+
require('../dist/es5/scanners/cli');

__tests__/utils.spec.ts renamed to src/scanners/__tests__/parser.spec.ts

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,81 @@
11
import { dirname } from 'path';
2-
import { remapImports } from '../src/scanners/scanForImports';
2+
import { remapImports } from '../scanForImports';
3+
import { getRelative } from '../shared';
34

45
describe('scanForImports', () => {
5-
const rel = '.' + dirname(__dirname);
6+
const rel = dirname(__dirname);
67
const root = '.';
8+
const rootRel = '.' + process.cwd();
9+
const sourceFile = `${rel}/a`;
710

811
it('should map simple import', () => {
912
const imports = {};
1013
remapImports(
11-
[{ file: 'a', content: 'blabla;import("./a.js"); blabla;' }],
12-
root,
13-
root,
14-
(a, b) => a + b,
14+
[{ file: `${rel}/a`, content: 'blabla;import("./b.js"); blabla;' }],
15+
rel,
16+
rel,
17+
getRelative,
1518
imports,
1619
() => true
1720
);
18-
expect(Object.values(imports)).toEqual([`[() => import('${rel}/a.js'), '', '${rel}/a.js', false] /* from .a */`]);
21+
expect(Object.values(imports)).toEqual([`[() => import('./b.js'), '', './b.js', false] /* from ./a */`]);
22+
});
23+
24+
it('handles imports in jsdoc', () => {
25+
const imports = {};
26+
remapImports(
27+
[
28+
{
29+
file: sourceFile,
30+
content: `
31+
/**
32+
* @type {import('wrong-import')}
33+
*/
34+
import(/* comment:valuable */ "./a.js");
35+
import("./b.js");
36+
// import('another-wrong-import');// FIXME: temporary removed
37+
`,
38+
},
39+
],
40+
rel,
41+
rel,
42+
getRelative,
43+
imports,
44+
() => true
45+
);
46+
expect(Object.values(imports)).toEqual([
47+
`[() => import(/* comment:valuable */'./a.js'), '', './a.js', false] /* from ./a */`,
48+
`[() => import('./b.js'), '', './b.js', false] /* from ./a */`,
49+
]);
1950
});
2051

2152
it('should map client-side import', () => {
2253
const imports = {};
2354
remapImports(
24-
[{ file: 'a', content: 'blabla;import(/* client-side */"./a.js"); blabla;' }],
25-
root,
26-
root,
27-
(a, b) => a + b,
55+
[{ file: sourceFile, content: 'blabla;import(/* client-side */"./a.js"); blabla;' }],
56+
rel,
57+
rel,
58+
getRelative,
2859
imports,
2960
() => true
3061
);
3162
expect(Object.values(imports)).toEqual([
32-
`[() => import(/* client-side */'${rel}/a.js'), '', '${rel}/a.js', true] /* from .a */`,
63+
`[() => import(/* client-side */'./a.js'), '', './a.js', true] /* from ./a */`,
3364
]);
3465
});
3566

3667
it('should map simple import with a comment', () => {
3768
const imports = {};
3869
remapImports(
39-
[{ file: 'a', content: 'blabla;import(/* comment:42 */"./a.js"); blabla;' }],
40-
root,
41-
root,
42-
(a, b) => a + b,
70+
[{ file: sourceFile, content: 'blabla;import(/* comment:42 */"./a.js"); blabla;' }],
71+
rel,
72+
rel,
73+
getRelative,
4374
imports,
4475
() => true
4576
);
4677
expect(Object.values(imports)).toEqual([
47-
`[() => import(/* comment:42 */'${rel}/a.js'), '', '${rel}/a.js', false] /* from .a */`,
78+
`[() => import(/* comment:42 */'./a.js'), '', './a.js', false] /* from ./a */`,
4879
]);
4980
});
5081

@@ -53,19 +84,19 @@ describe('scanForImports', () => {
5384
remapImports(
5485
[
5586
{
56-
file: 'a',
87+
file: sourceFile,
5788
content: 'blabla;import(/* webpack: "123" */"./a.js"); blabla; import(/* webpack: 123 */ \'./b.js\');',
5889
},
5990
],
60-
root,
61-
root,
62-
(a, b) => a + b,
91+
rel,
92+
rel,
93+
getRelative,
6394
imports,
6495
() => true
6596
);
6697
expect(Object.values(imports)).toEqual([
67-
`[() => import(/* webpack: \"123\" */'${rel}/a.js'), '', '${rel}/a.js', false] /* from .a */`,
68-
`[() => import(/* webpack: 123 */'${rel}/b.js'), '', '${rel}/b.js', false] /* from .a */`,
98+
`[() => import(/* webpack: \"123\" */'./a.js'), '', './a.js', false] /* from ./a */`,
99+
`[() => import(/* webpack: 123 */'./b.js'), '', './b.js', false] /* from ./a */`,
69100
]);
70101
});
71102

@@ -86,8 +117,8 @@ describe('scanForImports', () => {
86117
() => true
87118
);
88119
expect(Object.values(imports)).toEqual([
89-
`[() => import(/* webpackChunkName: "chunk-a" */'${rel}/a.js'), 'chunk-a', '${rel}/a.js', false] /* from .a */`,
90-
`[() => import(/* webpack: 123 */'${rel}/b.js'), '', '${rel}/b.js', false] /* from .a */`,
120+
`[() => import(/* webpackChunkName: "chunk-a" */'${rootRel}/a.js'), 'chunk-a', '${rootRel}/a.js', false] /* from .a */`,
121+
`[() => import(/* webpack: 123 */'${rootRel}/b.js'), '', '${rootRel}/b.js', false] /* from .a */`,
91122
]);
92123
});
93124

@@ -109,8 +140,8 @@ describe('scanForImports', () => {
109140
(imported, _, options) => (imported.indexOf('a.js') > 0 ? `test-${options.chunkName}-test` : 'bundle-b')
110141
);
111142
expect(Object.values(imports)).toEqual([
112-
`[() => import(/* webpackChunkName: \"chunk-a\" */'${rel}/a.js'), 'test-chunk-a-test', '${rel}/a.js', false] /* from .a */`,
113-
`[() => import(/* webpackChunkName: \"chunk-b\" */'${rel}/b.js'), 'bundle-b', '${rel}/b.js', false] /* from .a */`,
143+
`[() => import(/* webpackChunkName: \"chunk-a\" */'${rootRel}/a.js'), 'test-chunk-a-test', '${rootRel}/a.js', false] /* from .a */`,
144+
`[() => import(/* webpackChunkName: \"chunk-b\" */'${rootRel}/b.js'), 'bundle-b', '${rootRel}/b.js', false] /* from .a */`,
114145
]);
115146
});
116147

@@ -140,8 +171,8 @@ describe('scanForImports', () => {
140171
() => true
141172
);
142173
expect(Object.values(imports)).toEqual([
143-
`[() => import(/* webpackChunkName: \"chunk-a\" */'${rel}/a.js'), 'chunk-a', '${rel}/a.js', false] /* from .a */`,
144-
`[() => import('${rel}/b.js'), '', '${rel}/b.js', false] /* from .a */`,
174+
`[() => import(/* webpackChunkName: \"chunk-a\" */'${rootRel}/a.js'), 'chunk-a', '${rootRel}/a.js', false] /* from .a */`,
175+
`[() => import('${rootRel}/b.js'), '', '${rootRel}/b.js', false] /* from .a */`,
145176
]);
146177
});
147178

@@ -162,8 +193,8 @@ describe('scanForImports', () => {
162193
() => true
163194
);
164195
expect(Object.values(imports)).toEqual([
165-
`[() => import(/* *//* webpack: \"123\" */'${rel}/a.js'), '', '${rel}/a.js', false] /* from .a */`,
166-
`[() => import(/* */'${rel}/b.js'), '', '${rel}/b.js', false] /* from .a */`,
196+
`[() => import(/* *//* webpack: \"123\" */'${rootRel}/a.js'), '', '${rootRel}/a.js', false] /* from .a */`,
197+
`[() => import(/* */'${rootRel}/b.js'), '', '${rootRel}/b.js', false] /* from .a */`,
167198
]);
168199
});
169200
});

src/scanners/cli.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* tslint:disable no-console */
2+
3+
import { scanTop } from './scanForImports';
4+
5+
if (!process.argv[3]) {
6+
console.log('usage: imported-components sourceRoot targetFile');
7+
console.log('example: imported-components src src/importedComponents.js');
8+
} else {
9+
scanTop(process.cwd(), process.argv[2], process.argv[3]);
10+
}

src/scanners/scanForImports.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ const getImportString = (pattern: string, selected: number) => (str: string): Ma
5454

5555
export const getDynamicImports = getImportString(`import[\\s]?\\((([^)])+['"]?)\\)`, 1);
5656

57+
export const cleanFileContent = (content: string) => {
58+
const mapping: string[] = [];
59+
// wrap
60+
const wrapped = content.replace(new RegExp(`import[\\s]?\\((([^)])+['"]?)\\)`, 'g'), match => {
61+
const placement = mapping.push(match) - 1;
62+
return `imported_${placement}_replacement`;
63+
});
64+
65+
const cleaned = wrapped.replace(new RegExp('//.*', 'g'), '').replace(new RegExp('\\/\\*[\\s\\S]*?\\*\\/', 'gm'), '');
66+
67+
const unwrapped = cleaned.replace(new RegExp('imported_([\\d]*)_replacement', 'g'), (_, b: string) => {
68+
return mapping[+b];
69+
});
70+
71+
return unwrapped;
72+
};
73+
5774
const mapImports = (file: string, imports: MappedImport[]) =>
5875
imports.map(dep => {
5976
const { name } = dep;
@@ -85,7 +102,7 @@ export const remapImports = (
85102
chunkName?: ImportedConfiguration['chunkName']
86103
) =>
87104
data
88-
.map(({ file, content }) => mapImports(file, getDynamicImports(content)))
105+
.map(({ file, content }) => mapImports(file, getDynamicImports(cleanFileContent(content))))
89106
.forEach(importBlock =>
90107
importBlock.forEach(({ name, comment, doNotTransform, file }) => {
91108
const rootName = doNotTransform ? name : getRelativeName(root, name);
@@ -105,7 +122,7 @@ export const remapImports = (
105122
})
106123
);
107124

108-
function scanTop(root: string, start: string, target: string) {
125+
export function scanTop(root: string, start: string, target: string) {
109126
async function scan() {
110127
console.log('scanning', start, 'for imports...');
111128

@@ -184,12 +201,3 @@ ${Object.keys(imports)
184201

185202
return scan();
186203
}
187-
188-
// --------
189-
190-
if (!process.argv[3]) {
191-
console.log('usage: imported-components sourceRoot targetFile');
192-
console.log('example: imported-components src src/importedComponents.js');
193-
} else {
194-
scanTop(process.cwd(), process.argv[2], process.argv[3]);
195-
}

0 commit comments

Comments
 (0)