1+ /* eslint-disable
2+ arrow-body-style
3+ */
14import uglify from 'uglify-es' ;
25
3- const buildDefaultUglifyOptions = ( { ecma, warnings, parse = { } , compress = { } , mangle, output, toplevel, ie8 } ) => {
4- return {
5- ecma,
6- warnings,
7- parse,
8- compress,
9- mangle : mangle == null ? true : mangle ,
10- // Ignoring sourcemap from options
11- sourceMap : null ,
12- output : {
13- comments : / ^ \* * ! | @ p r e s e r v e | @ l i c e n s e | @ c c _ o n / ,
14- beautify : false ,
15- semicolons : true ,
16- shebang : true ,
17- ...output ,
18- } ,
19- toplevel,
20- ie8,
21- } ;
22- } ;
6+ const buildUglifyOptions = ( {
7+ ie8,
8+ ecma,
9+ parse = { } ,
10+ mangle,
11+ output,
12+ compress = { } ,
13+ warnings,
14+ toplevel,
15+ nameCache,
16+ } = { } ) => ( {
17+ ie8,
18+ ecma,
19+ parse,
20+ mangle : mangle == null ? true : mangle ,
21+ output : {
22+ shebang : true ,
23+ comments : / ^ \* * ! | @ p r e s e r v e | @ l i c e n s e | @ c c _ o n / ,
24+ beautify : false ,
25+ semicolons : true ,
26+ ...output ,
27+ } ,
28+ compress,
29+ warnings,
30+ toplevel,
31+ nameCache,
32+ // Ignoring sourcemap from options
33+ sourceMap : null ,
34+ } ) ;
2335
2436const buildComments = ( options , uglifyOptions , extractedComments ) => {
2537 const condition = { } ;
2638 const commentsOpts = uglifyOptions . output . comments ;
27- if ( typeof options . extractComments === 'string' || options . extractComments instanceof RegExp ) {
39+
40+ if (
41+ typeof options . extractComments === 'string' ||
42+ options . extractComments instanceof RegExp
43+ ) {
2844 // extractComments specifies the extract condition and commentsOpts specifies the preserve condition
2945 condition . preserve = commentsOpts ;
3046 condition . extract = options . extractComments ;
31- } else if ( Object . prototype . hasOwnProperty . call ( options . extractComments , 'condition' ) ) {
47+ } else if (
48+ Object . prototype . hasOwnProperty . call ( options . extractComments , 'condition' )
49+ ) {
3250 // Extract condition is given in extractComments.condition
3351 condition . preserve = commentsOpts ;
3452 condition . extract = options . extractComments . condition ;
@@ -42,26 +60,39 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
4260 [ 'preserve' , 'extract' ] . forEach ( ( key ) => {
4361 let regexStr ;
4462 let regex ;
63+
4564 switch ( typeof ( condition [ key ] ) ) {
4665 case 'boolean' :
4766 condition [ key ] = condition [ key ] ? ( ) => true : ( ) => false ;
67+
4868 break ;
4969 case 'function' :
5070 break ;
5171 case 'string' :
5272 if ( condition [ key ] === 'all' ) {
5373 condition [ key ] = ( ) => true ;
74+
5475 break ;
5576 }
77+
5678 if ( condition [ key ] === 'some' ) {
57- condition [ key ] = ( astNode , comment ) => comment . type === 'comment2' && / @ p r e s e r v e | @ l i c e n s e | @ c c _ o n / i. test ( comment . value ) ;
79+ condition [ key ] = ( astNode , comment ) => {
80+ return comment . type === 'comment2' && / @ p r e s e r v e | @ l i c e n s e | @ c c _ o n / i. test ( comment . value ) ;
81+ } ;
82+
5883 break ;
5984 }
85+
6086 regexStr = condition [ key ] ;
61- condition [ key ] = ( astNode , comment ) => new RegExp ( regexStr ) . test ( comment . value ) ;
87+
88+ condition [ key ] = ( astNode , comment ) => {
89+ return new RegExp ( regexStr ) . test ( comment . value ) ;
90+ } ;
91+
6292 break ;
6393 default :
6494 regex = condition [ key ] ;
95+
6596 condition [ key ] = ( astNode , comment ) => ( regex . test ( comment . value ) ) ;
6697 }
6798 } ) ;
@@ -74,14 +105,15 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
74105 comment . type === 'comment2' ? `/*${ comment . value } */` : `//${ comment . value } ` ,
75106 ) ;
76107 }
108+
77109 return condition . preserve ( astNode , comment ) ;
78110 } ;
79111} ;
80112
81113const minify = ( options ) => {
82114 const { file, input, inputSourceMap, extractComments } = options ;
83115 // Copy uglify options
84- const uglifyOptions = buildDefaultUglifyOptions ( options . uglifyOptions || { } ) ;
116+ const uglifyOptions = buildUglifyOptions ( options . uglifyOptions ) ;
85117
86118 // Add source map data
87119 if ( inputSourceMap ) {
@@ -91,11 +123,20 @@ const minify = (options) => {
91123 }
92124
93125 const extractedComments = [ ] ;
126+
94127 if ( extractComments ) {
95- uglifyOptions . output . comments = buildComments ( options , uglifyOptions , extractedComments ) ;
128+ uglifyOptions . output . comments = buildComments (
129+ options ,
130+ uglifyOptions ,
131+ extractedComments ,
132+ ) ;
96133 }
97134
98- const { error, map, code, warnings } = uglify . minify ( { [ file ] : input } , uglifyOptions ) ;
135+ const { error, map, code, warnings } = uglify . minify (
136+ { [ file ] : input } ,
137+ uglifyOptions ,
138+ ) ;
139+
99140 return { error, map, code, warnings, extractedComments } ;
100141} ;
101142
0 commit comments