S
S
svm2020-11-30 14:32:42
webpack
svm, 2020-11-30 14:32:42

How to bind css-loader modules:true to html (renaming classes in css, js and html files)?

I'm trying to shorten (or obfuscate) the names of css classes using css-modules in webpack, but I get the desired result only in the css file, and in the html and js files the class names are not renamed (the original ones remain).

webpack.config.js
const path = require('path');
const HtmlPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
  mode: "development",
  devtool: 'source-map',
  entry: {
    main: './src/main.js'
  },
  output: {
    filename: 'js/[name].[hash].js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlPlugin({
      template: "./src/index.html"
    }),
    new MiniCssExtractPlugin({
      filename: 'css/[name].[hash].css'
    }),
  ],
  module: {
    rules: [
      {
        test: /\.s[ac]ss$/i,
        use: [
          MiniCssExtractPlugin.loader, // extract css to file
          {
            loader: 'css-loader',
            options: {
              modules: {
                localIdentName: '[sha1:hash:hex:4]'
              }
            }
          },
          "sass-loader", // Compiles Sass to CSS
        ],
      },
    ],
  }
};

webpack 5.9.0
css-loader 5.0.1

Or maybe there is a better solution to achieve the desired result, then I will also be very grateful.

I want to get something like this
1*mGuDYFM56iyLi1MgZPC8bw.png

https://www.freecodecamp.org/news/reducing-css-bun...
https://develoger.com/how-to-obfuscate-css-class-n...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
viktorleg, 2021-08-23
@svm

You probably don't need help anymore, but for those who do , this plugin will make it easier.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question