Skip to content

Commit

Permalink
fix: logic for extracting and preserving comments (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 24, 2019
1 parent 6355274 commit 6bdee64
Show file tree
Hide file tree
Showing 11 changed files with 5,811 additions and 480 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -662,7 +662,7 @@ module.exports = {

### Remove Comments

If you avoid building with comments, set **terserOptions.output.comments** to **false** as in this config:
If you avoid building with comments, use this config:

**webpack.config.js**

Expand All @@ -677,6 +677,7 @@ module.exports = {
comments: false,
},
},
extractComments: false,
}),
],
},
Expand Down
383 changes: 175 additions & 208 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -43,7 +43,7 @@
"schema-utils": "^2.2.0",
"serialize-javascript": "^2.1.0",
"source-map": "^0.6.1",
"terser": "^4.3.1",
"terser": "^4.3.2",
"webpack-sources": "^1.4.3"
},
"devDependencies": {
Expand Down
9 changes: 1 addition & 8 deletions src/index.js
Expand Up @@ -48,14 +48,7 @@ class TerserPlugin {
include,
exclude,
minify,
terserOptions: {
output: {
comments: extractComments
? false
: /^\**!|@preserve|@license|@cc_on/i,
},
...terserOptions,
},
terserOptions,
};
}

Expand Down
62 changes: 27 additions & 35 deletions src/minify.js
Expand Up @@ -29,10 +29,7 @@ const buildTerserOptions = ({
? mangle
: { ...mangle },
output: {
shebang: true,
comments: false,
beautify: false,
semicolons: true,
...output,
},
module,
Expand All @@ -46,41 +43,44 @@ const buildTerserOptions = ({
safari10,
});

const someCommentsRegExp = /^\**!|@preserve|@license|@cc_on/i;
function isObject(value) {
const type = typeof value;

return value != null && (type === 'object' || type === 'function');
}

const buildComments = (options, terserOptions, extractedComments) => {
const condition = {};
const commentsOpts = terserOptions.output.comments;
const { extractComments } = options;

// Use /^\**!|@preserve|@license|@cc_on/i RegExp
if (typeof extractComments === 'boolean') {
condition.preserve = commentsOpts;
condition.extract = someCommentsRegExp;
condition.preserve =
typeof commentsOpts !== 'undefined' ? commentsOpts : false;

if (typeof extractComments === 'boolean' && extractComments) {
condition.extract = 'some';
} else if (
typeof extractComments === 'string' ||
extractComments instanceof RegExp
) {
// extractComments specifies the extract condition and commentsOpts specifies the preserve condition
condition.preserve = commentsOpts;
condition.extract = extractComments;
} else if (typeof extractComments === 'function') {
condition.preserve = commentsOpts;
condition.extract = extractComments;
} else if (
Object.prototype.hasOwnProperty.call(extractComments, 'condition')
) {
// Extract condition is given in extractComments.condition
condition.preserve = commentsOpts;
} else if (isObject(extractComments)) {
condition.extract =
typeof extractComments.condition === 'boolean' &&
extractComments.condition
? 'some'
: extractComments.condition;
: typeof extractComments.condition !== 'undefined'
? extractComments.condition
: 'some';
} else {
// No extract condition is given. Extract comments that match commentsOpts instead of preserving them
condition.preserve = false;
condition.extract = commentsOpts;
// No extract
// Preserve using "commentsOpts" or "some"
// Todo remove this in next major release
condition.preserve =
typeof commentsOpts !== 'undefined' ? commentsOpts : 'some';
condition.extract = false;
}

// Ensure that both conditions are functions
Expand All @@ -106,7 +106,7 @@ const buildComments = (options, terserOptions, extractedComments) => {
condition[key] = (astNode, comment) => {
return (
comment.type === 'comment2' &&
someCommentsRegExp.test(comment.value)
/^\**!|@preserve|@license|@cc_on/i.test(comment.value)
);
};

Expand Down Expand Up @@ -147,13 +147,7 @@ const buildComments = (options, terserOptions, extractedComments) => {
};

const minify = (options) => {
const {
file,
input,
inputSourceMap,
extractComments,
minify: minifyFn,
} = options;
const { file, input, inputSourceMap, minify: minifyFn } = options;

if (minifyFn) {
return minifyFn({ [file]: input }, inputSourceMap);
Expand All @@ -169,13 +163,11 @@ const minify = (options) => {

const extractedComments = [];

if (extractComments) {
terserOptions.output.comments = buildComments(
options,
terserOptions,
extractedComments
);
}
terserOptions.output.comments = buildComments(
options,
terserOptions,
extractedComments
);

const { error, map, code, warnings } = terserMinify(
{ [file]: input },
Expand Down

0 comments on commit 6bdee64

Please sign in to comment.