Skip to content

Commit 933fb49

Browse files
fix: respect errors and warnings from minimizer without code
1 parent 95e5fa1 commit 933fb49

File tree

5 files changed

+105
-17
lines changed

5 files changed

+105
-17
lines changed

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ class CssMinimizerPlugin {
622622
innerSourceMap,
623623
true,
624624
);
625-
} else {
625+
} else if (item.code) {
626626
output.source = new RawSource(item.code);
627627
}
628628
}
@@ -632,7 +632,7 @@ class CssMinimizerPlugin {
632632
inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);
633633

634634
for (const error of result.errors) {
635-
output.warnings.push(
635+
output.errors.push(
636636
CssMinimizerPlugin.buildError(
637637
error,
638638
name,
@@ -691,6 +691,10 @@ class CssMinimizerPlugin {
691691
}
692692
}
693693

694+
if (!output.source) {
695+
return;
696+
}
697+
694698
const newInfo = { minimized: true };
695699
const { source } = output;
696700

src/minify.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ async function minify(options) {
3434
);
3535

3636
if (typeof minifyResult.code !== "string") {
37-
throw new Error(
38-
"minimizer function doesn't return the 'code' property or result is not a string value",
37+
result.errors.push(
38+
new Error(
39+
"minimizer function doesn't return the 'code' property or result is not a string value",
40+
),
3941
);
4042
}
4143

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,58 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3+
exports[`"minify" option should work and allow to return errors and warnings from custom function without code: assets 1`] = `
4+
{
5+
"foo.css": "body {
6+
font-weight: bold;
7+
}
8+
9+
body {
10+
color: red;
11+
}
12+
body a {
13+
text-align: center;
14+
}
15+
",
16+
}
17+
`;
18+
19+
exports[`"minify" option should work and allow to return errors and warnings from custom function without code: error 1`] = `
20+
[
21+
"Error: foo.css from Css Minimizer plugin
22+
Error 1",
23+
"Error: foo.css from Css Minimizer plugin
24+
Error: Error 2",
25+
"Error: foo.css from Css Minimizer plugin
26+
Error: minimizer function doesn't return the 'code' property or result is not a string value",
27+
]
28+
`;
29+
30+
exports[`"minify" option should work and allow to return errors and warnings from custom function without code: warning 1`] = `
31+
[
32+
"Warning: foo.css from Css Minimizer plugin
33+
Warning 1",
34+
"Warning: foo.css from Css Minimizer plugin
35+
Warning 2",
36+
]
37+
`;
38+
339
exports[`"minify" option should work and allow to return errors and warnings from custom function: assets 1`] = `
440
{
541
"foo.css": ".test { color: red; }",
642
}
743
`;
844

9-
exports[`"minify" option should work and allow to return errors and warnings from custom function: error 1`] = `[]`;
10-
11-
exports[`"minify" option should work and allow to return errors and warnings from custom function: warning 1`] = `
45+
exports[`"minify" option should work and allow to return errors and warnings from custom function: error 1`] = `
1246
[
1347
"Error: foo.css from Css Minimizer plugin
1448
Error 1",
1549
"Error: foo.css from Css Minimizer plugin
1650
Error: Error 2",
51+
]
52+
`;
53+
54+
exports[`"minify" option should work and allow to return errors and warnings from custom function: warning 1`] = `
55+
[
1756
"Warning: foo.css from Css Minimizer plugin
1857
Warning 1",
1958
"Warning: foo.css from Css Minimizer plugin
@@ -471,7 +510,15 @@ exports[`"minify" option should work with "csso" minifier: warning 1`] = `[]`;
471510

472511
exports[`"minify" option should work with empty code: assets 1`] = `
473512
{
474-
"foo.css": "",
513+
"foo.css": "body {
514+
color: red;
515+
}
516+
a {
517+
color: blue;
518+
}
519+
520+
/*# sourceMappingURL=foo.css.map*/",
521+
"foo.css.map": "{"version":3,"file":"foo.css","mappings":"AAAA;EACE,UAAU;AACZ;AACA;EACE,WAAW;AACb,C","sources":["webpack:///./foo.css"],"sourcesContent":["body {\\n color: red;\\n}\\na {\\n color: blue;\\n}"],"names":[],"sourceRoot":""}",
475522
}
476523
`;
477524

test/minify-option.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,4 +1293,39 @@ describe('"minify" option', () => {
12931293
expect(getErrors(stats)).toMatchSnapshot("error");
12941294
expect(getWarnings(stats)).toMatchSnapshot("warning");
12951295
});
1296+
1297+
it("should work and allow to return errors and warnings from custom function without code", async () => {
1298+
const compiler = getCompiler({
1299+
entry: {
1300+
foo: path.join(__dirname, "fixtures", "sourcemap", "foo.scss"),
1301+
},
1302+
module: {
1303+
rules: [
1304+
{
1305+
test: /.s?css$/i,
1306+
use: [
1307+
MiniCssExtractPlugin.loader,
1308+
{ loader: "css-loader", options: { sourceMap: true } },
1309+
{ loader: "sass-loader", options: { sourceMap: true } },
1310+
],
1311+
},
1312+
],
1313+
},
1314+
});
1315+
1316+
new CssMinimizerPlugin({
1317+
minify: async () => ({
1318+
warnings: ["Warning 1", new Error("Warning 2")],
1319+
errors: ["Error 1", new Error("Error 2")],
1320+
}),
1321+
}).apply(compiler);
1322+
1323+
const stats = await compile(compiler);
1324+
1325+
expect(readAssets(compiler, stats, /\.css(\.map)?$/)).toMatchSnapshot(
1326+
"assets",
1327+
);
1328+
expect(getErrors(stats)).toMatchSnapshot("error");
1329+
expect(getWarnings(stats)).toMatchSnapshot("warning");
1330+
});
12961331
});

0 commit comments

Comments
 (0)