Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mangle-css-class-webpack-plugin

sndyuk2.7kMIT5.1.0

Minifies and obfuscates the class names in your existing JavaScript, CSS, and HTML without any modern css modules.

Webpack, css, minify, mangle

readme

Build Status

mangle-css-class-webpack-plugin

Minifies and obfuscates the class names in JavaScript, CSS, and HTML.

Install

  npm i --save-dev mangle-css-class-webpack-plugin
  yarn add --dev mangle-css-class-webpack-plugin

The latest version WORKS ONLY with Webpack 5. For Webpack v4 & v3 support, install version 4.x(mangle-css-class-webpack-plugin@4.0.12).

Usage

The plugin will generate optimized class name in HTML, JavaScript, and CSS files. configure as follows:

webpack.config.js

const MangleCssClassPlugin = require('mangle-css-class-webpack-plugin');

module.exports = {
  ...
  plugins: [
    new MangleCssClassPlugin({
      classNameRegExp: '[cl]-[a-z][a-zA-Z0-9_]*',
      mangleCssVariables: true,
      log: true,
    }),
  ],
};

This will replace class name matched regex in HTML, JavaScript, CSS files. Identify the class names not to match unexpected words since it replaces all words that are matched with the classNameRegExp. I suggest that your class names have specific prefix or suffix that identified as a class name.

Options

classNameRegExp

e.g. '(abc-|efg-)?[cl]-[a-z][a-zA-Z0-9_]*'
the sample regexp maches l-main, c-textbox, l-main__header, abc-textbox__input, and so on...

If you want to use the back slash \ on the regexp, use [\\\\]* to match class names contained both JS(\\\\) and CSS(\\\\\\\\\\\\\\\\).

reserveClassName

The class names won't be used.
e.g.

reserveClassName: ['fa', 'fas', 'far'],

ignorePrefix

The prefix will be ignored from mangling.
e.g.

classNameRegExp: '(abc-|efg-)?[cl]-[a-z][a-zA-Z0-9_]*',
ignorePrefix: ['abc-', 'efg-'],

In this case, abc-c-textbox__input becomes abc-a.

ignorePrefixRegExp

Same behavior as ignorePrefix.
e.g.

classNameRegExp: '((hover|focus|xs|md|sm|lg|xl)[\\\\]*:)*tw-[a-z_-][a-zA-Z0-9_-]*',
ignorePrefixRegExp: '((hover|focus|xs|md|sm|lg|xl)[\\\\]*:)*',

In this case, hover\:xs\:c-textbox__input becomes hover\:xs\:a.

classGenerator

Override the default class name generator.

// original: original class name
// opts: options of the plugin
// context: own context of the class generator(initial value is just an empty object)
classGenerator: (original, opts, context) => {
  // return custom generated class name.
  // Or return undefined if you want to leave it to the original behavior.
}

mangleCssVariables

When truthy, the plugin will also mangle CSS variables (custom properties) whose name is matching the same classNameRegExp. Disabled by default.

Example

Source code

<!-- html -->
<main class='l-abc'>
  <div class='l-efg' />
</main>
// js
document.querySelector('l-efg');
// css
.l-abc {
}
.l-abc .l-efg {
}

Output code

<!-- html -->
<main class='a'>
  <div class='b' />
</main>
// js
document.querySelector('b');
// css
.a {
}
.a .b {
}

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.1.0 (2023-08-26)

Features

  • introduces mangleCssVariables option (46e6b1f), closes #51

5.0.9 (2022-10-11)

5.0.8 (2022-05-22)

5.0.7 (2022-01-05)

5.0.6 (2021-12-30)

Bug Fixes

  • adapt to webpack 5 (5f76d5b)
  • use right methods (ca40bf2)
  • plugin: use webpack 5 processAssets hook (cf16845)

5.0.5 (2021-12-18)

  • adapt to webpack 5 (5f76d5b)
  • use right methods (ca40bf2)
  • plugin: use webpack 5 processAssets hook (cf16845)

4.0.12 (2021-03-29)

4.0.11 (2020-05-29)

4.0.10 (2020-03-20)

4.0.9 (2020-02-22)

4.0.8 (2020-02-22)

4.0.7 (2020-02-21)

4.0.6 (2020-02-20)

4.0.3 (2018-06-13)

4.0.2 (2018-06-13)

4.0.1 (2018-06-13)