Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: logic for extract and preserve comments #164

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ module.exports = {

### Preserve Comments

Extract all legal comments (i.e. `/^\**!|@preserve|@license|@cc_on/i`) and preserve `/@license/i` comments.
Extract all legal comments (i.e. `/^\**!|@preserve|@cc_on/i`) and preserve `/@license/i` comments.

**webpack.config.js**

Expand All @@ -653,7 +653,6 @@ module.exports = {
comments: /@license/i,
},
},
extractComments: true,
}),
],
},
Expand Down
9 changes: 1 addition & 8 deletions src/index.js
Original file line number Diff line number Diff line change
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
49 changes: 22 additions & 27 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,38 @@ const someCommentsRegExp = /^\**!|@preserve|@license|@cc_on/i;
const buildComments = (options, terserOptions, extractedComments) => {
const condition = {};
const commentsOpts = terserOptions.output.comments;
const { extractComments } = options;

// Use /^\**!|@preserve|@license|@cc_on/i RegExp
if (typeof options.extractComments === 'boolean') {
if (typeof extractComments === 'boolean' && extractComments) {
condition.preserve = commentsOpts;
condition.extract = someCommentsRegExp;
} else if (
typeof options.extractComments === 'string' ||
options.extractComments instanceof RegExp
typeof extractComments === 'string' ||
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.extract = extractComments;
} else if (typeof extractComments === 'function') {
condition.preserve = commentsOpts;
condition.extract = options.extractComments;
condition.extract = extractComments;
} else if (
Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')
extractComments &&
Object.prototype.hasOwnProperty.call(extractComments, 'condition')
) {
// Extract condition is given in extractComments.condition
condition.preserve = commentsOpts;
condition.extract =
typeof options.extractComments.condition === 'boolean' &&
options.extractComments.condition
typeof extractComments.condition === 'boolean' &&
extractComments.condition
? 'some'
: options.extractComments.condition;
: extractComments.condition;
} else {
// No extract condition is given. Extract comments that match commentsOpts instead of preserving them
condition.preserve = false;
condition.extract = commentsOpts;
// No extract condition is given
// Preserve specified or 'some' comments
condition.preserve = commentsOpts || someCommentsRegExp;
condition.extract = false;
}

// Ensure that both conditions are functions
Expand Down Expand Up @@ -146,13 +149,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 @@ -168,13 +165,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
Loading