diff --git a/src/index.js b/src/index.js index b8bcbed..f4963e0 100644 --- a/src/index.js +++ b/src/index.js @@ -114,6 +114,7 @@ export default ({ } replaceJsxExpressionContainer( t, + path, styleNameAttribute, filenameMap[filename].importedHelperIndentifier, filenameMap[filename].styleModuleImportMapIdentifier diff --git a/src/replaceJsxExpressionContainer.js b/src/replaceJsxExpressionContainer.js index 1eb0b45..3faf4e6 100644 --- a/src/replaceJsxExpressionContainer.js +++ b/src/replaceJsxExpressionContainer.js @@ -5,16 +5,33 @@ import BabelTypes, { Identifier } from 'babel-types'; -export default (t: BabelTypes, styleNameAttribute: JSXAttribute, importedHelperIndentifier: Identifier, styleModuleImportMapIdentifier: Identifier): void => { +export default (t: BabelTypes, path: Object, styleNameAttribute: JSXAttribute, importedHelperIndentifier: Identifier, styleModuleImportMapIdentifier: Identifier): void => { const expressionContainerValue = styleNameAttribute.value; + const classNameAttribute = path.node.openingElement.attributes + .find((attribute) => { + return typeof attribute.name !== 'undefined' && attribute.name.name === 'className'; + }); + const classNameAttributeValue = classNameAttribute ? classNameAttribute.value.value : ''; + + if (classNameAttribute) { + path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(classNameAttribute), 1); + } + + const styleNameExpression = t.callExpression( + importedHelperIndentifier, + [ + expressionContainerValue.expression, + styleModuleImportMapIdentifier + ] + ); styleNameAttribute.value = t.jSXExpressionContainer( - t.callExpression( - importedHelperIndentifier, - [ - expressionContainerValue.expression, - styleModuleImportMapIdentifier - ] - ) + classNameAttribute ? + t.binaryExpression( + '+', + t.stringLiteral(classNameAttributeValue + ' '), + styleNameExpression + ) : styleNameExpression ); + styleNameAttribute.name.name = 'className'; }; diff --git a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/actual.js b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/actual.js similarity index 72% rename from test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/actual.js rename to test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/actual.js index f284d12..cb49c7a 100644 --- a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/actual.js +++ b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/actual.js @@ -5,4 +5,4 @@ const styleNameBar = 'bar.a-b'; const styleNameFoo = 'a-b';
; -
; +
; diff --git a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/bar.css b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/bar.css similarity index 100% rename from test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/bar.css rename to test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/bar.css diff --git a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/expected.js b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/expected.js similarity index 68% rename from test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/expected.js rename to test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/expected.js index 41d4f3e..e9f288f 100644 --- a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/expected.js +++ b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/expected.js @@ -13,5 +13,5 @@ const _styleModuleImportMap = { const styleNameBar = 'bar.a-b'; const styleNameFoo = 'a-b'; -
; -
; +
; +
; diff --git a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/foo.css b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/foo.css similarity index 100% rename from test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/foo.css rename to test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/foo.css diff --git a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/options.json b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/options.json similarity index 100% rename from test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/options.json rename to test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/options.json diff --git a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName/expected.js b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName/expected.js index 41d4f3e..336e388 100644 --- a/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName/expected.js +++ b/test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName/expected.js @@ -13,5 +13,5 @@ const _styleModuleImportMap = { const styleNameBar = 'bar.a-b'; const styleNameFoo = 'a-b'; -
; -
; +
; +
;