Skip to content

Feat: added defaults functions for clean-css and csso #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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