Skip to content

Commit 8607f79

Browse files
fix: respect errors and warnings from minimizer without code
1 parent 30182a3 commit 8607f79

File tree

5 files changed

+136
-85
lines changed

5 files changed

+136
-85
lines changed

src/index.js

Lines changed: 87 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,6 @@ class TerserPlugin {
559559
`${name} from Terser plugin\nMinimizer doesn't return result`,
560560
),
561561
);
562-
563-
return;
564562
}
565563

566564
if (output.warnings && output.warnings.length > 0) {
@@ -598,99 +596,104 @@ class TerserPlugin {
598596
);
599597
}
600598

601-
let shebang;
602-
603-
if (
604-
/** @type {ExtractCommentsObject} */
605-
(this.options.extractComments).banner !== false &&
606-
output.extractedComments &&
607-
output.extractedComments.length > 0 &&
608-
output.code.startsWith("#!")
609-
) {
610-
const firstNewlinePosition = output.code.indexOf("\n");
611-
612-
shebang = output.code.slice(0, Math.max(0, firstNewlinePosition));
613-
output.code = output.code.slice(
614-
Math.max(0, firstNewlinePosition + 1),
615-
);
616-
}
599+
if (output.code) {
600+
let shebang;
617601

618-
if (output.map) {
619-
output.source = new SourceMapSource(
620-
output.code,
621-
name,
622-
output.map,
623-
input,
624-
/** @type {RawSourceMap} */
625-
(inputSourceMap),
626-
true,
627-
);
628-
} else {
629-
output.source = new RawSource(output.code);
630-
}
631-
632-
if (output.extractedComments && output.extractedComments.length > 0) {
633-
const commentsFilename =
602+
if (
634603
/** @type {ExtractCommentsObject} */
635-
(this.options.extractComments).filename ||
636-
"[file].LICENSE.txt[query]";
637-
638-
let query = "";
639-
let filename = name;
640-
641-
const querySplit = filename.indexOf("?");
604+
(this.options.extractComments).banner !== false &&
605+
output.extractedComments &&
606+
output.extractedComments.length > 0 &&
607+
output.code.startsWith("#!")
608+
) {
609+
const firstNewlinePosition = output.code.indexOf("\n");
642610

643-
if (querySplit >= 0) {
644-
query = filename.slice(querySplit);
645-
filename = filename.slice(0, querySplit);
611+
shebang = output.code.slice(0, Math.max(0, firstNewlinePosition));
612+
output.code = output.code.slice(
613+
Math.max(0, firstNewlinePosition + 1),
614+
);
646615
}
647616

648-
const lastSlashIndex = filename.lastIndexOf("/");
649-
const basename =
650-
lastSlashIndex === -1
651-
? filename
652-
: filename.slice(lastSlashIndex + 1);
653-
const data = { filename, basename, query };
654-
655-
output.commentsFilename = compilation.getPath(
656-
commentsFilename,
657-
data,
658-
);
659-
660-
let banner;
617+
if (output.map) {
618+
output.source = new SourceMapSource(
619+
output.code,
620+
name,
621+
output.map,
622+
input,
623+
/** @type {RawSourceMap} */
624+
(inputSourceMap),
625+
true,
626+
);
627+
} else {
628+
output.source = new RawSource(output.code);
629+
}
661630

662-
// Add a banner to the original file
663631
if (
664-
/** @type {ExtractCommentsObject} */
665-
(this.options.extractComments).banner !== false
632+
output.extractedComments &&
633+
output.extractedComments.length > 0
666634
) {
667-
banner =
635+
const commentsFilename =
668636
/** @type {ExtractCommentsObject} */
669-
(this.options.extractComments).banner ||
670-
`For license information please see ${path
671-
.relative(path.dirname(name), output.commentsFilename)
672-
.replace(/\\/g, "/")}`;
637+
(this.options.extractComments).filename ||
638+
"[file].LICENSE.txt[query]";
673639

674-
if (typeof banner === "function") {
675-
banner = banner(output.commentsFilename);
640+
let query = "";
641+
let filename = name;
642+
643+
const querySplit = filename.indexOf("?");
644+
645+
if (querySplit >= 0) {
646+
query = filename.slice(querySplit);
647+
filename = filename.slice(0, querySplit);
676648
}
677649

678-
if (banner) {
679-
output.source = new ConcatSource(
680-
shebang ? `${shebang}\n` : "",
681-
`/*! ${banner} */\n`,
682-
output.source,
683-
);
650+
const lastSlashIndex = filename.lastIndexOf("/");
651+
const basename =
652+
lastSlashIndex === -1
653+
? filename
654+
: filename.slice(lastSlashIndex + 1);
655+
const data = { filename, basename, query };
656+
657+
output.commentsFilename = compilation.getPath(
658+
commentsFilename,
659+
data,
660+
);
661+
662+
let banner;
663+
664+
// Add a banner to the original file
665+
if (
666+
/** @type {ExtractCommentsObject} */
667+
(this.options.extractComments).banner !== false
668+
) {
669+
banner =
670+
/** @type {ExtractCommentsObject} */
671+
(this.options.extractComments).banner ||
672+
`For license information please see ${path
673+
.relative(path.dirname(name), output.commentsFilename)
674+
.replace(/\\/g, "/")}`;
675+
676+
if (typeof banner === "function") {
677+
banner = banner(output.commentsFilename);
678+
}
679+
680+
if (banner) {
681+
output.source = new ConcatSource(
682+
shebang ? `${shebang}\n` : "",
683+
`/*! ${banner} */\n`,
684+
output.source,
685+
);
686+
}
684687
}
685-
}
686688

687-
const extractedCommentsString = output.extractedComments
688-
.sort()
689-
.join("\n\n");
689+
const extractedCommentsString = output.extractedComments
690+
.sort()
691+
.join("\n\n");
690692

691-
output.extractedCommentsSource = new RawSource(
692-
`${extractedCommentsString}\n`,
693-
);
693+
output.extractedCommentsSource = new RawSource(
694+
`${extractedCommentsString}\n`,
695+
);
696+
}
694697
}
695698

696699
await cacheItem.storePromise({
@@ -714,6 +717,10 @@ class TerserPlugin {
714717
}
715718
}
716719

720+
if (!output.source) {
721+
return;
722+
}
723+
717724
/** @type {AssetInfo} */
718725
const newInfo = { minimized: true };
719726
const { source, extractedCommentsSource } = output;

src/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,6 @@ async function uglifyJsMinify(
523523

524524
return {
525525
code: result.code,
526-
527526
map: result.map ? JSON.parse(result.map) : undefined,
528527
errors: result.error ? [result.error] : [],
529528
warnings: result.warnings || [],

test/__snapshots__/TerserPlugin.test.js.snap

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3377,8 +3377,14 @@ exports[`TerserPlugin should work, extract comments in one file and use memory c
33773377
33783378
exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: assets 1`] = `
33793379
Object {
3380-
"one.js": "",
3381-
"two.js": "",
3380+
"one.js": "/******/ (() => { // webpackBootstrap
3381+
3382+
/******/ })()
3383+
;",
3384+
"two.js": "/******/ (() => { // webpackBootstrap
3385+
3386+
/******/ })()
3387+
;",
33823388
}
33833389
`;
33843390
@@ -3400,8 +3406,14 @@ exports[`TerserPlugin should write stdout and stderr of workers to stdout and st
34003406
34013407
exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
34023408
Object {
3403-
"one.js": "",
3404-
"two.js": "",
3409+
"one.js": "/******/ (() => { // webpackBootstrap
3410+
3411+
/******/ })()
3412+
;",
3413+
"two.js": "/******/ (() => { // webpackBootstrap
3414+
3415+
/******/ })()
3416+
;",
34053417
}
34063418
`;
34073419

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`minify option should output errors and warning: errors 1`] = `
4+
Array [
5+
"Error: main.js from Terser plugin
6+
Minimizer doesn't return result",
7+
"Error: main.js from Terser plugin
8+
error",
9+
]
10+
`;
11+
12+
exports[`minify option should output errors and warning: warnings 1`] = `
13+
Array [
14+
"Warning: warning",
15+
]
16+
`;
17+
318
exports[`minify option should snapshot with extracting comments: assets 1`] = `
419
Object {
520
"main.js": "/*! For license information please see main.js.LICENSE.txt */
@@ -400,6 +415,8 @@ exports[`minify option should work using when the \`minify\` option is \`uglifyJ
400415
Array [
401416
"Error: broken.js from Terser plugin
402417
Minimizer doesn't return result",
418+
"Error: broken.js from Terser plugin
419+
Unterminated template literal [broken.js:1,undefined]",
403420
]
404421
`;
405422

test/minify-option.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,22 @@ describe("minify option", () => {
157157
expect(getWarnings(stats)).toMatchSnapshot("warnings");
158158
});
159159

160+
it("should output errors and warning", async () => {
161+
const compiler = getCompiler();
162+
163+
new TerserPlugin({
164+
minify: () => ({
165+
errors: ["error"],
166+
warnings: ["warning"],
167+
}),
168+
}).apply(compiler);
169+
170+
const stats = await compile(compiler);
171+
172+
expect(getErrors(stats)).toMatchSnapshot("errors");
173+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
174+
});
175+
160176
it("should snapshot with extracting comments", async () => {
161177
const compiler = getCompiler({
162178
entry: path.resolve(__dirname, "./fixtures/minify/es5.js"),

0 commit comments

Comments
 (0)