6

For testing/learning purposes I am using an ejected version of create-react-app 3.4.1 which comes with css-loader 3.4.2, and I am trying to reproduce these results where the css selectors are written in kebab-case, but the js styles object converts them to camelCase:

styles.css

.foo-baz {
  color: red;
}
.bar {
  color: blue;
}

index.js

import styles from './styles.css';

console.log(styles);
// expected result: { fooBaz, bar }
// actual, default results: { foo-baz, bar }

According to the css-loader changelog in version 3.0.0:

exportLocalsStyle option was remove in favor localsConvention option, also it is accept only {String} value (use 'camelCase' value if you previously value was true and 'asIs' if you previously value was false)

So I tried to do that:

webpack.config.js

...
{
  test: cssRegex,
  exclude: cssModuleRegex,
  use: getStyleLoaders({
    importLoaders: 1,
    sourceMap: isEnvProduction && shouldUseSourceMap,
    localsConvention: "camelCase", // my only addition is this line
  }),
  sideEffects: true,
},
...

Running npm start and npm run build both work successfully, however the conversion does not happen and I am still stuck with kebab-case in my js files, even when adding a console.log(styles) on my App.js file, the output object printed to the console is still kebab-case keys and subsequent kebab-case values.

Am I missing something here? Perhaps It's very possible I don't have a clear understanding on the pre/post css compiling process and I am attempting to inject this logic in the wrong place? Does anyone have a working example of utilizing this kebab to camelCase capabilities in css-loader 3.*?

Sidenote: I do not want to update the css-loader package to it's latest version, I am intending on bringing this information back into a non-ejected create-react-app and then update the config with craco - the less I have to change the better.

2 Answers2

3

I got this fixed by exportLocalsConvention in modules section:

{
    loader: 'css-loader',
    options: {
        modules: {
            localIdentName: '[name]_[local]_[hash:base64:6]',
            exportLocalsConvention: 'camelCase'
        }
    }
},
Vočko
  • 2,478
  • 1
  • 23
  • 31
  • Are you definitely using css-loader 3.4.2 ? – k1eran Jul 01 '21 at 00:06
  • 1
    Not sure what version I was using at the time of writing, atm on 5.2.4 and it's working. I'm trying to always be on the latest. – Vočko Jul 01 '21 at 04:37
  • This is the correct answer as of writing, based on the documentation. https://github.com/webpack-contrib/css-loader – Daryl Teo Oct 24 '21 at 04:21
2

I got this working by specifying localsConvention option as,

{
    loader: 'css-loader',
     options: { 
        modules: {
            localIdentName: '[name]_[local]_[hash:base64:5]',
        },
        localsConvention: 'camelCase'
     }
},

Hope this helps someone, as I had to spend few hours to find the correct fix.

Vishmi Money
  • 144
  • 1
  • 5
  • This is only for old versions of css-loader. As of writing, the updated config is the above answer. https://github.com/webpack-contrib/css-loader – Daryl Teo Oct 24 '21 at 04:20
  • Yes, this question is about `css-loader 3.4.2`. So, if you are using an old version you will have to specify the options this way. – Vishmi Money Nov 13 '21 at 20:42