@@ -17,6 +17,12 @@ function virtualGlobalEval(jsStr) {
17
17
return globalContext . evalSync ( String ( jsStr ) )
18
18
}
19
19
20
+ const optGenMin = {
21
+ comments : false ,
22
+ minified : true ,
23
+ jsescOption : { minimal : true } ,
24
+ }
25
+
20
26
/**
21
27
* Extract the literal value of an object, and remove an object if all
22
28
* references to the object are replaced.
@@ -120,7 +126,7 @@ function stringArrayV2(ast) {
120
126
// The string array can be found by its binding
121
127
const bind = path . scope . getBinding ( obj . stringArrayName )
122
128
const def = t . variableDeclaration ( 'var' , [ bind . path . node ] )
123
- obj . stringArrayCodes . push ( generator ( def , { minified : true } ) . code )
129
+ obj . stringArrayCodes . push ( generator ( def , optGenMin ) . code )
124
130
// The calls can be found by its references
125
131
for ( let ref of bind . referencePaths ) {
126
132
if ( ref ?. listKey === 'arguments' ) {
@@ -135,7 +141,7 @@ function stringArrayV2(ast) {
135
141
if ( up1 . node . id ) {
136
142
// 2.12.0 <= v < 2.15.4
137
143
// The `stringArrayCallsWrapperName` is included in the definition
138
- obj . stringArrayCodes . push ( generator ( up1 . node , { minified : true } ) . code )
144
+ obj . stringArrayCodes . push ( generator ( up1 . node , optGenMin ) . code )
139
145
up1 . node . body = t . blockStatement ( [ ] )
140
146
obj . stringArrayCalls . push ( { name : up1 . node . id . name , path : up1 } )
141
147
continue
@@ -145,7 +151,7 @@ function stringArrayV2(ast) {
145
151
// The `stringArrayCallsWrapperName` is defined by VariableDeclarator
146
152
up1 = up1 . parentPath
147
153
const node = t . variableDeclaration ( 'var' , [ up1 . node ] )
148
- obj . stringArrayCodes . push ( generator ( node , { minified : true } ) . code )
154
+ obj . stringArrayCodes . push ( generator ( node , optGenMin ) . code )
149
155
up1 . node . init = null
150
156
obj . stringArrayCalls . push ( { name : up1 . node . id . name , path : up1 } )
151
157
continue
@@ -159,15 +165,15 @@ function stringArrayV2(ast) {
159
165
console . warn ( 'Unexpected reference!' )
160
166
continue
161
167
}
162
- obj . stringArrayCodes . push ( generator ( up2 . node , { minified : true } ) . code )
168
+ obj . stringArrayCodes . push ( generator ( up2 . node , optGenMin ) . code )
163
169
up2 . node . body = t . blockStatement ( [ ] )
164
170
obj . stringArrayCalls . push ( { name : wrapper , path : up2 } )
165
171
}
166
172
// Remove the string array
167
173
bind . path . remove ( )
168
174
// Add the rotate function
169
175
const node = t . expressionStatement ( path . node )
170
- obj . stringArrayCodes . push ( generator ( node , { minified : true } ) . code )
176
+ obj . stringArrayCodes . push ( generator ( node , optGenMin ) . code )
171
177
path . stop ( )
172
178
if ( path . parentPath . isUnaryExpression ( ) ) {
173
179
path . parentPath . remove ( )
@@ -233,7 +239,7 @@ function stringArrayV3(ast) {
233
239
}
234
240
const nodes = [ ...body ]
235
241
nodes . shift ( )
236
- const code = generator ( t . BlockStatement ( nodes ) ) . code
242
+ const code = generator ( t . BlockStatement ( nodes ) , optGenMin ) . code
237
243
const fp = `${ name_func } =function(){return${ string_var } }${ name_func } ()`
238
244
if ( ! checkPattern ( code , fp ) ) {
239
245
return
@@ -275,7 +281,7 @@ function stringArrayV3(ast) {
275
281
if ( name_func == rm_path . node . id . name ) {
276
282
return
277
283
}
278
- const code = generator ( rm_path . node , { minified : true } ) . code
284
+ const code = generator ( rm_path . node , optGenMin ) . code
279
285
rm_path . node . body = t . blockStatement ( [ ] )
280
286
nodes . push ( [ code , 'func3' , rm_path ] )
281
287
} else {
@@ -287,7 +293,7 @@ function stringArrayV3(ast) {
287
293
return
288
294
}
289
295
ob_string_func_name = name_func
290
- ob_func_str . push ( generator ( path . node , { minified : true } ) . code )
296
+ ob_func_str . push ( generator ( path . node , optGenMin ) . code )
291
297
nodes . map ( function ( item ) {
292
298
if ( item [ 1 ] == 'func3' ) {
293
299
ob_func_str . push ( item [ 0 ] )
@@ -298,7 +304,7 @@ function stringArrayV3(ast) {
298
304
if ( t . isCallExpression ( node ) ) {
299
305
node = t . expressionStatement ( node )
300
306
}
301
- ob_func_str . push ( generator ( node , { minified : true } ) . code )
307
+ ob_func_str . push ( generator ( node , optGenMin ) . code )
302
308
} )
303
309
path . stop ( )
304
310
path . remove ( )
@@ -334,7 +340,7 @@ function decodeGlobal(ast) {
334
340
Program ( path ) {
335
341
let loc = path . scope . getBinding ( lost ) . path
336
342
let obj = t . variableDeclaration ( loc . parent . kind , [ loc . node ] )
337
- ob_func_str . unshift ( generator ( obj , { minified : true } ) . code )
343
+ ob_func_str . unshift ( generator ( obj , optGenMin ) . code )
338
344
loc . node . init = null
339
345
ob_dec_call . push ( { name : lost , path : loc } )
340
346
path . stop ( )
@@ -358,7 +364,7 @@ function decodeGlobal(ast) {
358
364
// FunctionDeclaration
359
365
// function A (...) { return function B (...) }
360
366
root = func
361
- code = generator ( root . node , { minified : true } ) . code
367
+ code = generator ( root . node , optGenMin ) . code
362
368
} else {
363
369
// FunctionExpression
364
370
// var A = function (...) { return function B (...) }
@@ -975,22 +981,13 @@ module.exports = function (code) {
975
981
console . log ( '清理死代码...' )
976
982
ast = cleanDeadCode ( ast )
977
983
// 刷新代码
978
- ast = parse (
979
- generator ( ast , {
980
- comments : false ,
981
- jsescOption : { minimal : true } ,
982
- } ) . code ,
983
- { errorRecovery : true }
984
- )
984
+ ast = parse ( generator ( ast , optGenMin ) . code , { errorRecovery : true } )
985
985
console . log ( '提高代码可读性...' )
986
986
ast = purifyCode ( ast )
987
987
console . log ( '解除环境限制...' )
988
988
ast = unlockEnv ( ast )
989
989
console . log ( '净化完成' )
990
- code = generator ( ast , {
991
- comments : false ,
992
- jsescOption : { minimal : true } ,
993
- } ) . code
990
+ code = generator ( ast , optGenMin ) . code
994
991
if ( global_eval ) {
995
992
code = PluginEval . pack ( code )
996
993
}
0 commit comments