2

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 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Or there is option, suggested in issue thread: const genericNames = require('generic-names'); // v3.0.0 const CSS_MODULE_LOCAL_IDENT_NAME = '[local]___[hash:base64:5]'; // old: generateScopedName: CSS_MODULE_LOCAL_IDENT_NAME generateScopedName: genericNames(CSS_MODULE_LOCAL_IDENT_NAME)
why there is no this issue in create-creact-app script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.