I'm trying to use babel-plugin-react-css-modules in existing React project. Most of components work just fine, however there is a problem with 'passing' style to child components.
I have some parent components like this:
SearchBlock/index.js
import "./style.scss"
import { SettingsIcon } from "../../Icons"
...
<div styleName='SearchBlock'>
<SettingsIcon size='17' color='#2991F5' />
</div>
SearcBlock/style.scss
.SearchBlock {
...
.SettingsIcon {
margin-right: 7px;
}
...
}
And child components like this
SettingsIcon/index.js
const SettingsIcon = (props) => {
...
return (
<svg
width={size}
height={size}
viewBox='0 0 24 24'
fill='none'
xmlns='...'
className='SettingsIcon'
>
...
So the icon has an appropriate style in different components.
What I get in generated css files:
.src-components-Search-SearchBlock-___style__SearchBlock___2MeUy .src-components-Search-SearchBlock-___style__SettingsIcon___3SWQh {
margin-right: 7px; }
Seems correct. However, the child element is rendered like this:
<svg ... class="SettingsIcon"><</svg>
instead of
<svg ... class="src-components-Search-SearchBlock-___style__SettingsIcon___3SWQh"><</svg>
I cannot use styleName in settingsIcon because webpack throws error without importing at least one stylesheet.
Is there any way to fix this?
My configuration
webpack.common.js
...
test: /\.s?css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
modules: {
mode: 'local',
localIdentName: isProd
? '[hash:base64]'
: "[path]___[name]__[local]___[hash:base64:5]",
},
sourceMap: true,
importLoaders: 1,
onlyLocals: false,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
...
babel.config.js
const isProd = process.env.NODE_ENV === 'production';
function createReactCssModulesPlugin() {
return [
"react-css-modules",
{
filetypes: {
".scss": {
syntax: "postcss-scss",
plugins: [
"postcss-import-sync2",
[
"postcss-nested",
{
bubble: ["@include"],
preserveEmpty: true,
},
],
],
},
},
generateScopedName: isProd
? '[hash:base64]'
: "[path]___[name]__[local]___[hash:base64:5]",
webpackHotModuleReloading: isProd ? false : true,
exclude: "node_modules",
handleMissingStyleName: isProd ? "throw" : "warn",
autoResolveMultipleImports: true,
},
];
}
module.exports = function (api) {
api.cache(true)
return {
plugins: ["@babel/transform-react-jsx", createReactCssModulesPlugin()],
};
};