javascriptnode.jswebpackwebpack-2extract-text-plugin

Webpack Extract-Text-Plugin localIdentName not working in DOM


I saw the FlipKart , they made hash all of their classNames and its CSSes. I know that they use SCSS for building CSS. How can I config my WebPack to make my exportation like their:

enter image description here

This is my webpack:

var webpack = require('webpack'),
path = require('path'),
ExtractTextPlugin = require('extract-text-webpack-plugin'),
UglifyJSPlugin = require('uglifyjs-webpack-plugin'),
OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');

var DistDir = path.resolve(__dirname, 'app/dist'),
SrcDir = path.resolve(__dirname, 'app/src');

module.exports = {
entry: SrcDir,
output: {
    path: DistDir,
    filename: "bundle.min.js"
},
module: {
    rules: [
        {
            test: /\.js$/,
            exclude: /node_modules/,
            include: SrcDir,
            loader: 'babel-loader'
        },
        {
            test: /\.(scss|css)$/,
            exclude: /node_modules/,
            use: ExtractTextPlugin.extract({
                use: [{
                    loader: "css-loader", options: {
                        modules: true,
                        localIdentName: '[hash:base64:3]',
                        sourceMap: false
                    }
                }, {
                    loader: "sass-loader", options: {
                        sourceMap: false
                    }
                }],
                fallback: "style-loader"
            })
        },
        {
            test: /\.png$/,
            loader: "file-loader",
            exclude: /node_modules/
        }
    ]
},
plugins: [
    new webpack.DefinePlugin({
        'process.env': {
            'NODE_ENV': JSON.stringify('production')
        }
    }),
    new ExtractTextPlugin("bundle.min.css"),
    new OptimizeCssAssetsPlugin({
        cssProcessor: require('cssnano'),
        cssProcessorOptions: { discardComments: {removeAll: true } },
        canPrint: true
    }),
    new UglifyJSPlugin({
        compress: {
            unused: true,
            dead_code: true,
            warnings: false,
            drop_debugger: true,
            conditionals: true,
            evaluate: true,
            drop_console: true,
            sequences: true,
            booleans: true
        },
        comments: false
    }),
    new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]),
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.optimize.AggressiveMergingPlugin()
]
};

But It just hash my class names in CSS file and class names in DOM are in dev version, for example one of class name element in DOM is product__bag--red, its class name in CSS file is _1x4.

How I config webpack or extract-text-plugin for I see _1x4 instead of product__bag--red in DOM?


Solution

  • Separate CSS file:

    .app {
      display: flex;
    }
    

    Separate React component file:

    import style from './file.css'
    
    class Component extends React.Component {
       render () {
         return (
           <div className={style.app}></div> 
        )
      }
    }
    

    Separate common JavaScript file:

    import style from './file.css'
    
    document.body.innerHTML =  `
       <div class=${style.app}></div>
    `
    

    When modules are set we must get the classNames on the CSS import as an {Object}