Skip to content

Commit

Permalink
fix: make extractComments API more consistent (#129)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: using the `extractComments.condition` option with `true` value extract only `some` comments
  • Loading branch information
evilebottnawi committed Sep 2, 2019
1 parent edbd3e0 commit 37d2df0
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 446 deletions.
12 changes: 9 additions & 3 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ const buildTerserOptions = ({
safari10,
});

const someCommentsRegExp = /^\**!|@preserve|@license|@cc_on/i;

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;
condition.extract = someCommentsRegExp;
} else if (
typeof options.extractComments === 'string' ||
options.extractComments instanceof RegExp
Expand All @@ -72,7 +74,11 @@ const buildComments = (options, terserOptions, extractedComments) => {
) {
// Extract condition is given in extractComments.condition
condition.preserve = commentsOpts;
condition.extract = options.extractComments.condition;
condition.extract =
typeof options.extractComments.condition === 'boolean' &&
options.extractComments.condition
? 'some'
: options.extractComments.condition;
} else {
// No extract condition is given. Extract comments that match commentsOpts instead of preserving them
condition.preserve = false;
Expand Down Expand Up @@ -102,7 +108,7 @@ const buildComments = (options, terserOptions, extractedComments) => {
condition[key] = (astNode, comment) => {
return (
comment.type === 'comment2' &&
/^\**!|@preserve|@license|@cc_on/i.test(comment.value)
someCommentsRegExp.test(comment.value)
);
};

Expand Down
Loading

0 comments on commit 37d2df0

Please sign in to comment.