Skip to content

Commit

Permalink
feat: added defaults functions for clean-css and csso
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Mar 23, 2021
1 parent 8ebb052 commit 5211eed
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 2,231 deletions.
76 changes: 46 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,19 @@ module.exports = {
### `minify`

Type: `Function|Array<Function>`
Default: `undefined`
Default: `CssMinimizerPlugin.cssnanoMinify`

Allows you to override default minify function.
Allows to override default minify function.
By default plugin uses [cssnano](https://github.com/cssnano/cssnano) package.
Useful for using and testing unpublished versions or forks.

Possible options:

- CssMinimizerPlugin.cssnanoMinify
- CssMinimizerPlugin.cssoMinify
- CssMinimizerPlugin.cleanCssMinify
- async (data, inputMap, minimizerOptions) => {return {code: `a{color: red}`, map: `...`, warnings: []}}

> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
#### `Function`
Expand All @@ -206,36 +213,14 @@ module.exports = {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minify: (data, inputMap, minimizerOptions) => {
const postcss = require('postcss');

const plugin = postcss.plugin(
'custom-plugin',
() => (css, result) => {
// custom code
}
);

const [[filename, input]] = Object.entries(data);

const postcssOptions = {
from: filename,
to: filename,
map: {
prev: inputMap,
minimizerOptions: {
level: {
1: {
roundingPrecision: 'all=3,px=5',
},
};

return postcss([plugin])
.process(input, postcssOptions)
.then((result) => {
return {
code: result.css,
map: result.map,
warnings: result.warnings(),
};
});
},
},
minify: CssMinimizerPlugin.cleanCssMinify,
}),
],
},
Expand All @@ -247,6 +232,37 @@ module.exports = {
If an array of functions is passed to the `minify` option, the `minimizerOptions` must also be an array.
The function index in the `minify` array corresponds to the options object with the same index in the `minimizerOptions` array.

**webpack.config.js**

```js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: [
{}, // Options for the first function (CssMinimizerPlugin.cssnanoMinify)
{}, // Options for the second function (CssMinimizerPlugin.cleanCssMinify)
{}, // Options for the third function
],
minify: [
CssMinimizerPlugin.cssnanoMinify,
CssMinimizerPlugin.cleanCssMinify,
async (data, inputMap, minimizerOptions) => {
// To do something
return {
code: `a{color: red}`,
map: `{"version": "3", ...}`,
warnings: [],
};
},
],
}),
],
},
};
```

### `minimizerOptions`

Type: `Object|Array<Object>`
Expand Down
Loading

0 comments on commit 5211eed

Please sign in to comment.