@@ -5,60 +5,98 @@ const { extname } = require('path');
55function namedAssetImportPlugin ( { types : t } ) {
66 const visited = new WeakSet ( ) ;
77
8+ function generateNewSourcePath ( loaderMap , moduleName , sourcePath ) {
9+ const ext = extname ( sourcePath ) . substr ( 1 ) ;
10+ const extMap = loaderMap [ ext ] ;
11+ return extMap [ moduleName ]
12+ ? extMap [ moduleName ] . replace ( / \[ p a t h \] / , sourcePath )
13+ : sourcePath ;
14+ }
15+
16+ function replaceMatchingSpecifiers ( path , loaderMap , callback ) {
17+ const sourcePath = path . node . source . value ;
18+ const ext = extname ( sourcePath ) . substr ( 1 ) ;
19+
20+ if ( visited . has ( path . node ) || sourcePath . indexOf ( '!' ) !== - 1 ) {
21+ return ;
22+ }
23+
24+ if ( loaderMap [ ext ] ) {
25+ path . replaceWithMultiple (
26+ path . node . specifiers . map ( specifier => {
27+ const newSpecifier = callback ( specifier , sourcePath ) ;
28+ visited . add ( newSpecifier ) ;
29+
30+ return newSpecifier ;
31+ } )
32+ ) ;
33+ }
34+ }
35+
836 return {
937 visitor : {
10- ImportDeclaration (
38+ ExportNamedDeclaration (
1139 path ,
1240 {
1341 opts : { loaderMap } ,
1442 }
1543 ) {
16- const sourcePath = path . node . source . value ;
17- const ext = extname ( sourcePath ) . substr ( 1 ) ;
18-
19- if ( visited . has ( path . node ) || sourcePath . indexOf ( '!' ) !== - 1 ) {
44+ if ( ! path . node . source ) {
2045 return ;
2146 }
2247
23- if ( loaderMap [ ext ] ) {
24- path . replaceWithMultiple (
25- path . node . specifiers . map ( specifier => {
26- if ( t . isImportDefaultSpecifier ( specifier ) ) {
27- const newDefaultImport = t . importDeclaration (
28- [
29- t . importDefaultSpecifier (
30- t . identifier ( specifier . local . name )
31- ) ,
32- ] ,
33- t . stringLiteral ( sourcePath )
34- ) ;
35-
36- visited . add ( newDefaultImport ) ;
37- return newDefaultImport ;
38- }
39-
40- const newImport = t . importDeclaration (
41- [
42- t . importSpecifier (
43- t . identifier ( specifier . local . name ) ,
44- t . identifier ( specifier . imported . name )
45- ) ,
46- ] ,
47- t . stringLiteral (
48- loaderMap [ ext ] [ specifier . imported . name ]
49- ? loaderMap [ ext ] [ specifier . imported . name ] . replace (
50- / \[ p a t h \] / ,
51- sourcePath
52- )
53- : sourcePath
54- )
55- ) ;
48+ replaceMatchingSpecifiers ( path , loaderMap , ( specifier , sourcePath ) => {
49+ if ( t . isExportDefaultSpecifier ( specifier ) ) {
50+ return t . exportDeclaration (
51+ [ t . exportDefaultSpecifier ( t . identifier ( specifier . local . name ) ) ] ,
52+ t . stringLiteral ( sourcePath )
53+ ) ;
54+ }
5655
57- visited . add ( newImport ) ;
58- return newImport ;
59- } )
56+ return t . exportNamedDeclaration (
57+ null ,
58+ [
59+ t . exportSpecifier (
60+ t . identifier ( specifier . local . name ) ,
61+ t . identifier ( specifier . exported . name )
62+ ) ,
63+ ] ,
64+ t . stringLiteral (
65+ generateNewSourcePath ( loaderMap , specifier . local . name , sourcePath )
66+ )
6067 ) ;
68+ } ) ;
69+ } ,
70+ ImportDeclaration (
71+ path ,
72+ {
73+ opts : { loaderMap } ,
6174 }
75+ ) {
76+ replaceMatchingSpecifiers ( path , loaderMap , ( specifier , sourcePath ) => {
77+ if ( t . isImportDefaultSpecifier ( specifier ) ) {
78+ return t . importDeclaration (
79+ [ t . importDefaultSpecifier ( t . identifier ( specifier . local . name ) ) ] ,
80+ t . stringLiteral ( sourcePath )
81+ ) ;
82+ }
83+
84+ return t . importDeclaration (
85+ [
86+ t . importSpecifier (
87+ t . identifier ( specifier . local . name ) ,
88+ t . identifier ( specifier . imported . name )
89+ ) ,
90+ ] ,
91+ t . stringLiteral (
92+ generateNewSourcePath (
93+ loaderMap ,
94+ specifier . imported . name ,
95+ sourcePath
96+ )
97+ )
98+ ) ;
99+ } ) ;
62100 } ,
63101 } ,
64102 } ;
0 commit comments