Skip to content

Commit

Permalink
feat: map webpack@5 options to terser options (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Oct 21, 2019
1 parent 9a0a575 commit f4c47aa
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,31 @@ class TerserPlugin {
}

apply(compiler) {
const { devtool, output } = compiler.options;

this.options.sourceMap =
typeof this.options.sourceMap === 'undefined'
? compiler.options.devtool &&
/^((inline|hidden|nosources)-)?source-?map/.test(
compiler.options.devtool
)
? devtool &&
!devtool.includes('eval') &&
!devtool.includes('cheap') &&
(devtool.includes('source-map') ||
// Todo remove when `webpack@5` support will be dropped
devtool.includes('sourcemap'))
: Boolean(this.options.sourceMap);

const buildModuleFn = (moduleArg) => {
// to get detailed location info about errors
// eslint-disable-next-line no-param-reassign
moduleArg.useSourceMap = true;
};
if (
typeof this.options.terserOptions.module === 'undefined' &&
typeof output.module !== 'undefined'
) {
this.options.terserOptions.module = output.module;
}

if (
typeof this.options.terserOptions.ecma === 'undefined' &&
typeof output.ecmaVersion !== 'undefined'
) {
this.options.terserOptions.ecma = output.ecmaVersion;
}

const optimizeFn = async (compilation, chunks) => {
const processedAssets = new WeakSet();
Expand Down Expand Up @@ -474,7 +486,11 @@ class TerserPlugin {

compiler.hooks.compilation.tap(plugin, (compilation) => {
if (this.options.sourceMap) {
compilation.hooks.buildModule.tap(plugin, buildModuleFn);
compilation.hooks.buildModule.tap(plugin, (moduleArg) => {
// to get detailed location info about errors
// eslint-disable-next-line no-param-reassign
moduleArg.useSourceMap = true;
});
}

const { mainTemplate, chunkTemplate } = compilation;
Expand Down
33 changes: 33 additions & 0 deletions test/__snapshots__/sourceMap-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,39 @@ exports[`sourceMap should match snapshot for a "false" value (the "devtool" opti

exports[`sourceMap should match snapshot for a "false" value (the "devtool" option has the "source-map" value): warnings 1`] = `Array []`;

exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "cheap-source-map" value): 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)}}]);
//# sourceMappingURL=main.js.map",
"main.js.map": {
"version": 3,
"file": "main.js",
"sources": [
"webpack:///main.js"
],
"sourcesContent": [
"!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)}}]);"
],
"mappings": "AAAA",
"sourceRoot": ""
},
}
`;

exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "cheap-source-map" value): errors 1`] = `Array []`;

exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "cheap-source-map" value): warnings 1`] = `Array []`;

exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "eval" value): assets 1`] = `
Object {
"main.js": "!function(e){var n={};function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},t.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},t.t=function(e,n){if(1&n&&(e=t(e)),8&n)return e;if(4&n&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(t.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&n&&\\"string\\"!=typeof e)for(var o in e)t.d(r,o,function(n){return e[n]}.bind(null,o));return r},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,\\"a\\",n),n},t.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},t.p=\\"\\",t(t.s=0)}([function(module,exports){eval(\\"// foo\\\\n/* @preserve*/\\\\n// bar\\\\nconst a = 2 + 2;\\\\n\\\\nmodule.exports = function Foo() {\\\\n const b = 2 + 2;\\\\n console.log(b + 1 + 2);\\\\n};\\\\n\\\\n\\\\n//# sourceURL=webpack:///./test/fixtures/entry.js?\\")}]);",
}
`;

exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "eval" value): errors 1`] = `Array []`;

exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "eval" value): warnings 1`] = `Array []`;

exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "false" value): 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
36 changes: 36 additions & 0 deletions test/sourceMap-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,40 @@ describe('sourceMap', () => {
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});

it('should match snapshot for a "true" value (the "devtool" option has the "eval" value)', async () => {
const compiler = createCompiler({
entry: `${__dirname}/fixtures/entry.js`,
devtool: 'eval',
});

new TerserPlugin({ sourceMap: true }).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(getAssets(stats, compiler)).toMatchSnapshot('assets');
});

it('should match snapshot for a "true" value (the "devtool" option has the "cheap-source-map" value)', async () => {
const compiler = createCompiler({
entry: `${__dirname}/fixtures/entry.js`,
devtool: 'cheap-source-map',
});

new TerserPlugin({ sourceMap: true }).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(getAssets(stats, compiler)).toMatchSnapshot('assets');
});
});

0 comments on commit f4c47aa

Please sign in to comment.