@@ -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,13 +65,15 @@ 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
71
72
let styleImportName : string ;
72
73
73
74
if ( path . node . specifiers . length === 0 ) {
74
- styleImportName = 'random-' + Math . random ( ) ;
75
+ // eslint-disable-next-line no-process-env
76
+ styleImportName = process . env . NODE_ENV === 'test' ? 'random-test' : 'random-' + Math . random ( ) ;
75
77
} else if ( path . node . specifiers . length === 1 ) {
76
78
styleImportName = path . node . specifiers [ 0 ] . local . name ;
77
79
} else {
@@ -81,36 +83,49 @@ export default ({
81
83
throw new Error ( 'Unexpected use case.' ) ;
82
84
}
83
85
84
- styleModuleImportMap [ styleImportName ] = requireCssModule ( targetResourcePath , {
86
+ filenameMap [ filename ] . styleModuleImportMap [ styleImportName ] = requireCssModule ( targetResourcePath , {
85
87
generateScopedName : stats . opts . generateScopedName
86
88
} ) ;
87
89
} ,
88
- JSXElement ( path : Object ) : void {
90
+ JSXElement ( path : Object , stats : Object ) : void {
91
+ const filename = stats . file . opts . filename ;
89
92
const styleNameAttribute = path . node . openingElement . attributes
90
93
. find ( ( attribute ) => {
91
- return attribute . name . name === 'styleName' ;
94
+ return typeof attribute . name !== 'undefined' && attribute . name . name === 'styleName' ;
92
95
} ) ;
93
96
94
97
if ( ! styleNameAttribute ) {
95
98
return;
96
99
}
97
100
98
101
if ( t . isStringLiteral ( styleNameAttribute . value ) ) {
99
- resolveStringLiteral ( path , styleModuleImportMap , styleNameAttribute ) ;
102
+ resolveStringLiteral (
103
+ path ,
104
+ filenameMap [ filename ] . styleModuleImportMap ,
105
+ styleNameAttribute
106
+ ) ;
100
107
101
108
return ;
102
109
}
103
110
104
111
if ( t . isJSXExpressionContainer ( styleNameAttribute . value ) ) {
105
- if ( ! importedHelperIndentifier ) {
106
- setupFileForRuntimeResolution ( path ) ;
112
+ if ( ! filenameMap [ filename ] . importedHelperIndentifier ) {
113
+ setupFileForRuntimeResolution ( path , filename ) ;
107
114
}
108
-
109
- replaceJsxExpressionContainer ( t , styleNameAttribute , importedHelperIndentifier , styleModuleImportMapIdentifier ) ;
115
+ replaceJsxExpressionContainer (
116
+ t ,
117
+ styleNameAttribute ,
118
+ filenameMap [ filename ] . importedHelperIndentifier ,
119
+ filenameMap [ filename ] . styleModuleImportMapIdentifier
120
+ ) ;
110
121
}
111
122
} ,
112
- Program ( ) {
113
- styleModuleImportMap = { } ;
123
+ Program ( path : Object , stats : Object ) : void {
124
+ const filename = stats . file . opts . filename ;
125
+
126
+ filenameMap [ filename ] = {
127
+ styleModuleImportMap : { }
128
+ } ;
114
129
}
115
130
}
116
131
} ;
0 commit comments