Skip to content

Commit 2e54869

Browse files
fix: esbuild warning output (#550)
1 parent 723ed45 commit 2e54869

File tree

4 files changed

+69
-32
lines changed

4 files changed

+69
-32
lines changed

src/utils.js

+31-31
Original file line numberDiff line numberDiff line change
@@ -703,37 +703,37 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) {
703703
warnings:
704704
result.warnings.length > 0
705705
? result.warnings.map((item) => {
706-
return {
707-
name: "Warning",
708-
source: item.location && item.location.file,
709-
line: item.location && item.location.line,
710-
column: item.location && item.location.column,
711-
plugin: item.pluginName,
712-
message: `${item.text}${
713-
item.detail ? `\nDetails:\n${item.detail}` : ""
714-
}${
715-
item.notes.length > 0
716-
? `\n\nNotes:\n${item.notes
717-
.map(
718-
(note) =>
719-
`${
720-
note.location
721-
? `[${note.location.file}:${note.location.line}:${note.location.column}] `
722-
: ""
723-
}${note.text}${
724-
note.location
725-
? `\nSuggestion: ${note.location.suggestion}`
726-
: ""
727-
}${
728-
note.location
729-
? `\nLine text:\n${note.location.lineText}\n`
730-
: ""
731-
}`
732-
)
733-
.join("\n")}`
734-
: ""
735-
}`,
736-
};
706+
const plugin = item.pluginName
707+
? `\nPlugin Name: ${item.pluginName}`
708+
: "";
709+
const location = item.location
710+
? `\n\n${item.location.file}:${item.location.line}:${item.location.column}:\n ${item.location.line} | ${item.location.lineText}\n\nSuggestion: ${item.location.suggestion}`
711+
: "";
712+
const notes =
713+
item.notes.length > 0
714+
? `\n\nNotes:\n${item.notes
715+
.map(
716+
(note) =>
717+
`${
718+
note.location
719+
? `[${note.location.file}:${note.location.line}:${note.location.column}] `
720+
: ""
721+
}${note.text}${
722+
note.location
723+
? `\nSuggestion: ${note.location.suggestion}`
724+
: ""
725+
}${
726+
note.location
727+
? `\nLine text:\n${note.location.lineText}\n`
728+
: ""
729+
}`
730+
)
731+
.join("\n")}`
732+
: "";
733+
734+
return `${item.text} [${item.id}]${plugin}${location}${
735+
item.detail ? `\nDetails:\n${item.detail}` : ""
736+
}${notes}`;
737737
})
738738
: [],
739739
};

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

+16
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ Transform failed with 1 error:",
106106
107107
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and output errors: warnings 1`] = `Array []`;
108108
109+
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and output well formatted warnings: assets 1`] = `
110+
Object {
111+
"main.js": "(()=>{var o={};!new Array instanceof FormData&&console.log(\\"error in form\\")})();
112+
",
113+
}
114+
`;
115+
116+
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and output well formatted warnings: errors 1`] = `Array []`;
117+
118+
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and output well formatted warnings: warnings 1`] = `
119+
Array [
120+
"Warning: Suspicious use of the \\"!\\" operator inside the \\"instanceof\\" operator [suspicious-boolean-not]
121+
",
122+
]
123+
`;
124+
109125
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\`: assets 1`] = `
110126
Object {
111127
"main.js": "(()=>{var a={791:e=>{/* @preserve*/e.exports=function(){console.log(7)}}},r={};function c(e){var o=r[e];if(o!==void 0)return o.exports;var _=r[e]={exports:{}};return a[e](_,_.exports,c),_.exports}var n=c(791)})();

test/fixtures/warning.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const form = new Array;
2+
3+
if (!form instanceof FormData) {
4+
console.log("error in form")
5+
}

test/minify-option.test.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,23 @@ describe("minify option", () => {
824824
const stats = await compile(compiler);
825825

826826
expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
827-
expect(stats.compilation.errors).toMatchSnapshot("errors");
827+
expect(getErrors(stats)).toMatchSnapshot("errors");
828+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
829+
});
830+
831+
it("should work using when the `minify` option is `esbuildMinify` and output well formatted warnings", async () => {
832+
const compiler = getCompiler({
833+
entry: path.resolve(__dirname, "./fixtures/warning.js"),
834+
});
835+
836+
new TerserPlugin({
837+
minify: TerserPlugin.esbuildMinify,
838+
}).apply(compiler);
839+
840+
const stats = await compile(compiler);
841+
842+
expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
843+
expect(getErrors(stats)).toMatchSnapshot("errors");
828844
expect(getWarnings(stats)).toMatchSnapshot("warnings");
829845
});
830846
});

0 commit comments

Comments
 (0)