1
+ /* eslint-disable
2
+ arrow-body-style
3
+ */
1
4
import uglify from 'uglify-es' ;
2
5
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
+ } ) ;
23
35
24
36
const buildComments = ( options , uglifyOptions , extractedComments ) => {
25
37
const condition = { } ;
26
38
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
+ ) {
28
44
// extractComments specifies the extract condition and commentsOpts specifies the preserve condition
29
45
condition . preserve = commentsOpts ;
30
46
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
+ ) {
32
50
// Extract condition is given in extractComments.condition
33
51
condition . preserve = commentsOpts ;
34
52
condition . extract = options . extractComments . condition ;
@@ -42,26 +60,39 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
42
60
[ 'preserve' , 'extract' ] . forEach ( ( key ) => {
43
61
let regexStr ;
44
62
let regex ;
63
+
45
64
switch ( typeof ( condition [ key ] ) ) {
46
65
case 'boolean' :
47
66
condition [ key ] = condition [ key ] ? ( ) => true : ( ) => false ;
67
+
48
68
break ;
49
69
case 'function' :
50
70
break ;
51
71
case 'string' :
52
72
if ( condition [ key ] === 'all' ) {
53
73
condition [ key ] = ( ) => true ;
74
+
54
75
break ;
55
76
}
77
+
56
78
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
+
58
83
break ;
59
84
}
85
+
60
86
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
+
62
92
break ;
63
93
default :
64
94
regex = condition [ key ] ;
95
+
65
96
condition [ key ] = ( astNode , comment ) => ( regex . test ( comment . value ) ) ;
66
97
}
67
98
} ) ;
@@ -74,14 +105,15 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
74
105
comment . type === 'comment2' ? `/*${ comment . value } */` : `//${ comment . value } ` ,
75
106
) ;
76
107
}
108
+
77
109
return condition . preserve ( astNode , comment ) ;
78
110
} ;
79
111
} ;
80
112
81
113
const minify = ( options ) => {
82
114
const { file, input, inputSourceMap, extractComments } = options ;
83
115
// Copy uglify options
84
- const uglifyOptions = buildDefaultUglifyOptions ( options . uglifyOptions || { } ) ;
116
+ const uglifyOptions = buildUglifyOptions ( options . uglifyOptions ) ;
85
117
86
118
// Add source map data
87
119
if ( inputSourceMap ) {
@@ -91,11 +123,20 @@ const minify = (options) => {
91
123
}
92
124
93
125
const extractedComments = [ ] ;
126
+
94
127
if ( extractComments ) {
95
- uglifyOptions . output . comments = buildComments ( options , uglifyOptions , extractedComments ) ;
128
+ uglifyOptions . output . comments = buildComments (
129
+ options ,
130
+ uglifyOptions ,
131
+ extractedComments ,
132
+ ) ;
96
133
}
97
134
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
+
99
140
return { error, map, code, warnings, extractedComments } ;
100
141
} ;
101
142
0 commit comments