Skip to content

Commit 8d60fc9

Browse files
docs: custom css minifiers (#29)
1 parent 37d64eb commit 8d60fc9

6 files changed

+215
-2
lines changed

README.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ module.exports = {
328328
return {
329329
css: result.css,
330330
map: result.map,
331-
error: result.error,
332331
warnings: result.warnings(),
333332
};
334333
});
@@ -460,6 +459,80 @@ module.exports = {
460459
};
461460
```
462461

462+
### Using custom minifier [csso](https://github.com/css/csso)
463+
464+
By default plugin uses [cssnano](https://github.com/cssnano/cssnano) package.
465+
It is possible to use another minify function.
466+
467+
> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
468+
469+
**webpack.config.js**
470+
471+
```js
472+
module.exports = {
473+
devtool: 'source-map',
474+
optimization: {
475+
minimize: true,
476+
minimizer: [
477+
new CssMinimizerPlugin({
478+
minify: ({ input, postcssOptions }) => {
479+
// eslint-disable-next-line global-require
480+
const csso = require('csso');
481+
482+
const minifiedCss = csso.minify(input, {
483+
filename: postcssOptions.from,
484+
sourceMap: true,
485+
});
486+
487+
return {
488+
css: minifiedCss.css,
489+
map: minifiedCss.map.toJSON(),
490+
};
491+
},
492+
}),
493+
],
494+
},
495+
};
496+
```
497+
498+
### Using custom minifier [clean-css](https://github.com/jakubpawlowicz/clean-css)
499+
500+
By default plugin uses [cssnano](https://github.com/cssnano/cssnano) package.
501+
It is possible to use another minify function.
502+
503+
> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
504+
505+
**webpack.config.js**
506+
507+
```js
508+
module.exports = {
509+
devtool: 'source-map',
510+
optimization: {
511+
minimize: true,
512+
minimizer: [
513+
new CssMinimizerPlugin({
514+
minify: async ({ input, postcssOptions }) => {
515+
// eslint-disable-next-line global-require
516+
const CleanCSS = require('clean-css');
517+
518+
const minifiedCss = await new CleanCSS({ sourceMap: true }).minify({
519+
[postcssOptions.from]: {
520+
styles: input,
521+
},
522+
});
523+
524+
return {
525+
css: minifiedCss.styles,
526+
map: minifiedCss.sourceMap.toJSON(),
527+
warnings: minifiedCss.warnings,
528+
};
529+
},
530+
}),
531+
],
532+
},
533+
};
534+
```
535+
463536
## Contributing
464537

465538
Please take a moment to read our contributing guidelines if you haven't yet done so.

package-lock.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@
6060
"@webpack-contrib/defaults": "^6.3.0",
6161
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
6262
"babel-jest": "^26.3.0",
63+
"clean-css": "^4.2.3",
6364
"commitlint-azure-pipelines-cli": "^1.0.3",
6465
"copy-webpack-plugin": "^6.0.3",
6566
"cross-env": "^7.0.2",
6667
"css-loader": "^4.2.1",
68+
"csso": "^4.0.3",
6769
"del": "^5.1.0",
6870
"del-cli": "^3.0.1",
6971
"eslint": "^7.7.0",

test/CssMinimizerPlugin.test.js

+78-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ describe('CssMinimizerPlugin', () => {
321321
return {
322322
css: result.css,
323323
map: result.map,
324-
error: result.error,
325324
warnings: result.warnings(),
326325
};
327326
});
@@ -608,4 +607,82 @@ describe('CssMinimizerPlugin', () => {
608607
resolve();
609608
});
610609
});
610+
611+
it('should work with custom csso minifier', async () => {
612+
const compiler = getCompiler({
613+
devtool: 'source-map',
614+
entry: {
615+
foo: `${__dirname}/fixtures/foo.css`,
616+
},
617+
});
618+
619+
new CssMinimizerPlugin({
620+
minify: ({ input, postcssOptions }) => {
621+
// eslint-disable-next-line global-require
622+
const csso = require('csso');
623+
624+
const minifiedCss = csso.minify(input, {
625+
filename: postcssOptions.from,
626+
sourceMap: true,
627+
});
628+
629+
return {
630+
css: minifiedCss.css,
631+
map: minifiedCss.map.toJSON(),
632+
};
633+
},
634+
}).apply(compiler);
635+
636+
const stats = await compile(compiler);
637+
638+
const maps = readAssets(compiler, stats, '.css.map');
639+
640+
Object.keys(maps).forEach((assetKey) => {
641+
expect(maps[assetKey]).toMatchSnapshot(assetKey);
642+
});
643+
644+
expect(readAssets(compiler, stats, '.css')).toMatchSnapshot('assets');
645+
expect(getErrors(stats)).toMatchSnapshot('error');
646+
expect(getWarnings(stats)).toMatchSnapshot('warning');
647+
});
648+
649+
it('should work with custom clean-css minifier', async () => {
650+
const compiler = getCompiler({
651+
devtool: 'source-map',
652+
entry: {
653+
foo: `${__dirname}/fixtures/foo.css`,
654+
},
655+
});
656+
657+
new CssMinimizerPlugin({
658+
minify: async ({ input, postcssOptions }) => {
659+
// eslint-disable-next-line global-require
660+
const CleanCSS = require('clean-css');
661+
662+
const minifiedCss = await new CleanCSS({ sourceMap: true }).minify({
663+
[postcssOptions.from]: {
664+
styles: input,
665+
},
666+
});
667+
668+
return {
669+
css: minifiedCss.styles,
670+
map: minifiedCss.sourceMap.toJSON(),
671+
warnings: minifiedCss.warnings,
672+
};
673+
},
674+
}).apply(compiler);
675+
676+
const stats = await compile(compiler);
677+
678+
const maps = readAssets(compiler, stats, '.css.map');
679+
680+
Object.keys(maps).forEach((assetKey) => {
681+
expect(maps[assetKey]).toMatchSnapshot(assetKey);
682+
});
683+
684+
expect(readAssets(compiler, stats, '.css')).toMatchSnapshot('assets');
685+
expect(getErrors(stats)).toMatchSnapshot('error');
686+
expect(getWarnings(stats)).toMatchSnapshot('warning');
687+
});
611688
});

test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack4

+26
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ exports[`CssMinimizerPlugin should work with child compilation: errors 1`] = `Ar
139139

