Skip to content

Commit

Permalink
feat: propagate an error stacktrace from terser (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Oct 21, 2019
1 parent d01c1b5 commit a11e66b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

Expand Down
46 changes: 46 additions & 0 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)}}]);",
Expand Down

0 comments on commit a11e66b

Please sign in to comment.