Skip to content

Commit 8865423

Browse files
feat: added support processorOptions for cssnano
1 parent 53cba69 commit 8865423

11 files changed

+1458
-1188
lines changed

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,53 @@ module.exports = {
380380
};
381381
```
382382

383+
#### `processorOptions`
384+
385+
Type: `Object`
386+
Default: `{ to: assetName, from: assetName }`
387+
388+
Allows to specify options [`processoptions`](https://postcss.org/api/#processoptions) for the cssnano.
389+
The `parser`,` stringifier` and `syntax` can be either a function or a string indicating the module that will be imported.
390+
391+
> ⚠️ **If a function is passed, the `parallel` option must be disabled.**.
392+
393+
```js
394+
import sugarss from 'sugarss';
395+
396+
module.exports = {
397+
optimization: {
398+
minimize: true,
399+
minimizer: [
400+
new CssMinimizerPlugin({
401+
parallel: false,
402+
minimizerOptions: {
403+
processorOptions: {
404+
parser: sugarss,
405+
},
406+
},
407+
}),
408+
],
409+
},
410+
};
411+
```
412+
413+
```js
414+
module.exports = {
415+
optimization: {
416+
minimize: true,
417+
minimizer: [
418+
new CssMinimizerPlugin({
419+
minimizerOptions: {
420+
processorOptions: {
421+
parser: 'sugarss',
422+
},
423+
},
424+
}),
425+
],
426+
},
427+
};
428+
```
429+
383430
### `warningsFilter`
384431

385432
Type: `Function<(warning, file, source) -> Boolean>`

package-lock.json

+1,094-1,183
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"prettier": "^2.1.2",
8282
"sass-loader": "^10.0.2",
8383
"standard-version": "^9.0.0",
84+
"sugarss": "^3.0.3",
8485
"webpack": "^4.45.0"
8586
},
8687
"keywords": [

src/minify.js

+64-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const cssnano = require('cssnano');
2-
32
/*
43
* We bring to the line here, because when passing result from the worker,
54
* the warning.toString is replaced with native Object.toString
@@ -8,6 +7,34 @@ function warningsToString(warnings) {
87
return warnings.map((i) => i.toString());
98
}
109

10+
async function load(module) {
11+
let exports;
12+
13+
try {
14+
// eslint-disable-next-line import/no-dynamic-require, global-require
15+
exports = require(module);
16+
17+
return exports;
18+
} catch (requireError) {
19+
let importESM;
20+
21+
try {
22+
// eslint-disable-next-line no-new-func
23+
importESM = new Function('id', 'return import(id);');
24+
} catch (e) {
25+
importESM = null;
26+
}
27+
28+
if (requireError.code === 'ERR_REQUIRE_ESM' && importESM) {
29+
exports = await importESM(module);
30+
31+
return exports.default;
32+
}
33+
34+
throw requireError;
35+
}
36+
}
37+
1138
const minify = async (options) => {
1239
const {
1340
name,
@@ -18,8 +45,6 @@ const minify = async (options) => {
1845
minify: minifyFn,
1946
} = options;
2047

21-
const postcssOptions = { to: name, from: name };
22-
2348
if (minifyFn) {
2449
const result = await minifyFn(
2550
{ [name]: input },
@@ -35,6 +60,42 @@ const minify = async (options) => {
3560
};
3661
}
3762

63+
const postcssOptions = {
64+
to: name,
65+
from: name,
66+
...minimizerOptions.processorOptions,
67+
};
68+
69+
if (typeof postcssOptions.parser === 'string') {
70+
try {
71+
postcssOptions.parser = await load(postcssOptions.parser);
72+
} catch (error) {
73+
throw new Error(
74+
`Loading PostCSS "${postcssOptions.parser}" parser failed: ${error.message}\n\n(@${name})`
75+
);
76+
}
77+
}
78+
79+
if (typeof postcssOptions.stringifier === 'string') {
80+
try {
81+
postcssOptions.stringifier = await load(postcssOptions.stringifier);
82+
} catch (error) {
83+
throw new Error(
84+
`Loading PostCSS "${postcssOptions.stringifier}" stringifier failed: ${error.message}\n\n(@${name})`
85+
);
86+
}
87+
}
88+
89+
if (typeof postcssOptions.syntax === 'string') {
90+
try {
91+
postcssOptions.syntax = await load(postcssOptions.syntax);
92+
} catch (error) {
93+
throw new Error(
94+
`Loading PostCSS "${postcssOptions.syntax}" syntax failed: ${error.message}\n\n(@${name})`
95+
);
96+
}
97+
}
98+
3899
if (inputSourceMap) {
39100
// TODO remove `inline` value for the `sourceMap` option
40101
postcssOptions.map = {
@@ -66,7 +127,6 @@ async function transform(options) {
66127
'__dirname',
67128
`'use strict'\nreturn ${options}`
68129
)(exports, require, module, __filename, __dirname);
69-
70130
const result = await minify(options);
71131

72132
if (result.error) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ exports[`CssMinimizerPlugin should work and do not use memory cache when the "ca
104104

