@@ -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 = / ^ \* * ! | @ 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+ }
5051
5152const 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+ / ^ \* * ! | @ p r e s e r v e | @ l i c e n s e | @ c c _ o n / i . test ( comment . value )
110110 ) ;
111111 } ;
112112
@@ -147,13 +147,7 @@ const buildComments = (options, terserOptions, extractedComments) => {
147147} ;
148148
149149const 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