Skip to content

Commit

Permalink
fix: esbuild warning output (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Mar 8, 2023
1 parent 723ed45 commit 2e54869
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
62 changes: 31 additions & 31 deletions src/utils.js
Expand Up @@ -703,37 +703,37 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) {
warnings:
result.warnings.length > 0
? result.warnings.map((item) => {
return {
name: "Warning",
source: item.location && item.location.file,
line: item.location && item.location.line,
column: item.location && item.location.column,
plugin: item.pluginName,
message: `${item.text}${
item.detail ? `\nDetails:\n${item.detail}` : ""
}${
item.notes.length > 0
? `\n\nNotes:\n${item.notes
.map(
(note) =>
`${
note.location
? `[${note.location.file}:${note.location.line}:${note.location.column}] `
: ""
}${note.text}${
note.location
? `\nSuggestion: ${note.location.suggestion}`
: ""
}${
note.location
? `\nLine text:\n${note.location.lineText}\n`
: ""
}`
)
.join("\n")}`
: ""
}`,
};
const plugin = item.pluginName
? `\nPlugin Name: ${item.pluginName}`
: "";
const location = item.location
? `\n\n${item.location.file}:${item.location.line}:${item.location.column}:\n ${item.location.line} | ${item.location.lineText}\n\nSuggestion: ${item.location.suggestion}`
: "";
const notes =
item.notes.length > 0
? `\n\nNotes:\n${item.notes
.map(
(note) =>
`${
note.location
? `[${note.location.file}:${note.location.line}:${note.location.column}] `
: ""
}${note.text}${
note.location
? `\nSuggestion: ${note.location.suggestion}`
: ""
}${
note.location
? `\nLine text:\n${note.location.lineText}\n`
: ""
}`
)
.join("\n")}`
: "";

return `${item.text} [${item.id}]${plugin}${location}${
item.detail ? `\nDetails:\n${item.detail}` : ""
}${notes}`;
})
: [],
};
Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/minify-option.test.js.snap
Expand Up @@ -106,6 +106,22 @@ Transform failed with 1 error:",
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and output errors: warnings 1`] = `Array []`;
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and output well formatted warnings: assets 1`] = `
Object {
"main.js": "(()=>{var o={};!new Array instanceof FormData&&console.log(\\"error in form\\")})();
",
}
`;
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and output well formatted warnings: errors 1`] = `Array []`;
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and output well formatted warnings: warnings 1`] = `
Array [
"Warning: Suspicious use of the \\"!\\" operator inside the \\"instanceof\\" operator [suspicious-boolean-not]
",
]
`;
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\`: assets 1`] = `
Object {
"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)})();
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/warning.js
@@ -0,0 +1,5 @@
const form = new Array;

if (!form instanceof FormData) {
console.log("error in form")
}
18 changes: 17 additions & 1 deletion test/minify-option.test.js
Expand Up @@ -824,7 +824,23 @@ describe("minify option", () => {
const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(stats.compilation.errors).toMatchSnapshot("errors");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it("should work using when the `minify` option is `esbuildMinify` and output well formatted warnings", async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/warning.js"),
});

new TerserPlugin({
minify: TerserPlugin.esbuildMinify,
}).apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});
});

0 comments on commit 2e54869

Please sign in to comment.