Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
fix: don't crash when no extracted comments (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 27, 2018
1 parent e3eff76 commit 68ad71c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/__snapshots__/minify-option.test.js.snap
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/minify-option.test.js
Expand Up @@ -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);
}
}
});
});
});

0 comments on commit 68ad71c

Please sign in to comment.