Unverified Commit 3f9a4499 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Remove more terser code (#6564)

* Remove chunkfilter and extractcomments options as they’re unused

* Remove unused variable

* Remove cacheKeys option, improve filter

* Remove more terser code

* Remove unused variable
parent d9060f5a
......@@ -15,7 +15,6 @@ const JS_REGEX = /\.m?js$/
class TerserPlugin {
constructor(options = {}) {
const {
minify,
terserOptions = {},
warningsFilter = () => true,
sourceMap = false,
......@@ -28,7 +27,6 @@ class TerserPlugin {
sourceMap,
cache,
parallel,
minify,
terserOptions: {
output: {
comments: /^\**!|@preserve|@license|@cc_on/i,
......@@ -182,8 +180,7 @@ class TerserPlugin {
file,
input,
inputSourceMap,
terserOptions: this.options.terserOptions,
minify: this.options.minify,
terserOptions: this.options.terserOptions
};
if (this.options.cache) {
......
......@@ -49,112 +49,13 @@ const buildTerserOptions = ({
safari10,
});
const buildComments = (options, terserOptions, extractedComments) => {
const condition = {};
const commentsOpts = terserOptions.output.comments;
// Use /^\**!|@preserve|@license|@cc_on/i RegExp
if (typeof options.extractComments === 'boolean') {
condition.preserve = commentsOpts;
condition.extract = /^\**!|@preserve|@license|@cc_on/i;
} else 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 (typeof options.extractComments === 'function') {
condition.preserve = commentsOpts;
condition.extract = options.extractComments;
} else if (
Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')
) {
// Extract condition is given in extractComments.condition
condition.preserve = commentsOpts;
condition.extract = options.extractComments.condition;
} else {
// No extract condition is given. Extract comments that match commentsOpts instead of preserving them
condition.preserve = false;
condition.extract = commentsOpts;
}
// Ensure that both conditions are functions
['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) => {
return (
comment.type === 'comment2' &&
/^\**!|@preserve|@license|@cc_on/i.test(comment.value)
);
};
break;
}
regexStr = condition[key];
condition[key] = (astNode, comment) => {
return new RegExp(regexStr).test(comment.value);
};
break;
default:
regex = condition[key];
condition[key] = (astNode, comment) => regex.test(comment.value);
}
});
// Redefine the comments function to extract and preserve
// comments according to the two conditions
return (astNode, comment) => {
if (condition.extract(astNode, comment)) {
const commentText =
comment.type === 'comment2'
? `/*${comment.value}*/`
: `//${comment.value}`;
// Don't include duplicate comments
if (!extractedComments.includes(commentText)) {
extractedComments.push(commentText);
}
}
return condition.preserve(astNode, comment);
};
};
const minify = (options) => {
const {
file,
input,
inputSourceMap,
extractComments,
minify: minifyFn,
inputSourceMap
} = options;
if (minifyFn) {
return minifyFn({ [file]: input }, inputSourceMap);
}
// Copy terser options
const terserOptions = buildTerserOptions(options.terserOptions);
......@@ -165,22 +66,12 @@ const minify = (options) => {
};
}
const extractedComments = [];
if (extractComments) {
terserOptions.output.comments = buildComments(
options,
terserOptions,
extractedComments
);
}
const { error, map, code, warnings } = terserMinify(
{ [file]: input },
terserOptions
);
return { error, map, code, warnings, extractedComments };
return { error, map, code, warnings };
};
export default minify;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment