Skip to content

Commit 6bdee64

Browse files
authoredSep 24, 2019
fix: logic for extracting and preserving comments (#166)
1 parent 6355274 commit 6bdee64

11 files changed

+5811
-480
lines changed
 

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ module.exports = {
662662

663663
### Remove Comments
664664

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

667667
**webpack.config.js**
668668

@@ -677,6 +677,7 @@ module.exports = {
677677
comments: false,
678678
},
679679
},
680+
extractComments: false,
680681
}),
681682
],
682683
},

‎package-lock.json

+175-208
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"schema-utils": "^2.2.0",
4444
"serialize-javascript": "^2.1.0",
4545
"source-map": "^0.6.1",
46-
"terser": "^4.3.1",
46+
"terser": "^4.3.2",
4747
"webpack-sources": "^1.4.3"
4848
},
4949
"devDependencies": {

‎src/index.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,7 @@ class TerserPlugin {
4848
include,
4949
exclude,
5050
minify,
51-
terserOptions: {
52-
output: {
53-
comments: extractComments
54-
? false
55-
: /^\**!|@preserve|@license|@cc_on/i,
56-
},
57-
...terserOptions,
58-
},
51+
terserOptions,
5952
};
6053
}
6154

‎src/minify.js

+27-35
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ const buildTerserOptions = ({
2929
? mangle
3030
: { ...mangle },
3131
output: {
32-
shebang: true,
33-
comments: false,
3432
beautify: false,
35-
semicolons: true,
3633
...output,
3734
},
3835
module,
@@ -46,41 +43,44 @@ const buildTerserOptions = ({
4643
safari10,
4744
});
4845

49-
const someCommentsRegExp = /^\**!|@preserve|@license|@cc_on/i;
46+
function isObject(value) {
47+
const type = typeof value;
48+
49+
return value != null && (type === 'object' || type === 'function');
50+
}
5051

5152
const buildComments = (options, terserOptions, extractedComments) => {
5253
const condition = {};
5354
const commentsOpts = terserOptions.output.comments;
5455
const { extractComments } = options;
5556

56-
// Use /^\**!|@preserve|@license|@cc_on/i RegExp
57-
if (typeof extractComments === 'boolean') {
58-
condition.preserve = commentsOpts;
59-
condition.extract = someCommentsRegExp;
57+
condition.preserve =
58+
typeof commentsOpts !== 'undefined' ? commentsOpts : false;
59+
60+
if (typeof extractComments === 'boolean' && extractComments) {
61+
condition.extract = 'some';
6062
} else if (
6163
typeof extractComments === 'string' ||
6264
extractComments instanceof RegExp
6365
) {
64-
// extractComments specifies the extract condition and commentsOpts specifies the preserve condition
65-
condition.preserve = commentsOpts;
6666
condition.extract = extractComments;
6767
} else if (typeof extractComments === 'function') {
68-
condition.preserve = commentsOpts;
6968
condition.extract = extractComments;
70-
} else if (
71-
Object.prototype.hasOwnProperty.call(extractComments, 'condition')
72-
) {
73-
// Extract condition is given in extractComments.condition
74-
condition.preserve = commentsOpts;
69+
} else if (isObject(extractComments)) {
7570
condition.extract =
7671
typeof extractComments.condition === 'boolean' &&
7772
extractComments.condition
7873
? 'some'
79-
: extractComments.condition;
74+
: typeof extractComments.condition !== 'undefined'
75+
? extractComments.condition
76+
: 'some';
8077
} else {
81-
// No extract condition is given. Extract comments that match commentsOpts instead of preserving them
82-
condition.preserve = false;
83-
condition.extract = commentsOpts;
78+
// No extract
79+
// Preserve using "commentsOpts" or "some"
80+
// Todo remove this in next major release
81+
condition.preserve =
82+
typeof commentsOpts !== 'undefined' ? commentsOpts : 'some';
83+
condition.extract = false;
8484
}
8585

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

@@ -147,13 +147,7 @@ const buildComments = (options, terserOptions, extractedComments) => {
147147
};
148148

149149
const minify = (options) => {
150-
const {
151-
file,
152-
input,
153-
inputSourceMap,
154-
extractComments,
155-
minify: minifyFn,
156-
} = options;
150+
const { file, input, inputSourceMap, minify: minifyFn } = options;
157151

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

170164
const extractedComments = [];
171165

172-
if (extractComments) {
173-
terserOptions.output.comments = buildComments(
174-
options,
175-
terserOptions,
176-
extractedComments
177-
);
178-
}
166+
terserOptions.output.comments = buildComments(
167+
options,
168+
terserOptions,
169+
extractedComments
170+
);
179171

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

0 commit comments

Comments
 (0)
Failed to load comments.