
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
mangle-css-class-webpack-plugin
Advanced tools
Minifies and obfuscates the class names in your existing JavaScript, CSS, and HTML without any modern css modules.
Minifies and obfuscates the class names in JavaScript, CSS, and HTML.
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).
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.
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(\\\\\\\\\\\\\\\\
).
The class names won't be used.
e.g.
reserveClassName: ['fa', 'fas', 'far'],
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
.
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
.
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.
}
When truthy, the plugin will also mangle CSS variables (custom properties) whose name is matching the same classNameRegExp
. Disabled by default.
<!-- html -->
<main class='l-abc'>
<div class='l-efg' />
</main>
// js
document.querySelector('l-efg');
// css
.l-abc {
}
.l-abc .l-efg {
}
<!-- html -->
<main class='a'>
<div class='b' />
</main>
// js
document.querySelector('b');
// css
.a {
}
.a .b {
}
FAQs
Minifies and obfuscates the class names in your existing JavaScript, CSS, and HTML without any modern css modules.
The npm package mangle-css-class-webpack-plugin receives a total of 952 weekly downloads. As such, mangle-css-class-webpack-plugin popularity was classified as not popular.
We found that mangle-css-class-webpack-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.