Skip to content

Commit

Permalink
test: custom css minifier
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Aug 25, 2020
1 parent 37d64eb commit ee25238
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ module.exports = {
return {
css: result.css,
map: result.map,
error: result.error,
warnings: result.warnings(),
};
});
Expand Down Expand Up @@ -460,6 +459,38 @@ module.exports = {
};
```

### Using custom css minifier

By default plugin uses [cssnano](https://github.com/cssnano/cssnano) package.
It is possible to use another minify function.

> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
**webpack.config.js**

```js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minify: (data) => {
// eslint-disable-next-line global-require
const csso = require('csso');

const minifiedCss = csso.minify(data.input);

return {
css: minifiedCss.css,
map: minifiedCss.map,
};
},
}),
],
},
};
```

## Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"copy-webpack-plugin": "^6.0.3",
"cross-env": "^7.0.2",
"css-loader": "^4.2.1",
"csso": "^4.0.3",
"del": "^5.1.0",
"del-cli": "^3.0.1",
"eslint": "^7.7.0",
Expand Down
29 changes: 28 additions & 1 deletion test/CssMinimizerPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ describe('CssMinimizerPlugin', () => {
return {
css: result.css,
map: result.map,
error: result.error,
warnings: result.warnings(),
};
});
Expand Down Expand Up @@ -608,4 +607,32 @@ describe('CssMinimizerPlugin', () => {
resolve();
});
});

it('should work with custom css minifier', async () => {
const compiler = getCompiler({
entry: {
foo: `${__dirname}/fixtures/test/foo.css`,
},
});

new CssMinimizerPlugin({
minify: (data) => {
// eslint-disable-next-line global-require
const csso = require('csso');

const minifiedCss = csso.minify(data.input);

return {
css: minifiedCss.css,
map: minifiedCss.map,
};
},
}).apply(compiler);

const stats = await compile(compiler);

expect(readAssets(compiler, stats, '.css')).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('error');
expect(getWarnings(stats)).toMatchSnapshot('warning');
});
});
10 changes: 10 additions & 0 deletions test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ exports[`CssMinimizerPlugin should work with child compilation: errors 1`] = `Ar

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

exports[`CssMinimizerPlugin should work with custom css minifier: assets 1`] = `
Object {
"foo.css": "a{text-align:center}",
}
`;

exports[`CssMinimizerPlugin should work with custom css minifier: error 1`] = `Array []`;

exports[`CssMinimizerPlugin should work with custom css minifier: warning 1`] = `Array []`;

exports[`CssMinimizerPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
Object {
"one.css": ".minify {};",
Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ exports[`CssMinimizerPlugin should work with child compilation: errors 1`] = `Ar

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

exports[`CssMinimizerPlugin should work with custom css minifier: assets 1`] = `
Object {
"foo.css": "a{text-align:center}",
}
`;

exports[`CssMinimizerPlugin should work with custom css minifier: error 1`] = `Array []`;

exports[`CssMinimizerPlugin should work with custom css minifier: warning 1`] = `Array []`;

exports[`CssMinimizerPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
Object {
"one.css": ".minify {};",
Expand Down

0 comments on commit ee25238

Please sign in to comment.