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

fix(minify): nameCache assignment (uglifyOptions.nameCache) #147

Merged
merged 1 commit into from
Oct 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 68 additions & 27 deletions src/uglify/minify.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
/* 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')) {
} else if (
Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')
) {
// Extract condition is given in extractComments.condition
condition.preserve = commentsOpts;
condition.extract = options.extractComments.condition;
Expand All @@ -42,26 +60,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));
}
});
Expand All @@ -74,14 +105,15 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`,
);
}

return condition.preserve(astNode, comment);
};
};

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) {
Expand All @@ -91,11 +123,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 };
};

Expand Down