105105
exports[`CssMinimizerPlugin should work and generate real content hash: assets 1`] = `
106106
Object {
107-
"entry.19e4764f9c1d9fe130e2.01741fc98b90f2f0954e.635720ae0ca713ce150b.css": "body{color:red}a{color:#00f}",
107+
"entry.19e4764f9c1d9fe130e2.c5d73cda61db28e71841.6a2aa6642f0109a40c41.css": "body{color:red}a{color:#00f}",
108108
}
109109
`;
110110

test/__snapshots__/cssMinimizerOptions-option.test.js.snap.webpack4

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ exports[`when applied with "minimizerOptions" option matches snapshot for "disca
2020
exports[`when applied with "minimizerOptions" option matches snapshot for "mergeRules" option (disable): entry.css 1`] = `"body{color:red}body{font-weight:700}"`;
2121

2222
exports[`when applied with "minimizerOptions" option matches snapshot for "mergeRules" option (enable [default]): entry.css 1`] = `"body{color:red;font-weight:700}"`;
23+
24+
exports[`when applied with "minimizerOptions" option matches snapshot for "parser" option with "Function" value: index.sss 1`] = `"a{color:#000}"`;
25+
26+
exports[`when applied with "minimizerOptions" option matches snapshot for "parser" option with "String" value: index.sss 1`] = `"a{color:#000}"`;
27+
28+
exports[`when applied with "minimizerOptions" option matches snapshot for "stringifier" option with "Function" value: entry.css 1`] = `"body color:reda color:#00f"`;
29+
30+
exports[`when applied with "minimizerOptions" option matches snapshot for "stringifier" option with "String" value: entry.css 1`] = `"body color:reda color:#00f"`;
31+
32+
exports[`when applied with "minimizerOptions" option matches snapshot for "syntax" option with "Function" value: index.sss 1`] = `"a color:#000"`;
33+
34+
exports[`when applied with "minimizerOptions" option matches snapshot for "syntax" option with "String" value: index.sss 1`] = `"a color:#000"`;

test/__snapshots__/cssMinimizerOptions-option.test.js.snap.webpack5

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ exports[`when applied with "minimizerOptions" option matches snapshot for "disca
2020
exports[`when applied with "minimizerOptions" option matches snapshot for "mergeRules" option (disable): entry.css 1`] = `"body{color:red}body{font-weight:700}"`;
2121

2222
exports[`when applied with "minimizerOptions" option matches snapshot for "mergeRules" option (enable [default]): entry.css 1`] = `"body{color:red;font-weight:700}"`;
23+
24+
exports[`when applied with "minimizerOptions" option matches snapshot for "parser" option with "Function" value: index.sss 1`] = `"a{color:#000}"`;
25+
26+
exports[`when applied with "minimizerOptions" option matches snapshot for "parser" option with "String" value: index.sss 1`] = `"a{color:#000}"`;
27+
28+
exports[`when applied with "minimizerOptions" option matches snapshot for "stringifier" option with "Function" value: entry.css 1`] = `"body color:reda color:#00f"`;
29+
30+
exports[`when applied with "minimizerOptions" option matches snapshot for "stringifier" option with "String" value: entry.css 1`] = `"body color:reda color:#00f"`;
31+
32+
exports[`when applied with "minimizerOptions" option matches snapshot for "syntax" option with "Function" value: index.sss 1`] = `"a color:#000"`;
33+
34+
exports[`when applied with "minimizerOptions" option matches snapshot for "syntax" option with "String" value: index.sss 1`] = `"a color:#000"`;

0 commit comments

Comments
 (0)