Different hashes between css-loader and babel-plugin-react-css-modules

1.8k views Asked by At

I trying to setup own simple webpack config with css-modules. And problem is that I getting different hashes by css-loader and babel css modules plugin in css classes names.

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");


module.exports = {
    context: __dirname,
    entry: "./src/index.js",
    output: {
        path: path.join(__dirname, "/dist"),
        filename: "index_bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: [
                    "style-loader",
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1,
                            modules: {
                                localIdentName: "__[local]___[hash:base64:5]",
                            },
                        }
                    }
                ]
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: "babel-loader",
                        options: {
                            plugins: [
                                [
                                    'react-css-modules',
                                    {
                                        generateScopedName: "__[local]___[hash:base64:5]",
                                        autoResolveMultipleImports: true,
                                        webpackHotModuleReloading: false
                                    }
                                ]
                            ]
                        }
                    }
                ]
            },

        ]
    },
    plugins: [
        new webpack.EnvironmentPlugin({ NODE_ENV: 'development', 'BABEL_ENV': 'development' }),
        new HtmlWebpackPlugin({
            template: "./src/index.html"
        }),
    ]
};
<html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>React Boilerplate</title>
<script defer="" src="index_bundle.js"></script>
<style>.App_labeltext__1dbpr {
    color: #96db27;
    text-align: center;
}</style></head>

<body>
<div id="root"><div><h1>My React App!</h1><div class="__labeltext___3QMuY">TEXT</div></div></div>


</body></html>
1

There are 1 answers

2
tmhao2005 On BEST ANSWER

It seems this issue is yet resolved yet which is open here. Basically you can use this forked package @dr.pogodin/babel-plugin-react-css-modules to fix the compatibility with css-loader in case of generating the name.

The idea would look like:

{
  plugins: [
    "@dr.pogodin/babel-plugin-react-css-modules", 
    {
      generateScopedName: "__[local]___[hash:base64:5]",
      autoResolveMultipleImports: true,
      webpackHotModuleReloading: false
    },
  ]
}

Or I think you can fix with a way by customizing the function to generate the hash name directly in the plugin which is addressed here