From 4a28d55fdf2d0390e979735b2012fd129388f5b1 Mon Sep 17 00:00:00 2001 From: MJC Date: Wed, 26 Dec 2018 19:29:23 -0600 Subject: [PATCH] fix: minify function override breaks build when extractComments is true --- src/index.js | 6 ++- test/__snapshots__/minify-option.test.js.snap | 6 +++ test/minify-option.test.js | 39 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7398f4b8..b92fa966 100644 --- a/src/index.js +++ b/src/index.js @@ -305,7 +305,11 @@ class TerserPlugin { } // Write extracted comments to commentsFile - if (commentsFile && extractedComments.length > 0) { + if ( + commentsFile && + extractedComments && + extractedComments.length > 0 + ) { if (commentsFile in compilation.assets) { const commentsFileSource = compilation.assets[ commentsFile diff --git a/test/__snapshots__/minify-option.test.js.snap b/test/__snapshots__/minify-option.test.js.snap index 4e09b610..b06d906a 100644 --- a/test/__snapshots__/minify-option.test.js.snap +++ b/test/__snapshots__/minify-option.test.js.snap @@ -183,6 +183,12 @@ exports[`when applied with \`minify\` option matches snapshot for \`terser\` min exports[`when applied with \`minify\` option matches snapshot for \`terser\` minifier: warnings 1`] = `Array []`; +exports[`when applied with \`minify\` option matches snapshot for \`uglify-js\` minifier while extracting comments: errors 1`] = `Array []`; + +exports[`when applied with \`minify\` option matches snapshot for \`uglify-js\` minifier while extracting comments: main.js 1`] = `"!function(n){var r={};function o(e){if(r[e])return r[e].exports;var t=r[e]={i:e,l:!1,exports:{}};return n[e].call(t.exports,t,t.exports,o),t.l=!0,t.exports}o.m=n,o.c=r,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&\\"object\\"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:t}),2&e&&\\"string\\"!=typeof t)for(var r in t)o.d(n,r,function(e){return t[e]}.bind(null,r));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,\\"a\\",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p=\\"\\",o(o.s=0)}([function(e,t){e.exports=function(){var baz=document.getElementById(\\"root\\").innerHTML;document.getElementById(\\"demo\\").innerHTML=\\"Paragraph changed.\\"+baz}}]);"`; + +exports[`when applied with \`minify\` option matches snapshot for \`uglify-js\` minifier while extracting comments: warnings 1`] = `Array []`; + exports[`when applied with \`minify\` option matches snapshot for \`uglify-js\` minifier: errors 1`] = `Array []`; exports[`when applied with \`minify\` option matches snapshot for \`uglify-js\` minifier: main.js 1`] = `"!function(n){var r={};function o(e){if(r[e])return r[e].exports;var t=r[e]={i:e,l:!1,exports:{}};return n[e].call(t.exports,t,t.exports,o),t.l=!0,t.exports}o.m=n,o.c=r,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&\\"object\\"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:t}),2&e&&\\"string\\"!=typeof t)for(var r in t)o.d(n,r,function(e){return t[e]}.bind(null,r));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,\\"a\\",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p=\\"\\",o(o.s=0)}([function(e,t){e.exports=function(){var baz=document.getElementById(\\"root\\").innerHTML;document.getElementById(\\"demo\\").innerHTML=\\"Paragraph changed.\\"+baz}}]);"`; diff --git a/test/minify-option.test.js b/test/minify-option.test.js index 0ffe54f9..137f0372 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -59,6 +59,45 @@ describe('when applied with `minify` option', () => { }); }); + it('matches snapshot for `uglify-js` minifier while extracting comments', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/minify/es5.js`, + output: { + path: `${__dirname}/dist-uglify-js`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new TerserPlugin({ + extractComments: true, + minify(file) { + // eslint-disable-next-line global-require + return require('uglify-js').minify(file, { + mangle: { + reserved: ['baz'], + }, + }); + }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + 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); + } + } + }); + }); + it('matches snapshot for `terser` minifier', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/minify/es6.js`,