diff --git a/src/utils.js b/src/utils.js index d1e45e5..55ebcea 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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}`; }) : [], }; diff --git a/test/__snapshots__/minify-option.test.js.snap b/test/__snapshots__/minify-option.test.js.snap index 226e292..48ed2a7 100644 --- a/test/__snapshots__/minify-option.test.js.snap +++ b/test/__snapshots__/minify-option.test.js.snap @@ -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)})(); diff --git a/test/fixtures/warning.js b/test/fixtures/warning.js new file mode 100644 index 0000000..d81fd0e --- /dev/null +++ b/test/fixtures/warning.js @@ -0,0 +1,5 @@ +const form = new Array; + +if (!form instanceof FormData) { + console.log("error in form") +} diff --git a/test/minify-option.test.js b/test/minify-option.test.js index d86241f..3e836a9 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -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"); }); });