140140
exports[`CssMinimizerPlugin should work with child compilation: warnings 1`] = `Array []`;
141141

142+
exports[`CssMinimizerPlugin should work with custom clean-css minifier: assets 1`] = `
143+
Object {
144+
"foo.css": "body{color:red}a{color:#00f}
145+
/*# sourceMappingURL=foo.css.map*/",
146+
}
147+
`;
148+
149+
exports[`CssMinimizerPlugin should work with custom clean-css minifier: error 1`] = `Array []`;
150+
151+
exports[`CssMinimizerPlugin should work with custom clean-css minifier: foo.css.map 1`] = `"{\\"version\\":3,\\"sources\\":[\\"webpack:///foo.css\\"],\\"names\\":[],\\"mappings\\":\\"AAAA,KACE,MAAO,IAET,EACE,MAAO\\",\\"file\\":\\"foo.css\\",\\"sourcesContent\\":[\\"body {\\\\n color: red;\\\\n}\\\\na {\\\\n color: blue;\\\\n}\\\\n\\"],\\"sourceRoot\\":\\"\\"}"`;
152+
153+
exports[`CssMinimizerPlugin should work with custom clean-css minifier: warning 1`] = `Array []`;
154+
155+
exports[`CssMinimizerPlugin should work with custom csso minifier: assets 1`] = `
156+
Object {
157+
"foo.css": "body{color:red}a{color:#00f}
158+
/*# sourceMappingURL=foo.css.map*/",
159+
}
160+
`;
161+
162+
exports[`CssMinimizerPlugin should work with custom csso minifier: error 1`] = `Array []`;
163+
164+
exports[`CssMinimizerPlugin should work with custom csso minifier: foo.css.map 1`] = `"{\\"version\\":3,\\"sources\\":[\\"webpack:///foo.css\\"],\\"names\\":[],\\"mappings\\":\\"AAAA,I,CACE,S,CAEF,C,CACE,U\\",\\"file\\":\\"foo.css\\",\\"sourcesContent\\":[\\"body {\\\\n color: red;\\\\n}\\\\na {\\\\n color: blue;\\\\n}\\\\n\\"],\\"sourceRoot\\":\\"\\"}"`;
165+
166+
exports[`CssMinimizerPlugin should work with custom csso minifier: warning 1`] = `Array []`;
167+
142168
exports[`CssMinimizerPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
143169
Object {
144170
"one.css": ".minify {};",

test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack5

+26
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,32 @@ exports[`CssMinimizerPlugin should work with child compilation: errors 1`] = `Ar
149149

150150
exports[`CssMinimizerPlugin should work with child compilation: warnings 1`] = `Array []`;
151151

152+
exports[`CssMinimizerPlugin should work with custom clean-css minifier: assets 1`] = `
153+
Object {
154+
"foo.css": "body{color:red}a{color:#00f}
155+
/*# sourceMappingURL=foo.css.map*/",
156+
}
157+
`;
158+
159+
exports[`CssMinimizerPlugin should work with custom clean-css minifier: error 1`] = `Array []`;
160+
161+
exports[`CssMinimizerPlugin should work with custom clean-css minifier: foo.css.map 1`] = `"{\\"version\\":3,\\"sources\\":[\\"webpack:///foo.css\\"],\\"names\\":[],\\"mappings\\":\\"AAAA,KACE,MAAO,IAET,EACE,MAAO\\",\\"sourceRoot\\":\\"\\",\\"file\\":\\"foo.css\\"}"`;
162+
163+
exports[`CssMinimizerPlugin should work with custom clean-css minifier: warning 1`] = `Array []`;
164+
165+
exports[`CssMinimizerPlugin should work with custom csso minifier: assets 1`] = `
166+
Object {
167+
"foo.css": "body{color:red}a{color:#00f}
168+
/*# sourceMappingURL=foo.css.map*/",
169+
}
170+
`;
171+
172+
exports[`CssMinimizerPlugin should work with custom csso minifier: error 1`] = `Array []`;
173+
174+
exports[`CssMinimizerPlugin should work with custom csso minifier: foo.css.map 1`] = `"{\\"version\\":3,\\"sources\\":[\\"webpack:///foo.css\\"],\\"names\\":[],\\"mappings\\":\\"AAAA,I,CACE,S,CAEF,C,CACE,U\\",\\"file\\":\\"foo.css\\",\\"sourcesContent\\":[\\"body {\\\\n color: red;\\\\n}\\\\na {\\\\n color: blue;\\\\n}\\\\n\\"],\\"sourceRoot\\":\\"\\"}"`;
175+
176+
exports[`CssMinimizerPlugin should work with custom csso minifier: warning 1`] = `Array []`;
177+
152178
exports[`CssMinimizerPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
153179
Object {
154180
"one.css": ".minify {};",

0 commit comments

Comments
 (0)