Skip to content

Commit

Permalink
fix: don't crash when no extracted comments (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjcowan authored and evilebottnawi committed Dec 27, 2018
1 parent 5af4547 commit efad586
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/__snapshots__/minify-option.test.js.snap
Original file line number Diff line number Diff line change
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(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}}]);"`;
Expand Down
39 changes: 39 additions & 0 deletions test/minify-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down

0 comments on commit efad586

Please sign in to comment.