@@ -10,11 +10,9 @@ import ajvKeywords from 'ajv-keywords';
10
10
import Ajv from 'ajv' ;
11
11
import optionsSchema from './schemas/optionsSchema.json' ;
12
12
import optionsDefaults from './schemas/optionsDefaults' ;
13
- import createObjectExpression from './createObjectExpression' ;
14
13
import requireCssModule from './requireCssModule' ;
15
14
import resolveStringLiteral from './resolveStringLiteral' ;
16
- import resolveJsxExpression from './resolveJsxExpression' ;
17
- import replaceJsxExpressionContainer from './replaceJsxExpressionContainer' ;
15
+ import resolveClassnameCallExpression from './resolveClassnameCallExpression' ;
18
16
19
17
const ajv = new Ajv ( {
20
18
// eslint-disable-next-line id-match
@@ -32,44 +30,44 @@ export default ({
32
30
} ) => {
33
31
const filenameMap = { } ;
34
32
35
- const setupFileForRuntimeResolution = ( path , filename ) => {
36
- const programPath = path . findParent ( ( parentPath ) => {
37
- return parentPath . isProgram ( ) ;
38
- } ) ;
39
-
40
- filenameMap [ filename ] . importedHelperIndentifier = programPath . scope . generateUidIdentifier ( 'getClassName' ) ;
41
- filenameMap [ filename ] . styleModuleImportMapIdentifier = programPath . scope . generateUidIdentifier ( 'styleModuleImportMap' ) ;
42
-
43
- programPath . unshiftContainer (
44
- 'body' ,
45
- t . importDeclaration (
46
- [
47
- t . importDefaultSpecifier (
48
- filenameMap [ filename ] . importedHelperIndentifier
49
- )
50
- ] ,
51
- t . stringLiteral ( 'babel-plugin-react-css-modules/dist/browser/getClassName' )
52
- )
53
- ) ;
54
-
55
- const firstNonImportDeclarationNode = programPath . get ( 'body' ) . find ( ( node ) => {
56
- return ! t . isImportDeclaration ( node ) ;
57
- } ) ;
58
-
59
- firstNonImportDeclarationNode . insertBefore (
60
- t . variableDeclaration (
61
- 'const' ,
62
- [
63
- t . variableDeclarator (
64
- filenameMap [ filename ] . styleModuleImportMapIdentifier ,
65
- createObjectExpression ( t , filenameMap [ filename ] . styleModuleImportMap )
66
- )
67
- ]
68
- )
69
- ) ;
70
- // eslint-disable-next-line no-console
71
- // console.log('setting up', filename, util.inspect(filenameMap,{depth: 5}))
72
- } ;
33
+ // const setupFileForRuntimeResolution = (path, filename) => {
34
+ // const programPath = path.findParent((parentPath) => {
35
+ // return parentPath.isProgram();
36
+ // });
37
+
38
+ // filenameMap[filename].importedHelperIndentifier = programPath.scope.generateUidIdentifier('getClassName');
39
+ // filenameMap[filename].styleModuleImportMapIdentifier = programPath.scope.generateUidIdentifier('styleModuleImportMap');
40
+
41
+ // programPath.unshiftContainer(
42
+ // 'body',
43
+ // t.importDeclaration(
44
+ // [
45
+ // t.importDefaultSpecifier(
46
+ // filenameMap[filename].importedHelperIndentifier
47
+ // )
48
+ // ],
49
+ // t.stringLiteral('babel-plugin-react-css-modules/dist/browser/getClassName')
50
+ // )
51
+ // );
52
+
53
+ // const firstNonImportDeclarationNode = programPath.get('body').find((node) => {
54
+ // return !t.isImportDeclaration(node);
55
+ // });
56
+
57
+ // firstNonImportDeclarationNode.insertBefore(
58
+ // t.variableDeclaration(
59
+ // 'const',
60
+ // [
61
+ // t.variableDeclarator(
62
+ // filenameMap[filename].styleModuleImportMapIdentifier,
63
+ // createObjectExpression(t, filenameMap[filename].styleModuleImportMap)
64
+ // )
65
+ // ]
66
+ // )
67
+ // );
68
+ // // eslint-disable-next-line no-console
69
+ // // console.log('setting up', filename, util.inspect(filenameMap,{depth: 5}))
70
+ // };
73
71
74
72
const addWebpackHotModuleAccept = ( path ) => {
75
73
const test = t . memberExpression ( t . identifier ( 'module' ) , t . identifier ( 'hot' ) ) ;
@@ -141,6 +139,22 @@ export default ({
141
139
return {
142
140
inherits : babelPluginJsxSyntax ,
143
141
visitor : {
142
+ CallExpression ( path : * , stats : * ) : void {
143
+ const filename = stats . file . opts . filename ;
144
+
145
+ const handleMissingStyleName = stats . opts && stats . opts . handleMissingStyleName || optionsDefaults . handleMissingStyleName ;
146
+
147
+ if ( t . isIdentifier ( path . node . callee , { name : 'classnames' } ) && ! t . isJSXExpressionContainer ( path . parentPath . node ) ) {
148
+ resolveClassnameCallExpression (
149
+ path ,
150
+ stats ,
151
+ filenameMap [ filename ] . styleModuleImportMap ,
152
+ {
153
+ handleMissingStyleName
154
+ }
155
+ ) ;
156
+ }
157
+ } ,
144
158
ImportDeclaration ( path : * , stats : * ) : void {
145
159
if ( notForPlugin ( path , stats ) ) {
146
160
return ;
@@ -210,35 +224,6 @@ export default ({
210
224
handleMissingStyleName
211
225
}
212
226
) ;
213
- } else if ( t . isJSXExpressionContainer ( attribute . value ) ) {
214
- if ( t . isCallExpression ( attribute . value . expression ) || t . isIdentifier ( attribute . value . expression ) ) {
215
- resolveJsxExpression (
216
- path ,
217
- stats ,
218
- filenameMap [ filename ] . styleModuleImportMap ,
219
- attribute ,
220
- destinationName ,
221
- {
222
- handleMissingStyleName
223
- }
224
- ) ;
225
-
226
- return ;
227
- }
228
- if ( ! filenameMap [ filename ] . importedHelperIndentifier ) {
229
- setupFileForRuntimeResolution ( path , filename ) ;
230
- }
231
- replaceJsxExpressionContainer (
232
- t ,
233
- path ,
234
- attribute ,
235
- destinationName ,
236
- filenameMap [ filename ] . importedHelperIndentifier ,
237
- filenameMap [ filename ] . styleModuleImportMapIdentifier ,
238
- {
239
- handleMissingStyleName
240
- }
241
- ) ;
242
227
}
243
228
}
244
229
} ,
@@ -256,16 +241,16 @@ export default ({
256
241
styleModuleImportMap : { }
257
242
} ;
258
243
259
- if ( stats . opts . defaultCssFile ) {
244
+ if ( stats . opts . defaultCssFile ) {
260
245
filenameMap [ filename ] = {
261
246
styleModuleImportMap : {
262
- " default" : requireCssModule ( resolve ( stats . opts . defaultCssFile ) , {
247
+ default : requireCssModule ( resolve ( stats . opts . defaultCssFile ) , {
263
248
context : stats . opts . context ,
264
249
filetypes : stats . opts . filetypes || { } ,
265
250
generateScopedName : stats . opts . generateScopedName
266
251
} )
267
252
}
268
- }
253
+ } ;
269
254
}
270
255
}
271
256
}
0 commit comments