Skip to content

Commit 1aa2a80

Browse files
fix: respect errors and warnings from minimizer without code
1 parent 06c6848 commit 1aa2a80

File tree

5 files changed

+87
-35
lines changed

5 files changed

+87
-35
lines changed

package-lock.json

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

src/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,12 @@ class HtmlMinimizerPlugin {
427427
}
428428

429429
output = { warnings: [], errors: [] };
430-
output.source = new RawSource(
431-
result.outputs[result.outputs.length - 1].code,
432-
);
430+
431+
if (result.outputs.length > 0) {
432+
output.source = new RawSource(
433+
result.outputs[result.outputs.length - 1].code,
434+
);
435+
}
433436

434437
for (const error of result.errors) {
435438
output.errors.push(HtmlMinimizerPlugin.buildError(error, name));
@@ -448,8 +451,6 @@ class HtmlMinimizerPlugin {
448451
});
449452
}
450453

451-
const newInfo = { minimized: true };
452-
453454
if (output.warnings && output.warnings.length > 0) {
454455
for (const warning of output.warnings) {
455456
compilation.warnings.push(
@@ -468,6 +469,12 @@ class HtmlMinimizerPlugin {
468469
}
469470
}
470471

472+
if (!output.source) {
473+
return;
474+
}
475+
476+
const newInfo = { minimized: true };
477+
471478
compilation.updateAsset(name, output.source, newInfo);
472479
});
473480
}

src/minify.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,26 @@ async function minify(options) {
3131
minifyOptions,
3232
);
3333

34-
if (
35-
typeof minifyResult !== "string" &&
36-
typeof minifyResult.code !== "string"
37-
) {
38-
throw new Error(
39-
"minimizer function doesn't return the 'code' property or result is not a string value",
40-
);
41-
}
42-
4334
if (typeof minifyResult === "string") {
4435
result.outputs.push({ code: minifyResult });
4536
} else {
37+
if (typeof minifyResult.code === "string") {
38+
result.outputs.push({ code: minifyResult.code });
39+
}
40+
4641
if (minifyResult.errors) {
4742
result.errors = [...result.errors, ...minifyResult.errors];
4843
}
4944

5045
if (minifyResult.warnings) {
5146
result.warnings = [...result.warnings, ...minifyResult.warnings];
5247
}
53-
54-
result.outputs.push({ code: minifyResult.code });
5548
}
5649
}
5750

58-
result.outputs = [result.outputs[result.outputs.length - 1]];
51+
if (typeof result.outputs[result.outputs.length - 1] !== "undefined") {
52+
result.outputs = [result.outputs[result.outputs.length - 1]];
53+
}
5954

6055
return result;
6156
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ simple.html from Html Minimizer plugin",
1313

1414
exports[`"minify" option should emit error: warnings 1`] = `[]`;
1515

16+
exports[`"minify" option should emit errors and warnings without code: errors 1`] = `
17+
[
18+
"Error: simple.html from Html Minimizer plugin
19+
simple.html from Html Minimizer plugin",
20+
"Error: simple.html from Html Minimizer plugin
21+
simple.html from Html Minimizer plugin",
22+
"Error: simple.html from Html Minimizer plugin
23+
simple.html from Html Minimizer plugin",
24+
]
25+
`;
26+
27+
exports[`"minify" option should emit errors and warnings without code: warnings 1`] = `
28+
[
29+
"Warning: object error",
30+
"Warning: string error",
31+
"Warning: test error",
32+
]
33+
`;
34+
1635
exports[`"minify" option should minimize code and emit warning: assets 1`] = `
1736
{
1837
"simple.html": "<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> <h2>An Unordered HTML List</h2> <ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul> <h2>An Ordered HTML List</h2> <ol> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ol> </body> </html> ",

test/minify-option.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,37 @@ describe('"minify" option', () => {
191191
expect(getWarnings(stats)).toMatchSnapshot("warnings");
192192
});
193193

194+
it("should emit errors and warnings without code", async () => {
195+
const testHtmlId = "./simple.html";
196+
const compiler = getCompiler(testHtmlId);
197+
198+
new HtmlMinimizerPlugin({
199+
minify: [
200+
async (_data) => ({
201+
warnings: [
202+
"string error",
203+
new Error("test error"),
204+
{
205+
message: "object error",
206+
},
207+
],
208+
errors: [
209+
"string error",
210+
new Error("test error"),
211+
{
212+
message: "object error",
213+
},
214+
],
215+
}),
216+
],
217+
}).apply(compiler);
218+
219+
const stats = await compile(compiler);
220+
221+
expect(getErrors(stats)).toMatchSnapshot("errors");
222+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
223+
});
224+
194225
it("should work with 'swcMinify'", async () => {
195226
const testHtmlId = "./simple.html";
196227
const compiler = getCompiler(testHtmlId);

0 commit comments

Comments
 (0)