@@ -29,10 +29,7 @@ const buildTerserOptions = ({
29
29
? mangle
30
30
: { ...mangle } ,
31
31
output : {
32
- shebang : true ,
33
- comments : false ,
34
32
beautify : false ,
35
- semicolons : true ,
36
33
...output ,
37
34
} ,
38
35
module,
@@ -46,41 +43,44 @@ const buildTerserOptions = ({
46
43
safari10,
47
44
} ) ;
48
45
49
- const someCommentsRegExp = / ^ \* * ! | @ p r e s e r v e | @ l i c e n s e | @ c c _ o n / i;
46
+ function isObject ( value ) {
47
+ const type = typeof value ;
48
+
49
+ return value != null && ( type === 'object' || type === 'function' ) ;
50
+ }
50
51
51
52
const buildComments = ( options , terserOptions , extractedComments ) => {
52
53
const condition = { } ;
53
54
const commentsOpts = terserOptions . output . comments ;
54
55
const { extractComments } = options ;
55
56
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' ;
60
62
} else if (
61
63
typeof extractComments === 'string' ||
62
64
extractComments instanceof RegExp
63
65
) {
64
- // extractComments specifies the extract condition and commentsOpts specifies the preserve condition
65
- condition . preserve = commentsOpts ;
66
66
condition . extract = extractComments ;
67
67
} else if ( typeof extractComments === 'function' ) {
68
- condition . preserve = commentsOpts ;
69
68
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 ) ) {
75
70
condition . extract =
76
71
typeof extractComments . condition === 'boolean' &&
77
72
extractComments . condition
78
73
? 'some'
79
- : extractComments . condition ;
74
+ : typeof extractComments . condition !== 'undefined'
75
+ ? extractComments . condition
76
+ : 'some' ;
80
77
} 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 ;
84
84
}
85
85
86
86
// Ensure that both conditions are functions
@@ -106,7 +106,7 @@ const buildComments = (options, terserOptions, extractedComments) => {
106
106
condition [ key ] = ( astNode , comment ) => {
107
107
return (
108
108
comment . type === 'comment2' &&
109
- someCommentsRegExp . test ( comment . value )
109
+ / ^ \* * ! | @ p r e s e r v e | @ l i c e n s e | @ c c _ o n / i . test ( comment . value )
110
110
) ;
111
111
} ;
112
112
@@ -147,13 +147,7 @@ const buildComments = (options, terserOptions, extractedComments) => {
147
147
} ;
148
148
149
149
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 ;
157
151
158
152
if ( minifyFn ) {
159
153
return minifyFn ( { [ file ] : input } , inputSourceMap ) ;
@@ -169,13 +163,11 @@ const minify = (options) => {
169
163
170
164
const extractedComments = [ ] ;
171
165
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
+ ) ;
179
171
180
172
const { error, map, code, warnings } = terserMinify (
181
173
{ [ file ] : input } ,
0 commit comments