
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
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.
Version 1.0.3
FAQs
Minify CSS identifiers in order to emit smaller CSS files
The npm package minify-css-idents receives a total of 1 weekly downloads. As such, minify-css-idents popularity was classified as not popular.
We found that minify-css-idents demonstrated a healthy version release cadence and project activity because the last version was released less than 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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.