From aed210a550c8df1746261687be89076d44e30c1b Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 21 Oct 2019 18:29:39 +0300 Subject: [PATCH] test: errors from `terser` --- src/index.js | 24 ++++++++-- test/TerserPlugin.test.js | 46 ++++++++++++++++++++ test/__snapshots__/TerserPlugin.test.js.snap | 18 ++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index fe0ab684..61c26416 100644 --- a/src/index.js +++ b/src/index.js @@ -88,14 +88,32 @@ class TerserPlugin { original.source )}:${original.line},${original.column}][${file}:${error.line},${ error.col - }]` + }]${ + error.stack + ? `\n${error.stack + .split('\n') + .slice(1) + .join('\n')}` + : '' + }` ); } return new Error( - `${file} from Terser\n${error.message} [${file}:${error.line},${error.col}]` + `${file} from Terser\n${error.message} [${file}:${error.line},${ + error.col + }]${ + error.stack + ? `\n${error.stack + .split('\n') + .slice(1) + .join('\n')}` + : '' + }` ); - } else if (error.stack) { + } + + if (error.stack) { return new Error(`${file} from Terser\n${error.stack}`); } diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index 3149c634..30300ee2 100644 --- a/test/TerserPlugin.test.js +++ b/test/TerserPlugin.test.js @@ -156,6 +156,52 @@ describe('TerserPlugin', () => { expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); }); + it('should work and respect "terser" errors (the "parallel" option is "true")', async () => { + const compiler = createCompiler(); + + new TerserPlugin({ + parallel: true, + minify(input) { + // eslint-disable-next-line global-require + return require('terser').minify(`${input}1()2()3()`); + }, + }).apply(compiler); + + const stats = await compile(compiler); + + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + expect(/node_modules(\/|\\)terser/.test(stats.compilation.errors[0])).toBe( + true + ); + }); + + it('should work and respect "terser" errors (the "parallel" option is "false")', async () => { + const compiler = createCompiler(); + + new TerserPlugin({ + parallel: false, + minify(input) { + // eslint-disable-next-line global-require + return require('terser').minify(`${input}1()2()3()`); + }, + }).apply(compiler); + + const stats = await compile(compiler); + + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + expect(/node_modules(\/|\\)terser/.test(stats.compilation.errors[0])).toBe( + true + ); + }); + it('should regenerate hash', async () => { const originalMainTemplateUpdateHashForChunk = MainTemplate.prototype.updateHashForChunk; diff --git a/test/__snapshots__/TerserPlugin.test.js.snap b/test/__snapshots__/TerserPlugin.test.js.snap index 28f656a6..70b99223 100644 --- a/test/__snapshots__/TerserPlugin.test.js.snap +++ b/test/__snapshots__/TerserPlugin.test.js.snap @@ -92,6 +92,24 @@ exports[`TerserPlugin should work (without options): errors 1`] = `Array []`; exports[`TerserPlugin should work (without options): warnings 1`] = `Array []`; +exports[`TerserPlugin should work and respect "terser" errors (the "parallel" option is "false"): errors 1`] = ` +Array [ + "Error: main.js from Terser +Unexpected token name «Object», expected punc «,» [main.js:1,8]", +] +`; + +exports[`TerserPlugin should work and respect "terser" errors (the "parallel" option is "false"): warnings 1`] = `Array []`; + +exports[`TerserPlugin should work and respect "terser" errors (the "parallel" option is "true"): errors 1`] = ` +Array [ + "Error: main.js from Terser +Unexpected token name «Object», expected punc «,» [main.js:1,8]", +] +`; + +exports[`TerserPlugin should work and respect "terser" errors (the "parallel" option is "true"): warnings 1`] = `Array []`; + exports[`TerserPlugin should work as a minimizer: assets 1`] = ` Object { "main.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);",