11import { dirname } from 'path' ;
2- import { remapImports } from '../src/scanners/scanForImports' ;
2+ import { remapImports } from '../scanForImports' ;
3+ import { getRelative } from '../shared' ;
34
45describe ( '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} ) ;
0 commit comments