Skip to content

Commit 773fbd1

Browse files
refactor: improve perf
1 parent 4d2a8fd commit 773fbd1

File tree

5 files changed

+57
-75
lines changed

5 files changed

+57
-75
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ module.exports = {
294294

295295
#### `Array`
296296

297-
If an array of functions is passed to the `minify` option, the `minimizerOptions` must also be an array.
298297
The function index in the `minify` array corresponds to the options object with the same index in the `minimizerOptions` array.
298+
If you use `minimizerOptions` like object, all `minify` function accept it.
299299

300300
#### `processorOptions`
301301

src/index.js

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -280,57 +280,34 @@ class CssMinimizerPlugin {
280280
input = input.toString();
281281
}
282282

283-
const minifyFns =
284-
typeof this.options.minify === 'function'
285-
? [this.options.minify]
286-
: this.options.minify;
287-
const minimizerOptions = {
283+
const options = {
288284
name,
289285
input,
290286
inputSourceMap,
287+
minify: this.options.minify,
288+
minifyOptions: this.options.minimizerOptions,
291289
};
292290

293-
let warnings = [];
294-
295-
this.options.minimizerOptions = Array.isArray(
296-
this.options.minimizerOptions
297-
)
298-
? this.options.minimizerOptions
299-
: [this.options.minimizerOptions];
300-
301-
for await (const [i, minifyFunc] of minifyFns.entries()) {
302-
minimizerOptions.minify = minifyFunc;
303-
minimizerOptions.minimizerOptions = this.options.minimizerOptions[
304-
i
305-
];
306-
307-
try {
308-
output = await (getWorker
309-
? getWorker().transform(serialize(minimizerOptions))
310-
: minifyFn(minimizerOptions));
311-
} catch (error) {
312-
compilation.errors.push(
313-
CssMinimizerPlugin.buildError(
314-
error,
315-
name,
316-
compilation.requestShortener,
317-
inputSourceMap &&
318-
CssMinimizerPlugin.isSourceMap(inputSourceMap)
319-
? new SourceMapConsumer(inputSourceMap)
320-
: null
321-
)
322-
);
323-
324-
return;
325-
}
291+
try {
292+
output = await (getWorker
293+
? getWorker().transform(serialize(options))
294+
: minifyFn(options));
295+
} catch (error) {
296+
compilation.errors.push(
297+
CssMinimizerPlugin.buildError(
298+
error,
299+
name,
300+
compilation.requestShortener,
301+
inputSourceMap &&
302+
CssMinimizerPlugin.isSourceMap(inputSourceMap)
303+
? new SourceMapConsumer(inputSourceMap)
304+
: null
305+
)
306+
);
326307

327-
minimizerOptions.input = output.code;
328-
minimizerOptions.inputSourceMap = output.map;
329-
warnings = warnings.concat(output.warnings);
308+
return;
330309
}
331310

332-
output.warnings = warnings;
333-
334311
if (output.map) {
335312
output.source = new SourceMapSource(
336313
output.code,

src/minify.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
/*
2-
* We bring to the line here, because when passing result from the worker,
3-
* the warning.toString is replaced with native Object.toString
4-
* */
5-
function warningsToString(warnings) {
6-
return warnings.map((i) => i.toString());
7-
}
8-
91
const minify = async (options) => {
10-
const {
11-
name,
12-
input,
13-
minimizerOptions,
14-
inputSourceMap,
15-
minify: minifyFn,
16-
} = options;
17-
18-
const result = await minifyFn(
19-
{ [name]: input },
20-
inputSourceMap,
21-
minimizerOptions
22-
);
23-
24-
return {
25-
code: result.code,
26-
map: result.map,
27-
warnings: warningsToString(result.warnings || []),
2+
const minifyFns =
3+
typeof options.minify === 'function' ? [options.minify] : options.minify;
4+
5+
const result = {
6+
code: options.input,
7+
map: options.inputSourceMap,
8+
warnings: [],
289
};
10+
11+
for (let i = 0; i <= minifyFns.length - 1; i++) {
12+
const minifyFn = minifyFns[i];
13+
const minifyOptions = Array.isArray(options.minifyOptions)
14+
? options.minifyOptions[i]
15+
: options.minifyOptions;
16+
// eslint-disable-next-line no-await-in-loop
17+
const minifyResult = await minifyFn(
18+
{ [options.name]: result.code },
19+
result.map,
20+
minifyOptions
21+
);
22+
23+
result.code = minifyResult.code;
24+
result.map = minifyResult.map;
25+
result.warnings = result.warnings.concat(minifyResult.warnings || []);
26+
}
27+
28+
if (result.warnings.length > 0) {
29+
result.warnings = result.warnings.map((warning) => warning.toString());
30+
}
31+
32+
return result;
2933
};
3034

3135
async function transform(options) {

test/__snapshots__/minify-option.test.js.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ exports[`"minify" option should work if minify is array && minimizerOptions is a
3333

3434
exports[`"minify" option should work if minify is array && minimizerOptions is object: assets 1`] = `
3535
Object {
36-
"foo.css": "body {
36+
"foo.css": "/*HERE*/.one{background: white;}
37+
body {
3738
font-weight: bold;
3839
}
3940
@@ -45,7 +46,7 @@ body a {
4546
}
4647
4748
.one{color: red;}
48-
.one{background: white;}
49+
/*HERE*/.one{background: white;}
4950
5051
.two{color: red;}
5152

test/minify-option.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ describe('"minify" option', () => {
195195
async (data, inputMap, minimizerOptions) => {
196196
const [input] = Object.values(data);
197197
return {
198-
code: `${input}\n.one{color: red;}\n${minimizerOptions.test}\n`,
198+
code: `${input}\n.one{color: red;}\n/*HERE*/${minimizerOptions.test}\n`,
199199
map: inputMap,
200200
};
201201
},
202-
async (data, inputMap) => {
202+
async (data, inputMap, minimizerOptions) => {
203203
const [input] = Object.values(data);
204204
return {
205-
code: `${input}\n.two{color: red;}\n`,
205+
code: `/*HERE*/${minimizerOptions.test}\n${input}\n.two{color: red;}\n`,
206206
map: inputMap,
207207
};
208208
},

0 commit comments

Comments
 (0)