Skip to content

Commit a08eaf7

Browse files
fix: respect errors and warnings without code
1 parent 30182a3 commit a08eaf7

4 files changed

Lines changed: 106 additions & 67 deletions

File tree

src/index.js

Lines changed: 73 additions & 66 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) {
@@ -615,82 +613,87 @@ class TerserPlugin {
615613
);
616614
}
617615

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-
}
616+
if (output.code) {
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+
}
631630

632-
if (output.extractedComments && output.extractedComments.length > 0) {
633-
const commentsFilename =
634-
/** @type {ExtractCommentsObject} */
635-
(this.options.extractComments).filename ||
636-
"[file].LICENSE.txt[query]";
631+
if (
632+
output.extractedComments &&
633+
output.extractedComments.length > 0
634+
) {
635+
const commentsFilename =
636+
/** @type {ExtractCommentsObject} */
637+
(this.options.extractComments).filename ||
638+
"[file].LICENSE.txt[query]";
637639

638-
let query = "";
639-
let filename = name;
640+
let query = "";
641+
let filename = name;
640642

641-
const querySplit = filename.indexOf("?");
643+
const querySplit = filename.indexOf("?");
642644

643-
if (querySplit >= 0) {
644-
query = filename.slice(querySplit);
645-
filename = filename.slice(0, querySplit);
646-
}
645+
if (querySplit >= 0) {
646+
query = filename.slice(querySplit);
647+
filename = filename.slice(0, querySplit);
648+
}
647649

648-
const lastSlashIndex = filename.lastIndexOf("/");
649-
const basename =
650-
lastSlashIndex === -1
651-
? filename
652-
: filename.slice(lastSlashIndex + 1);
653-
const data = { filename, basename, query };
650+
const lastSlashIndex = filename.lastIndexOf("/");
651+
const basename =
652+
lastSlashIndex === -1
653+
? filename
654+
: filename.slice(lastSlashIndex + 1);
655+
const data = { filename, basename, query };
654656

655-
output.commentsFilename = compilation.getPath(
656-
commentsFilename,
657-
data,
658-
);
657+
output.commentsFilename = compilation.getPath(
658+
commentsFilename,
659+
data,
660+
);
659661

660-
let banner;
662+
let banner;
661663

662-
// Add a banner to the original file
663-
if (
664-
/** @type {ExtractCommentsObject} */
665-
(this.options.extractComments).banner !== false
666-
) {
667-
banner =
664+
// Add a banner to the original file
665+
if (
668666
/** @type {ExtractCommentsObject} */
669-
(this.options.extractComments).banner ||
670-
`For license information please see ${path
671-
.relative(path.dirname(name), output.commentsFilename)
672-
.replace(/\\/g, "/")}`;
673-
674-
if (typeof banner === "function") {
675-
banner = banner(output.commentsFilename);
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+
}
676687
}
677688

678-
if (banner) {
679-
output.source = new ConcatSource(
680-
shebang ? `${shebang}\n` : "",
681-
`/*! ${banner} */\n`,
682-
output.source,
683-
);
684-
}
685-
}
689+
const extractedCommentsString = output.extractedComments
690+
.sort()
691+
.join("\n\n");
686692

687-
const extractedCommentsString = output.extractedComments
688-
.sort()
689-
.join("\n\n");
690-
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__/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: Error: 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: [new Error("error")],
166+
warnings: [new Error("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)