From 68ad71cfed10da90bf5db0e2aa95ee754f7725e7 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Thu, 27 Dec 2018 15:33:41 +0300 Subject: [PATCH] fix: don't crash when no extracted comments (#387) --- 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 44375353..19bdbfc1 100644 --- a/src/index.js +++ b/src/index.js @@ -306,7 +306,11 @@ class UglifyJsPlugin { } // 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 ea547057..4bd200bd 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(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(){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 errors into \`minify\` option and \`parallel\` is \`true\`: errors 1`] = ` Array [ "Error: main.js from UglifyJs diff --git a/test/minify-option.test.js b/test/minify-option.test.js index 83b73008..3b24e2be 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -212,4 +212,43 @@ 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 UglifyJsPlugin({ + extractComments: true, + minify(file) { + // eslint-disable-next-line global-require + return require('terser').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); + } + } + }); + }); });