@@ -16,24 +16,22 @@ export default ({
16
16
} : {
17
17
types : BabelTypes
18
18
} ) => {
19
- let styleModuleImportMap ;
20
- let importedHelperIndentifier ;
21
- let styleModuleImportMapIdentifier ;
19
+ const filenameMap = { } ;
22
20
23
- const setupFileForRuntimeResolution = ( path ) => {
21
+ const setupFileForRuntimeResolution = ( path , filename ) => {
24
22
const programPath = path . findParent ( ( parentPath ) => {
25
23
return parentPath . isProgram ( ) ;
26
24
} ) ;
27
25
28
- importedHelperIndentifier = programPath . scope . generateUidIdentifier ( 'getClassName' ) ;
29
- styleModuleImportMapIdentifier = programPath . scope . generateUidIdentifier ( 'styleModuleImportMap' ) ;
26
+ filenameMap [ filename ] . importedHelperIndentifier = programPath . scope . generateUidIdentifier ( 'getClassName' ) ;
27
+ filenameMap [ filename ] . styleModuleImportMapIdentifier = programPath . scope . generateUidIdentifier ( 'styleModuleImportMap' ) ;
30
28
31
29
programPath . unshiftContainer (
32
30
'body' ,
33
31
t . importDeclaration (
34
32
[
35
33
t . importDefaultSpecifier (
36
- importedHelperIndentifier
34
+ filenameMap [ filename ] . importedHelperIndentifier
37
35
)
38
36
] ,
39
37
t . stringLiteral ( 'babel-plugin-react-css-modules/dist/browser/getClassName' )
@@ -49,12 +47,14 @@ export default ({
49
47
'const' ,
50
48
[
51
49
t . variableDeclarator (
52
- styleModuleImportMapIdentifier ,
53
- createObjectExpression ( t , styleModuleImportMap )
50
+ filenameMap [ filename ] . styleModuleImportMapIdentifier ,
51
+ createObjectExpression ( t , filenameMap [ filename ] . styleModuleImportMap )
54
52
)
55
53
]
56
54
)
57
55
) ;
56
+ // eslint-disable-next-line
57
+ // console.log('setting up', filename, util.inspect(filenameMap,{depth: 5}))
58
58
} ;
59
59
60
60
return {
@@ -65,6 +65,7 @@ export default ({
65
65
return ;
66
66
}
67
67
68
+ const filename = stats . file . opts . filename ;
68
69
const targetFileDirectoryPath = dirname ( stats . file . opts . filename ) ;
69
70
const targetResourcePath = resolve ( targetFileDirectoryPath , path . node . source . value ) ;
70
71
@@ -82,11 +83,12 @@ export default ({
82
83
throw new Error ( 'Unexpected use case.' ) ;
83
84
}
84
85
85
- styleModuleImportMap [ styleImportName ] = requireCssModule ( targetResourcePath , {
86
+ filenameMap [ filename ] . styleModuleImportMap [ styleImportName ] = requireCssModule ( targetResourcePath , {
86
87
generateScopedName : stats . opts . generateScopedName
87
88
} ) ;
88
89
} ,
89
- JSXElement ( path : Object ) : void {
90
+ JSXElement ( path : Object , stats : Object ) : void {
91
+ const filename = stats . file . opts . filename ;
90
92
const styleNameAttribute = path . node . openingElement . attributes
91
93
. find ( ( attribute ) => {
92
94
return typeof attribute . name !== 'undefined' && attribute . name . name === 'styleName' ;
@@ -97,21 +99,33 @@ export default ({
97
99
}
98
100
99
101
if ( t . isStringLiteral ( styleNameAttribute . value ) ) {
100
- resolveStringLiteral ( path , styleModuleImportMap , styleNameAttribute ) ;
102
+ resolveStringLiteral (
103
+ path ,
104
+ filenameMap [ filename ] . styleModuleImportMap ,
105
+ styleNameAttribute
106
+ ) ;
101
107
102
108
return ;
103
109
}
104
110
105
111
if ( t . isJSXExpressionContainer ( styleNameAttribute . value ) ) {
106
- if ( ! importedHelperIndentifier ) {
107
- setupFileForRuntimeResolution ( path ) ;
112
+ if ( ! filenameMap [ filename ] . importedHelperIndentifier ) {
113
+ setupFileForRuntimeResolution ( path , filename ) ;
108
114
}
109
-
110
- replaceJsxExpressionContainer ( t , styleNameAttribute , importedHelperIndentifier , styleModuleImportMapIdentifier ) ;
115
+ replaceJsxExpressionContainer (
116
+ t ,
117
+ styleNameAttribute ,
118
+ filenameMap [ filename ] . importedHelperIndentifier ,
119
+ filenameMap [ filename ] . styleModuleImportMapIdentifier
120
+ ) ;
111
121
}
112
122
} ,
113
- Program ( ) {
114
- styleModuleImportMap = { } ;
123
+ Program ( path : Object , stats : Object ) : void {
124
+ const filename = stats . file . opts . filename ;
125
+
126
+ filenameMap [ filename ] = {
127
+ styleModuleImportMap : { }
128
+ } ;
115
129
}
116
130
}
117
131
} ;
0 commit comments