@@ -39,7 +39,7 @@ export interface ProcessBundleOptions {
39
39
optimize ?: boolean ;
40
40
optimizeOnly ?: boolean ;
41
41
ignoreOriginal ?: boolean ;
42
- cacheKeys ?: ( string | null ) [ ] ;
42
+ cacheKeys ?: ( string | undefined ) [ ] ;
43
43
integrityAlgorithm ?: 'sha256' | 'sha384' | 'sha512' ;
44
44
runtimeData ?: ProcessBundleResult [ ] ;
45
45
replacements ?: [ string , string ] [ ] ;
@@ -80,9 +80,9 @@ export function setup(data: number[] | { cachePath: string; i18n: I18nOptions })
80
80
i18n = options . i18n ;
81
81
}
82
82
83
- async function cachePut ( content : string , key : string | null , integrity ?: string ) : Promise < void > {
83
+ async function cachePut ( content : string , key : string | undefined , integrity ?: string ) : Promise < void > {
84
84
if ( cachePath && key ) {
85
- await cacache . put ( cachePath , key , content , {
85
+ await cacache . put ( cachePath , key || null , content , {
86
86
metadata : { integrity } ,
87
87
} ) ;
88
88
}
@@ -114,7 +114,6 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
114
114
const codeSize = Buffer . byteLength ( options . code ) ;
115
115
const mapSize = options . map ? Buffer . byteLength ( options . map ) : 0 ;
116
116
const manualSourceMaps = codeSize >= 500 * 1024 || mapSize >= 500 * 1024 ;
117
-
118
117
const sourceCode = options . code ;
119
118
const sourceMap = options . map ? JSON . parse ( options . map ) : undefined ;
120
119
@@ -155,59 +154,21 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
155
154
}
156
155
}
157
156
158
- if ( options . optimize ) {
159
- if ( downlevelCode ) {
160
- const minifyResult = terserMangle ( downlevelCode , {
161
- filename : downlevelFilename ,
162
- map : downlevelMap ,
163
- compress : true ,
164
- } ) ;
165
- downlevelCode = minifyResult . code ;
166
- downlevelMap = minifyResult . map ;
167
- }
168
-
169
- if ( ! options . ignoreOriginal ) {
170
- result . original = await mangleOriginal ( options ) ;
171
- }
172
- }
173
-
174
157
if ( downlevelCode ) {
175
- const downlevelPath = path . join ( basePath , downlevelFilename ) ;
176
-
177
- let mapContent ;
178
- if ( downlevelMap ) {
179
- if ( ! options . hiddenSourceMaps ) {
180
- downlevelCode += `\n//# sourceMappingURL=${ downlevelFilename } .map` ;
181
- }
182
-
183
- mapContent = JSON . stringify ( downlevelMap ) ;
184
- await cachePut ( mapContent , options . cacheKeys [ CacheKey . DownlevelMap ] ) ;
185
- fs . writeFileSync ( downlevelPath + '.map' , mapContent ) ;
186
- }
187
-
188
- result . downlevel = createFileEntry (
189
- path . join ( basePath , downlevelFilename ) ,
190
- downlevelCode ,
191
- mapContent ,
192
- options . integrityAlgorithm ,
193
- ) ;
194
-
195
- await cachePut (
196
- downlevelCode ,
197
- options . cacheKeys [ CacheKey . DownlevelCode ] ,
198
- result . downlevel . integrity ,
199
- ) ;
200
- fs . writeFileSync ( downlevelPath , downlevelCode ) ;
158
+ result . downlevel = await processBundle ( {
159
+ ...options ,
160
+ code : downlevelCode ,
161
+ map : downlevelMap ,
162
+ filename : path . join ( basePath , downlevelFilename ) ,
163
+ isOriginal : false ,
164
+ } ) ;
201
165
}
202
166
203
- // If original was not processed, add info
204
167
if ( ! result . original && ! options . ignoreOriginal ) {
205
- result . original = createFileEntry (
206
- options . filename ,
207
- options . code ,
208
- options . map ,
209
- options . integrityAlgorithm ,
210
- ) ;
168
+ result . original = await processBundle ( {
169
+ ...options ,
170
+ isOriginal : true ,
171
+ } ) ;
211
172
}
212
173
213
174
return result ;
@@ -286,41 +247,74 @@ async function mergeSourceMapsFast(first: RawSourceMap, second: RawSourceMap) {
286
247
return map ;
287
248
}
288
249
289
- async function mangleOriginal ( options : ProcessBundleOptions ) : Promise < ProcessBundleFile > {
290
- const result = terserMangle ( options . code , {
291
- filename : path . basename ( options . filename ) ,
292
- map : options . map ? JSON . parse ( options . map ) : undefined ,
293
- ecma : 6 ,
294
- } ) ;
250
+ async function processBundle (
251
+ options : Omit < ProcessBundleOptions , 'map' > & { isOriginal : boolean ; map ?: string | RawSourceMap } ,
252
+ ) : Promise < ProcessBundleFile > {
253
+ const {
254
+ optimize,
255
+ isOriginal,
256
+ code,
257
+ map,
258
+ filename : filepath ,
259
+ hiddenSourceMaps,
260
+ cacheKeys = [ ] ,
261
+ integrityAlgorithm,
262
+ } = options ;
263
+
264
+ const rawMap = typeof map === 'string' ? JSON . parse ( map ) as RawSourceMap : map ;
265
+ const filename = path . basename ( filepath ) ;
266
+
267
+ let result : {
268
+ code : string ,
269
+ map : RawSourceMap | undefined ,
270
+ } ;
271
+
272
+ if ( optimize ) {
273
+ result = terserMangle ( code , {
274
+ filename,
275
+ map : rawMap ,
276
+ compress : ! isOriginal , // We only compress bundles which are downlevelled.
277
+ ecma : isOriginal ? 6 : 5 ,
278
+ } ) ;
279
+ } else {
280
+ if ( rawMap ) {
281
+ rawMap . file = filename ;
282
+ }
295
283
296
- let mapContent ;
284
+ result = {
285
+ map : rawMap ,
286
+ code,
287
+ } ;
288
+ }
289
+
290
+ let mapContent : string | undefined ;
297
291
if ( result . map ) {
298
- if ( ! options . hiddenSourceMaps ) {
299
- result . code += `\n//# sourceMappingURL=${ path . basename ( options . filename ) } .map` ;
292
+ if ( ! hiddenSourceMaps ) {
293
+ result . code += `\n//# sourceMappingURL=${ filename } .map` ;
300
294
}
301
295
302
296
mapContent = JSON . stringify ( result . map ) ;
303
297
304
298
await cachePut (
305
299
mapContent ,
306
- ( options . cacheKeys && options . cacheKeys [ CacheKey . OriginalMap ] ) || null ,
300
+ cacheKeys [ isOriginal ? CacheKey . OriginalMap : CacheKey . DownlevelMap ] ,
307
301
) ;
308
- fs . writeFileSync ( options . filename + '.map' , mapContent ) ;
302
+ fs . writeFileSync ( filepath + '.map' , mapContent ) ;
309
303
}
310
304
311
305
const fileResult = createFileEntry (
312
- options . filename ,
306
+ filepath ,
313
307
result . code ,
314
308
mapContent ,
315
- options . integrityAlgorithm ,
309
+ integrityAlgorithm ,
316
310
) ;
317
311
318
312
await cachePut (
319
313
result . code ,
320
- ( options . cacheKeys && options . cacheKeys [ CacheKey . OriginalCode ] ) || null ,
314
+ cacheKeys [ isOriginal ? CacheKey . OriginalCode : CacheKey . DownlevelCode ] ,
321
315
fileResult . integrity ,
322
316
) ;
323
- fs . writeFileSync ( options . filename , result . code ) ;
317
+ fs . writeFileSync ( filepath , result . code ) ;
324
318
325
319
return fileResult ;
326
320
}
@@ -421,66 +415,19 @@ async function processRuntime(
421
415
// Extra spacing is intentional to align source line positions
422
416
downlevelCode = downlevelCode . replace ( / " \- e s 2 0 \d { 2 } \. / , ' "-es5.' ) ;
423
417
424
- const downlevelFilePath = options . filename . replace ( / \- e s 2 0 \d { 2 } / , '-es5' ) ;
425
- let downlevelMap ;
426
- let result ;
427
- if ( options . optimize ) {
428
- const minifiyResults = terserMangle ( downlevelCode , {
429
- filename : path . basename ( downlevelFilePath ) ,
430
- map : options . map === undefined ? undefined : JSON . parse ( options . map ) ,
431
- } ) ;
432
- downlevelCode = minifiyResults . code ;
433
- downlevelMap = JSON . stringify ( minifiyResults . map ) ;
434
-
435
- result = {
436
- original : await mangleOriginal ( { ...options , code : originalCode } ) ,
437
- downlevel : createFileEntry (
438
- downlevelFilePath ,
439
- downlevelCode ,
440
- downlevelMap ,
441
- options . integrityAlgorithm ,
442
- ) ,
443
- } ;
444
- } else {
445
- if ( options . map ) {
446
- const rawMap = JSON . parse ( options . map ) as RawSourceMap ;
447
- rawMap . file = path . basename ( downlevelFilePath ) ;
448
- downlevelMap = JSON . stringify ( rawMap ) ;
449
- }
450
-
451
- result = {
452
- original : createFileEntry (
453
- options . filename ,
454
- originalCode ,
455
- options . map ,
456
- options . integrityAlgorithm ,
457
- ) ,
458
- downlevel : createFileEntry (
459
- downlevelFilePath ,
460
- downlevelCode ,
461
- downlevelMap ,
462
- options . integrityAlgorithm ,
463
- ) ,
464
- } ;
465
- }
466
-
467
- if ( downlevelMap ) {
468
- await cachePut (
469
- downlevelMap ,
470
- ( options . cacheKeys && options . cacheKeys [ CacheKey . DownlevelMap ] ) || null ,
471
- ) ;
472
- fs . writeFileSync ( downlevelFilePath + '.map' , downlevelMap ) ;
473
- if ( ! options . hiddenSourceMaps ) {
474
- downlevelCode += `\n//# sourceMappingURL=${ path . basename ( downlevelFilePath ) } .map` ;
475
- }
476
- }
477
- await cachePut (
478
- downlevelCode ,
479
- ( options . cacheKeys && options . cacheKeys [ CacheKey . DownlevelCode ] ) || null ,
480
- ) ;
481
- fs . writeFileSync ( downlevelFilePath , downlevelCode ) ;
482
-
483
- return result ;
418
+ return {
419
+ original : await processBundle ( {
420
+ ...options ,
421
+ code : originalCode ,
422
+ isOriginal : true ,
423
+ } ) ,
424
+ downlevel : await processBundle ( {
425
+ ...options ,
426
+ code : downlevelCode ,
427
+ filename : options . filename . replace ( / \- e s 2 0 \d { 2 } / , '-es5' ) ,
428
+ isOriginal : false ,
429
+ } ) ,
430
+ } ;
484
431
}
485
432
486
433
function createReplacePlugin ( replacements : [ string , string ] [ ] ) : PluginObj {
0 commit comments