Skip to content

Commit 987d454

Browse files
feat: added esbuild minimizer (#122)
1 parent c9a11b2 commit 987d454

File tree

7 files changed

+531
-49
lines changed

7 files changed

+531
-49
lines changed

README.md

+13-38
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
This plugin uses [cssnano](https://cssnano.co) to optimize and minify your CSS.
1818

19-
Just like [optimize-css-assets-webpack-plugin](https://github.com/NMFR/optimize-css-assets-webpack-plugin) but more accurate with source maps and assets using query string, allows to cache and works in parallel mode.
19+
Just like [optimize-css-assets-webpack-plugin](https://github.com/NMFR/optimize-css-assets-webpack-plugin) but more accurate with source maps and assets using query string, allows caching and works in parallel mode.
2020

2121
## Getting Started
2222

@@ -169,8 +169,8 @@ Default: `true`
169169
Use multi-process parallel running to improve the build speed.
170170
Default number of concurrent runs: `os.cpus().length - 1`.
171171

172-
> ℹ️ Parallelization can speedup your build significantly and is therefore **highly recommended**.
173-
> If a parallelization is enabled, the packages in `minimizerOptions` must be required via strings (`packageName` or `require.resolve(packageName)`). Read more in [`minimizerOptions`](#minimizerOptions)
172+
> ℹ️ Parallelization can speed up your build significantly and is therefore **highly recommended**.
173+
> If a parallelization is enabled, the packages in `minimizerOptions` must be required via strings (`packageName` or `require.resolve(packageName)`). Read more in [`minimizerOptions`](#minimizeroptions)
174174
175175
#### `Boolean`
176176

@@ -215,15 +215,16 @@ module.exports = {
215215
Type: `Function|Array<Function>`
216216
Default: `CssMinimizerPlugin.cssnanoMinify`
217217

218-
Allows to override default minify function.
219-
By default plugin uses [cssnano](https://github.com/cssnano/cssnano) package.
218+
Allows overriding default minify function.
219+
By default, plugin uses [cssnano](https://github.com/cssnano/cssnano) package.
220220
Useful for using and testing unpublished versions or forks.
221221

222222
Possible options:
223223

224224
- CssMinimizerPlugin.cssnanoMinify
225225
- CssMinimizerPlugin.cssoMinify
226226
- CssMinimizerPlugin.cleanCssMinify
227+
- CssMinimizerPlugin.esbuildMinify
227228
- `async (data, inputMap, minimizerOptions) => {return {code: "a{color: red}", map: "...", warnings: [], errors: []}}`
228229

229230
> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
@@ -345,7 +346,7 @@ module.exports = {
345346
Type: `Object`
346347
Default: `{ to: assetName, from: assetName }`
347348

348-
Allows to specify options [`processoptions`](https://postcss.org/api/#processoptions) for the cssnano.
349+
Allows filtering options [`processoptions`](https://postcss.org/api/#processoptions) for the cssnano.
349350
The `parser`,` stringifier` and `syntax` can be either a function or a string indicating the module that will be imported.
350351

351352
> ⚠️ **If a function is passed, the `parallel` option must be disabled.**.
@@ -392,7 +393,7 @@ module.exports = {
392393
Type: `Function<(warning, file, source) -> Boolean>`
393394
Default: `() => true`
394395

395-
Allow to filter css-minimizer warnings (By default [cssnano](https://github.com/cssnano/cssnano)).
396+
Allow filtering css-minimizer warnings (By default [cssnano](https://github.com/cssnano/cssnano)).
396397
Return `true` to keep the warning, a falsy value (`false`/`null`/`undefined`) otherwise.
397398

398399
> ⚠️ The `source` argument will contain `undefined` if you don't use source maps.
@@ -481,11 +482,6 @@ module.exports = {
481482

482483
### Using custom minifier [csso](https://github.com/css/csso)
483484

484-
By default plugin uses [cssnano](https://github.com/cssnano/cssnano) package.
485-
It is possible to use another minify function.
486-
487-
> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
488-
489485
**webpack.config.js**
490486

491487
```js
@@ -495,28 +491,9 @@ module.exports = {
495491
minimize: true,
496492
minimizer: [
497493
new CssMinimizerPlugin({
498-
minify: async (data, inputMap) => {
499-
const csso = require("csso");
500-
const sourcemap = require("source-map");
501-
502-
const [[filename, input]] = Object.entries(data);
503-
const minifiedCss = csso.minify(input, {
504-
filename: filename,
505-
sourceMap: true,
506-
});
507-
508-
if (inputMap) {
509-
minifiedCss.map.applySourceMap(
510-
new sourcemap.SourceMapConsumer(inputMap),
511-
filename
512-
);
513-
}
514-
515-
return {
516-
code: minifiedCss.css,
517-
map: minifiedCss.map.toJSON(),
518-
};
519-
},
494+
minify: CssMinimizerPlugin.cssoMinify,
495+
// Uncomment this line for options
496+
// minimizerOptions: { restructure: false },
520497
}),
521498
],
522499
},
@@ -543,7 +520,7 @@ module.exports = {
543520
};
544521
```
545522

546-
### Using custom minifier [csso](https://github.com/css/csso)
523+
### Using custom minifier [esbuild](https://github.com/evanw/esbuild)
547524

548525
**webpack.config.js**
549526

@@ -554,9 +531,7 @@ module.exports = {
554531
minimize: true,
555532
minimizer: [
556533
new CssMinimizerPlugin({
557-
minify: CssMinimizerPlugin.cssoMinify,
558-
// Uncomment this line for options
559-
// minimizerOptions: { restructure: false },
534+
minify: CssMinimizerPlugin.esbuildMinify,
560535
}),
561536
],
562537
},

0 commit comments

Comments
 (0)