From 75d392716aeb132151b0a9fa5f07190fc8b97ba9 Mon Sep 17 00:00:00 2001 From: Michael Ciniawsky Date: Mon, 23 Oct 2017 23:35:29 +0200 Subject: [PATCH] fix(minify): `nameCache` assignment (`uglifyOptions.nameCache`) --- src/uglify/minify.js | 96 +++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 27 deletions(-) diff --git a/src/uglify/minify.js b/src/uglify/minify.js index 3ea0930d..043b8347 100644 --- a/src/uglify/minify.js +++ b/src/uglify/minify.js @@ -1,34 +1,53 @@ +/* eslint-disable + arrow-body-style +*/ import uglify from 'uglify-es'; -const buildDefaultUglifyOptions = ({ ecma, warnings, parse = {}, compress = {}, mangle, output, toplevel, ie8 }) => { - return { - ecma, - warnings, - parse, - compress, - mangle: mangle == null ? true : mangle, - // Ignoring sourcemap from options - sourceMap: null, - output: { - comments: /^\**!|@preserve|@license|@cc_on/, - beautify: false, - semicolons: true, - shebang: true, - ...output, - }, - toplevel, - ie8, - }; -}; +const buildUglifyOptions = ({ + ie8, + ecma, + parse = {}, + mangle, + output, + compress = {}, + warnings, + toplevel, + nameCache, +} = {}) => ({ + ie8, + ecma, + parse, + mangle: mangle == null ? true : mangle, + output: { + shebang: true, + comments: /^\**!|@preserve|@license|@cc_on/, + beautify: false, + semicolons: true, + ...output, + }, + compress, + warnings, + toplevel, + nameCache, + // Ignoring sourcemap from options + sourceMap: null, +}); const buildComments = (options, uglifyOptions, extractedComments) => { const condition = {}; const commentsOpts = uglifyOptions.output.comments; - if (typeof options.extractComments === 'string' || options.extractComments instanceof RegExp) { + + if ( + typeof options.extractComments === 'string' || + options.extractComments instanceof RegExp + ) { // extractComments specifies the extract condition and commentsOpts specifies the preserve condition condition.preserve = commentsOpts; condition.extract = options.extractComments; - } else if (Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')) { + // Object.prototype.hasOwnProperty.call(options.extractComments, 'condition') + } else if ( + Object.prototype.hasOwnProperty.call(options.extractComments, 'condition') + ) { // Extract condition is given in extractComments.condition condition.preserve = commentsOpts; condition.extract = options.extractComments.condition; @@ -42,26 +61,39 @@ const buildComments = (options, uglifyOptions, extractedComments) => { ['preserve', 'extract'].forEach((key) => { let regexStr; let regex; + switch (typeof (condition[key])) { case 'boolean': condition[key] = condition[key] ? () => true : () => false; + break; case 'function': break; case 'string': if (condition[key] === 'all') { condition[key] = () => true; + break; } + if (condition[key] === 'some') { - condition[key] = (astNode, comment) => comment.type === 'comment2' && /@preserve|@license|@cc_on/i.test(comment.value); + condition[key] = (astNode, comment) => { + return comment.type === 'comment2' && /@preserve|@license|@cc_on/i.test(comment.value); + }; + break; } + regexStr = condition[key]; - condition[key] = (astNode, comment) => new RegExp(regexStr).test(comment.value); + + condition[key] = (astNode, comment) => { + return new RegExp(regexStr).test(comment.value); + }; + break; default: regex = condition[key]; + condition[key] = (astNode, comment) => (regex.test(comment.value)); } }); @@ -74,6 +106,7 @@ const buildComments = (options, uglifyOptions, extractedComments) => { comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`, ); } + return condition.preserve(astNode, comment); }; }; @@ -81,7 +114,7 @@ const buildComments = (options, uglifyOptions, extractedComments) => { const minify = (options) => { const { file, input, inputSourceMap, extractComments } = options; // Copy uglify options - const uglifyOptions = buildDefaultUglifyOptions(options.uglifyOptions || {}); + const uglifyOptions = buildUglifyOptions(options.uglifyOptions); // Add source map data if (inputSourceMap) { @@ -91,11 +124,20 @@ const minify = (options) => { } const extractedComments = []; + if (extractComments) { - uglifyOptions.output.comments = buildComments(options, uglifyOptions, extractedComments); + uglifyOptions.output.comments = buildComments( + options, + uglifyOptions, + extractedComments, + ); } - const { error, map, code, warnings } = uglify.minify({ [file]: input }, uglifyOptions); + const { error, map, code, warnings } = uglify.minify( + { [file]: input }, + uglifyOptions, + ); + return { error, map, code, warnings, extractedComments }; };