@@ -39,7 +39,7 @@ export interface ProcessBundleOptions {
3939 optimize ?: boolean ;
4040 optimizeOnly ?: boolean ;
4141 ignoreOriginal ?: boolean ;
42- cacheKeys ?: ( string | null ) [ ] ;
42+ cacheKeys ?: ( string | undefined ) [ ] ;
4343 integrityAlgorithm ?: 'sha256' | 'sha384' | 'sha512' ;
4444 runtimeData ?: ProcessBundleResult [ ] ;
4545 replacements ?: [ string , string ] [ ] ;
@@ -80,9 +80,9 @@ export function setup(data: number[] | { cachePath: string; i18n: I18nOptions })
8080 i18n = options . i18n ;
8181}
8282
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 > {
8484 if ( cachePath && key ) {
85- await cacache . put ( cachePath , key , content , {
85+ await cacache . put ( cachePath , key || null , content , {
8686 metadata : { integrity } ,
8787 } ) ;
8888 }
@@ -114,7 +114,6 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
114114 const codeSize = Buffer . byteLength ( options . code ) ;
115115 const mapSize = options . map ? Buffer . byteLength ( options . map ) : 0 ;
116116 const manualSourceMaps = codeSize >= 500 * 1024 || mapSize >= 500 * 1024 ;
117-
118117 const sourceCode = options . code ;
119118 const sourceMap = options . map ? JSON . parse ( options . map ) : undefined ;
120119
@@ -155,59 +154,21 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
155154 }
156155 }
157156
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-
174157 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+ } ) ;
201165 }
202166
203- // If original was not processed, add info
204167 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+ } ) ;
211172 }
212173
213174 return result ;
@@ -286,41 +247,74 @@ async function mergeSourceMapsFast(first: RawSourceMap, second: RawSourceMap) {
286247 return map ;
287248}
288249
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+ }
295283
296- let mapContent ;
284+ result = {
285+ map : rawMap ,
286+ code,
287+ } ;
288+ }
289+
290+ let mapContent : string | undefined ;
297291 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` ;
300294 }
301295
302296 mapContent = JSON . stringify ( result . map ) ;
303297
304298 await cachePut (
305299 mapContent ,
306- ( options . cacheKeys && options . cacheKeys [ CacheKey . OriginalMap ] ) || null ,
300+ cacheKeys [ isOriginal ? CacheKey . OriginalMap : CacheKey . DownlevelMap ] ,
307301 ) ;
308- fs . writeFileSync ( options . filename + '.map' , mapContent ) ;
302+ fs . writeFileSync ( filepath + '.map' , mapContent ) ;
309303 }
310304
311305 const fileResult = createFileEntry (
312- options . filename ,
306+ filepath ,
313307 result . code ,
314308 mapContent ,
315- options . integrityAlgorithm ,
309+ integrityAlgorithm ,
316310 ) ;
317311
318312 await cachePut (
319313 result . code ,
320- ( options . cacheKeys && options . cacheKeys [ CacheKey . OriginalCode ] ) || null ,
314+ cacheKeys [ isOriginal ? CacheKey . OriginalCode : CacheKey . DownlevelCode ] ,
321315 fileResult . integrity ,
322316 ) ;
323- fs . writeFileSync ( options . filename , result . code ) ;
317+ fs . writeFileSync ( filepath , result . code ) ;
324318
325319 return fileResult ;
326320}
@@ -421,66 +415,19 @@ async function processRuntime(
421415 // Extra spacing is intentional to align source line positions
422416 downlevelCode = downlevelCode . replace ( / " \- e s 2 0 \d { 2 } \. / , ' "-es5.' ) ;
423417
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+ } ;
484431}
485432
486433function createReplacePlugin ( replacements : [ string , string ] [ ] ) : PluginObj {
0 commit comments