
Security News
GitHub Actions Pricing Whiplash: Self-Hosted Actions Billing Change Postponed
GitHub postponed a new billing model for self-hosted Actions after developer pushback, but moved forward with hosted runner price cuts on January 1.
minify-css-idents
Advanced tools
Webpack plug-in using css-loader to shorten identifiers and make CSS files lighter.
For maximum efficiency, also use a CSS minifier like css-minimizer-webpack-plugin/.
npm install --save-dev minify-css-idents
This is the typical configuration that should be compatible all use cases.
Just replace the css-loader with minify-css-idents/css-loader, ie:
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
"style-loader",
{
loader: "minify-css-idents/css-loader",
options: {
// Your usual css-loader configuration here,
// to setup how to build unminified CSS identifiers
}
},
"postcss-loader"
]
}
]
}
};
Then add the minifier to the plugins:
const MinifyCssIdentsPlugin = require("minify-css-idents");
module.exports = {
plugins: [
new MinifyCssIdentsPlugin({
filename: "path-to/idents-map.json",
exclude: ["global-identifiers-here"]
})
]
};
Available options to specify in the instanciation of MinifyCssIdentsPlugin:
new MinifyCssIdentsPlugin({
enabled: true,
exclude: ["some-ident", "some-ident-prefix-*"],
filename: "path-to/idents.map.json",
mapIndent: 2,
mode: "default",
startIdent: "some-minified-ident-to-start-with",
});
Default value: Webpack's optimization.minimize option value when set, otherwise true when mode is set to "production" or omitted and false in any other mode.
Enables/disables the minification of CSS identifiers.
Default value: ["ad*", "app", "root"]
Identifiers of identifier prefixes the minifier should not generate. You should put there all global identifiers your project uses that could match a minified one.
A global identifier is one that wouldn't get changed by css-loaded, either because it is wrapped by :global(), or because the CSS is not processed by css-loader to begin with (typically the stylesheet of an external module in which classnames are hard-coded).
You may also add there identifiers that may be problematics, like any identifier beginning with "ad".
Also, note that a global identifier/prefix longer than 10 characters, not beginning with a letter or having characters others than letters and digits cannot match a minified identifier. It is then not necessary to specify it in this option and it would be ignored anyway.
Default value: null
Pathname to the identifier map file. Useful if:
Default value: 2
The indentation size to use in the identifier map file. Set it to 0 if you want a minimal file without spaces and line returns.
Default value: "default"
Specify what to do with the identifier map file.
For instance, the first build step should only create then map, while the last one should only load it, and eventually delete it when done.
Possible values:
"default" | Load the map if available, add newly generated identifiers if any |
"load-map" | Only load the map |
"extend-map" | Load the map, add newly generated identifiers if any |
"consume-map" | Load the map, delete it when done |
"create-map" | Only create the map with generated identifiers |
Please note that "default" is the only mode where the minifier won't throw an error if it doesn't find the map file when trying to load it.
The creation, update or deletion of the map will occur when Webpack is done processing assets.
Default value: null
Identifier to start the generation with.
Please note that this identifier will be skipped if it matches a value in the "exclude" option.
Should minify-css-idents/css-loader not work properly or should your configuration not allow to use it, you may rely on the MinifyCssIdentsPlugin.getLocalIdent function, ie:
const MinifyCssIdentsPlugin = require("minify-css-idents");
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: {
getLocalIdent: MinifyCssIdentsPlugin.getLocalIdent
}
}
},
"postcss-loader"
]
}
]
}
};
If you already rely on a custom getLocalIdent function to generate unminified CSS identifiers, you may specify it:
modules: {
getLocalIdent: MinifyCssIdentsPlugin.getLocalIdent(your_former_getLocalIdent_here)
}
The minify-css-idents/css-loader wraps css-loader in order to override its getLocalIdent option, which allows to specify a function to generate CSS identifiers.
A minified identifier is a positive integer number representation in base 36 not beginning with a digit. Because of JavaScript's integer limitations (see Number.MAX_SAFE_INTEGER), they are limited to 10 characters. It matches the regular expression /^[a-z][0-9a-z]{0,9}$/i.
In simpler terms, a minified identifier is a letter eventually followed by up to 9 letters and digits.
Before generating a minified identifier, the MinifyCssIdentsPlugin.getLocalIdent function generates an unminified one just as css-loader would, using the getLocalIdent function specified in the loader options, or the default one provided with css-loader.
When MinifyCssIdentsPlugin is registered in Webpack's plug-ins, it has the opportunity to create, update and/or load an indentifier map file.
This feature is critical to keep the identifiers consistent across build steps.
It uses the beforeCompile hook of Webpack's compiler to read the map file, then the compilation and afterProcessAssets hooks to write/delete it.
When MinifyCssIdentsPlugin is omitted, it will instanciate automatically with its default options. However, it might not be able to detect the value of Webpack's optimization.minimize option in the future, as the way of accessing the compiler's options from a loader is deprecated.
It does not accept regular expressions for two reasons:
Such a feature could be developed on request, but at the moment it just seems unecessary.
Adrien Febvay https://github.com/adrien-febvay
Gajus Kuizinas https://github.com/gajus
For writing an article about Reducing CSS bundle size 70% by cutting the class names and using scope isolation.
FAQs
Minify CSS identifiers in order to emit smaller CSS files
We found that minify-css-idents 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
GitHub postponed a new billing model for self-hosted Actions after developer pushback, but moved forward with hosted runner price cuts on January 1.

Research
Destructive malware is rising across open source registries, using delays and kill switches to wipe code, break builds, and disrupt CI/CD.

Security News
Socket CTO Ahmad Nassri shares practical AI coding techniques, tools, and team workflows, plus what still feels noisy and why shipping remains human-led.