@@ -27,7 +27,7 @@ export interface ProcessBundleOptions {
27
27
optimize ?: boolean ;
28
28
optimizeOnly ?: boolean ;
29
29
ignoreOriginal ?: boolean ;
30
- cacheKeys ?: ( string | null ) [ ] ;
30
+ cacheKeys ?: ( string | undefined ) [ ] ;
31
31
integrityAlgorithm ?: 'sha256' | 'sha384' | 'sha512' ;
32
32
runtimeData ?: ProcessBundleResult [ ] ;
33
33
}
@@ -62,9 +62,9 @@ export function setup(options: { cachePath: string }): void {
62
62
cachePath = options . cachePath ;
63
63
}
64
64
65
- async function cachePut ( content : string , key : string | null , integrity ?: string ) : Promise < void > {
65
+ async function cachePut ( content : string , key : string | undefined , integrity ?: string ) : Promise < void > {
66
66
if ( cachePath && key ) {
67
- await cacache . put ( cachePath , key , content , {
67
+ await cacache . put ( cachePath , key || null , content , {
68
68
metadata : { integrity } ,
69
69
} ) ;
70
70
}
@@ -94,8 +94,7 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
94
94
// if code size is larger than 1 MB, manually handle sourcemaps with newer source-map package.
95
95
const codeSize = Buffer . byteLength ( options . code ) ;
96
96
const mapSize = options . map ? Buffer . byteLength ( options . map ) : 0 ;
97
- const manualSourceMaps = codeSize >= 1024 * 1024 || mapSize >= 1024 * 1024 ;
98
-
97
+ const manualSourceMaps = codeSize >= 500 * 1024 || mapSize >= 500 * 1024 ;
99
98
const sourceCode = options . code ;
100
99
const sourceMap = options . map ? JSON . parse ( options . map ) : false ;
101
100
@@ -138,59 +137,21 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
138
137
}
139
138
}
140
139
141
- if ( options . optimize ) {
142
- if ( downlevelCode ) {
143
- const minifyResult = terserMangle ( downlevelCode , {
144
- filename : downlevelFilename ,
145
- map : downlevelMap ,
146
- compress : true ,
147
- } ) ;
148
- downlevelCode = minifyResult . code ;
149
- downlevelMap = minifyResult . map ;
150
- }
151
-
152
- if ( ! options . ignoreOriginal ) {
153
- result . original = await mangleOriginal ( options ) ;
154
- }
155
- }
156
-
157
140
if ( downlevelCode ) {
158
- const downlevelPath = path . join ( basePath , downlevelFilename ) ;
159
-
160
- let mapContent ;
161
- if ( downlevelMap ) {
162
- if ( ! options . hiddenSourceMaps ) {
163
- downlevelCode += `\n//# sourceMappingURL=${ downlevelFilename } .map` ;
164
- }
165
-
166
- mapContent = JSON . stringify ( downlevelMap ) ;
167
- await cachePut ( mapContent , options . cacheKeys [ CacheKey . DownlevelMap ] ) ;
168
- fs . writeFileSync ( downlevelPath + '.map' , mapContent ) ;
169
- }
170
-
171
- result . downlevel = createFileEntry (
172
- downlevelFilename ,
173
- downlevelCode ,
174
- mapContent ,
175
- options . integrityAlgorithm ,
176
- ) ;
177
-
178
- await cachePut (
179
- downlevelCode ,
180
- options . cacheKeys [ CacheKey . DownlevelCode ] ,
181
- result . downlevel . integrity ,
182
- ) ;
183
- fs . writeFileSync ( downlevelPath , downlevelCode ) ;
141
+ result . downlevel = await processBundle ( {
142
+ ...options ,
143
+ code : downlevelCode ,
144
+ map : downlevelMap ,
145
+ filename : path . join ( basePath , downlevelFilename ) ,
146
+ isOriginal : false ,
147
+ } ) ;
184
148
}
185
149
186
- // If original was not processed, add info
187
150
if ( ! result . original && ! options . ignoreOriginal ) {
188
- result . original = createFileEntry (
189
- options . filename ,
190
- options . code ,
191
- options . map ,
192
- options . integrityAlgorithm ,
193
- ) ;
151
+ result . original = await processBundle ( {
152
+ ...options ,
153
+ isOriginal : true ,
154
+ } ) ;
194
155
}
195
156
196
157
return result ;
@@ -248,41 +209,74 @@ async function mergeSourcemaps(first: RawSourceMap, second: RawSourceMap) {
248
209
return map ;
249
210
}
250
211
251
- async function mangleOriginal ( options : ProcessBundleOptions ) : Promise < ProcessBundleFile > {
252
- const result = terserMangle ( options . code , {
253
- filename : path . basename ( options . filename ) ,
254
- map : options . map ? JSON . parse ( options . map ) : undefined ,
255
- ecma : 6 ,
256
- } ) ;
212
+ async function processBundle (
213
+ options : Omit < ProcessBundleOptions , 'map' > & { isOriginal : boolean ; map ?: string | RawSourceMap } ,
214
+ ) : Promise < ProcessBundleFile > {
215
+ const {
216
+ optimize,
217
+ isOriginal,
218
+ code,
219
+ map,
220
+ filename : filepath ,
221
+ hiddenSourceMaps,
222
+ cacheKeys = [ ] ,
223
+ integrityAlgorithm,
224
+ } = options ;
225
+
226
+ const rawMap = typeof map === 'string' ? JSON . parse ( map ) as RawSourceMap : map ;
227
+ const filename = path . basename ( filepath ) ;
228
+
229
+ let result : {
230
+ code : string ,
231
+ map : RawSourceMap | undefined ,
232
+ } ;
233
+
234
+ if ( optimize ) {
235
+ result = terserMangle ( code , {
236
+ filename,
237
+ map : rawMap ,
238
+ compress : ! isOriginal , // We only compress bundles which are downlevelled.
239
+ ecma : isOriginal ? 6 : 5 ,
240
+ } ) ;
241
+ } else {
242
+ if ( rawMap ) {
243
+ rawMap . file = filename ;
244
+ }
257
245
258
- let mapContent ;
246
+ result = {
247
+ map : rawMap ,
248
+ code,
249
+ } ;
250
+ }
251
+
252
+ let mapContent : string | undefined ;
259
253
if ( result . map ) {
260
- if ( ! options . hiddenSourceMaps ) {
261
- result . code += `\n//# sourceMappingURL=${ path . basename ( options . filename ) } .map` ;
254
+ if ( ! hiddenSourceMaps ) {
255
+ result . code += `\n//# sourceMappingURL=${ filename } .map` ;
262
256
}
263
257
264
258
mapContent = JSON . stringify ( result . map ) ;
265
259
266
260
await cachePut (
267
261
mapContent ,
268
- ( options . cacheKeys && options . cacheKeys [ CacheKey . OriginalMap ] ) || null ,
262
+ cacheKeys [ isOriginal ? CacheKey . OriginalMap : CacheKey . DownlevelMap ] ,
269
263
) ;
270
- fs . writeFileSync ( options . filename + '.map' , mapContent ) ;
264
+ fs . writeFileSync ( filepath + '.map' , mapContent ) ;
271
265
}
272
266
273
267
const fileResult = createFileEntry (
274
- options . filename ,
268
+ filepath ,
275
269
result . code ,
276
270
mapContent ,
277
- options . integrityAlgorithm ,
271
+ integrityAlgorithm ,
278
272
) ;
279
273
280
274
await cachePut (
281
275
result . code ,
282
- ( options . cacheKeys && options . cacheKeys [ CacheKey . OriginalCode ] ) || null ,
276
+ cacheKeys [ isOriginal ? CacheKey . OriginalCode : CacheKey . DownlevelCode ] ,
283
277
fileResult . integrity ,
284
278
) ;
285
- fs . writeFileSync ( options . filename , result . code ) ;
279
+ fs . writeFileSync ( filepath , result . code ) ;
286
280
287
281
return fileResult ;
288
282
}
@@ -383,62 +377,17 @@ async function processRuntime(
383
377
// Extra spacing is intentional to align source line positions
384
378
downlevelCode = downlevelCode . replace ( / " \- e s 2 0 \d { 2 } \. / , ' "-es5.' ) ;
385
379
386
- const downlevelFilePath = options . filename . replace ( / \- e s 2 0 \d { 2 } / , '-es5' ) ;
387
- let downlevelMap ;
388
- let result ;
389
- if ( options . optimize ) {
390
- const minifiyResults = terserMangle ( downlevelCode , {
391
- filename : path . basename ( downlevelFilePath ) ,
392
- map : options . map === undefined ? undefined : JSON . parse ( options . map ) ,
393
- } ) ;
394
- downlevelCode = minifiyResults . code ;
395
- downlevelMap = JSON . stringify ( minifiyResults . map ) ;
396
-
397
- result = {
398
- original : await mangleOriginal ( { ...options , code : originalCode } ) ,
399
- downlevel : createFileEntry (
400
- downlevelFilePath ,
401
- downlevelCode ,
402
- downlevelMap ,
403
- options . integrityAlgorithm ,
404
- ) ,
405
- } ;
406
- } else {
407
- if ( options . map ) {
408
- const rawMap = JSON . parse ( options . map ) as RawSourceMap ;
409
- rawMap . file = path . basename ( downlevelFilePath ) ;
410
- downlevelMap = JSON . stringify ( rawMap ) ;
411
- }
412
-
413
- result = {
414
- original : createFileEntry (
415
- options . filename ,
416
- originalCode ,
417
- options . map ,
418
- options . integrityAlgorithm ,
419
- ) ,
420
- downlevel : createFileEntry (
421
- downlevelFilePath ,
422
- downlevelCode ,
423
- downlevelMap ,
424
- options . integrityAlgorithm ,
425
- ) ,
426
- } ;
427
- }
428
-
429
- if ( downlevelMap ) {
430
- await cachePut (
431
- downlevelMap ,
432
- ( options . cacheKeys && options . cacheKeys [ CacheKey . DownlevelMap ] ) || null ,
433
- ) ;
434
- fs . writeFileSync ( downlevelFilePath + '.map' , downlevelMap ) ;
435
- downlevelCode += `\n//# sourceMappingURL=${ path . basename ( downlevelFilePath ) } .map` ;
436
- }
437
- await cachePut (
438
- downlevelCode ,
439
- ( options . cacheKeys && options . cacheKeys [ CacheKey . DownlevelCode ] ) || null ,
440
- ) ;
441
- fs . writeFileSync ( downlevelFilePath , downlevelCode ) ;
442
-
443
- return result ;
380
+ return {
381
+ original : await processBundle ( {
382
+ ...options ,
383
+ code : originalCode ,
384
+ isOriginal : true ,
385
+ } ) ,
386
+ downlevel : await processBundle ( {
387
+ ...options ,
388
+ code : downlevelCode ,
389
+ filename : options . filename . replace ( / \- e s 2 0 \d { 2 } / , '-es5' ) ,
390
+ isOriginal : false ,
391
+ } ) ,
392
+ } ;
444
393
}
0 commit comments