@@ -10,11 +10,9 @@ import ajvKeywords from 'ajv-keywords';
1010import Ajv from 'ajv' ;
1111import optionsSchema from './schemas/optionsSchema.json' ;
1212import optionsDefaults from './schemas/optionsDefaults' ;
13- import createObjectExpression from './createObjectExpression' ;
1413import requireCssModule from './requireCssModule' ;
1514import resolveStringLiteral from './resolveStringLiteral' ;
16- import resolveJsxExpression from './resolveJsxExpression' ;
17- import replaceJsxExpressionContainer from './replaceJsxExpressionContainer' ;
15+ import resolveClassnameCallExpression from './resolveClassnameCallExpression' ;
1816
1917const ajv = new Ajv ( {
2018 // eslint-disable-next-line id-match
@@ -32,44 +30,44 @@ export default ({
3230} ) => {
3331 const filenameMap = { } ;
3432
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+ // };
7371
7472 const addWebpackHotModuleAccept = ( path ) => {
7573 const test = t . memberExpression ( t . identifier ( 'module' ) , t . identifier ( 'hot' ) ) ;
@@ -141,6 +139,22 @@ export default ({
141139 return {
142140 inherits : babelPluginJsxSyntax ,
143141 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+ } ,
144158 ImportDeclaration ( path : * , stats : * ) : void {
145159 if ( notForPlugin ( path , stats ) ) {
146160 return ;
@@ -210,35 +224,6 @@ export default ({
210224 handleMissingStyleName
211225 }
212226 ) ;
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- ) ;
242227 }
243228 }
244229 } ,
@@ -256,16 +241,16 @@ export default ({
256241 styleModuleImportMap : { }
257242 } ;
258243
259- if ( stats . opts . defaultCssFile ) {
244+ if ( stats . opts . defaultCssFile ) {
260245 filenameMap [ filename ] = {
261246 styleModuleImportMap : {
262- " default" : requireCssModule ( resolve ( stats . opts . defaultCssFile ) , {
247+ default : requireCssModule ( resolve ( stats . opts . defaultCssFile ) , {
263248 context : stats . opts . context ,
264249 filetypes : stats . opts . filetypes || { } ,
265250 generateScopedName : stats . opts . generateScopedName
266251 } )
267252 }
268- }
253+ } ;
269254 }
270255 }
271256 }
0 commit comments