diff --git a/src/index.js b/src/index.js index 1730f2b9..a5192c5d 100644 --- a/src/index.js +++ b/src/index.js @@ -157,6 +157,10 @@ class UglifyJsPlugin { uglifyOptions.output = uglifyOptions.output || {}; } + if (typeof this.options.sourceMap === 'undefined' && ['sourcemap', 'source-map', 'hidden-source-map'].indexOf(compiler.options.devtool) > -1) { + this.options.sourceMap = true; + } + compiler.plugin('compilation', (compilation) => { if (this.options.sourceMap) { compilation.plugin('build-module', (moduleArg) => { diff --git a/test/__snapshots__/all-options.test.js.snap b/test/__snapshots__/all-options.test.js.snap index 1cb10095..a4827b54 100644 --- a/test/__snapshots__/all-options.test.js.snap +++ b/test/__snapshots__/all-options.test.js.snap @@ -1,5 +1,53 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`devtool === "hidden-source-map" errors 1`] = `Array []`; + +exports[`devtool === "hidden-source-map" main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; + +exports[`devtool === "hidden-source-map" main.0c220ec66316af2c1b24.js.map 1`] = `"{\\"version\\":3,\\"sources\\":[\\"webpack:///./test/fixtures/entry.js\\"],\\"names\\":[\\"module\\",\\"exports\\",\\"console\\",\\"log\\",\\"b\\"],\\"mappings\\":\\"gCAKAA,EAAAC,QAAA,WAEAC,QAAAC,IAAAC\\",\\"file\\":\\"main.0c220ec66316af2c1b24.js\\",\\"sourcesContent\\":[\\"// 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\\\\n//////////////////\\\\n// WEBPACK FOOTER\\\\n// ./test/fixtures/entry.js\\\\n// module id = 0\\\\n// module chunks = 0\\"],\\"sourceRoot\\":\\"\\"}"`; + +exports[`devtool === "hidden-source-map" manifest.6afe1bc6685e9ab36c1c.js 1`] = `"!function(e){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}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,a){for(var i,u,f,l=0,s=[];l { }).apply(compiler); return compile(compiler).then((stats) => { + const { assets } = stats.compilation; const errors = stats.compilation.errors.map(cleanErrorStack); const warnings = stats.compilation.warnings.map(cleanErrorStack); expect(errors).toMatchSnapshot('errors'); expect(warnings).toMatchSnapshot('warnings'); - for (const file in stats.compilation.assets) { - if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { - expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); - } - } + Object.keys(assets).forEach((file) => { + const source = assets[file].source(); + expect(source).toMatchSnapshot(file); + }); }); }); @@ -82,6 +82,60 @@ describe('when applied with all options', () => { expect(eventBindings.length).toBe(1); }); + describe('with compiler.optioo=ns.devtool set to "sourcemap" || "source-map" || "hidden-source-map"', () => { + it('matches snapshots', () => { + const compiler1 = createCompiler({ devtool: 'sourcemap' }); + const compiler2 = createCompiler({ devtool: 'source-map' }); + const compiler3 = createCompiler({ devtool: 'hidden-source-map' }); + + new UglifyJsPlugin().apply(compiler1); + new UglifyJsPlugin().apply(compiler2); + new UglifyJsPlugin().apply(compiler3); + + return Promise.all([ + compile(compiler1).then((stats) => { + const { assets } = stats.compilation; + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('devtool === "sourcemap" errors'); + expect(warnings).toMatchSnapshot('devtool === "sourcemap" warnings'); + + Object.keys(assets).forEach((file) => { + const source = assets[file].source(); + expect(source).toMatchSnapshot(`devtool === "sourcemap" ${file}`); + }); + }), + compile(compiler2).then((stats) => { + const { assets } = stats.compilation; + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('devtool === "source-map" errors'); + expect(warnings).toMatchSnapshot('devtool === "source-map" warnings'); + + Object.keys(assets).forEach((file) => { + const source = assets[file].source(); + expect(source).toMatchSnapshot(`devtool === "source-map" ${file}`); + }); + }), + compile(compiler3).then((stats) => { + const { assets } = stats.compilation; + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('devtool === "hidden-source-map" errors'); + expect(warnings).toMatchSnapshot('devtool === "hidden-source-map" warnings'); + + Object.keys(assets).forEach((file) => { + const source = assets[file].source(); + expect(source).toMatchSnapshot(`devtool === "hidden-source-map" ${file}`); + }); + }), + ]); + }); + }); + describe('compilation handler', () => { beforeEach(() => { [eventBinding] = eventBindings; diff --git a/test/helpers.js b/test/helpers.js index 221b7d52..e7349f00 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -14,6 +14,7 @@ export class PluginEnvironment { handler, }); }, + options: {}, }; }