@@ -310,37 +310,68 @@ export function getUrlAssets(urls: string | string[]) {
310310 return { jsList, cssList } ;
311311}
312312
313- export function fetchScripts ( jsList : Asset [ ] , fetch : Fetch = defaultFetch ) : Promise < string [ ] > {
314- return Promise . all ( jsList . map ( ( asset ) => {
315- const { type, content, library, version } = asset ;
313+ export async function fetchScripts ( jsList : Asset [ ] , fetch : Fetch = defaultFetch ) : Promise < string [ ] > {
314+ let jsBeforeRuntime = '' ;
315+ let jsAfterRuntime = '' ;
316+
317+ jsList . forEach ( ( asset ) => {
318+ if ( asset . type === AssetTypeEnum . RUNTIME ) {
319+ const { library, version } = asset ;
320+ const globalLib = `window['${ library } ']` ;
321+ const backupLib = `window['__${ library } __']` ;
322+ const versionedLib = `window['${ library } @${ version } ']` ;
323+ jsBeforeRuntime = `${ jsBeforeRuntime } if (${ globalLib } ) {${ backupLib } = ${ globalLib } ;}\n` ;
324+ jsAfterRuntime = `${ jsAfterRuntime } ${ versionedLib } = ${ globalLib } ; if (${ backupLib } ) {${ globalLib } = ${ backupLib } ;${ backupLib } = undefined;}\n` ;
325+ }
326+ } ) ;
327+
328+ const result = await Promise . all ( jsList . map ( async ( asset ) => {
329+ const { type, content } = asset ;
316330 if ( type === AssetTypeEnum . INLINE ) {
317- return content ;
331+ return {
332+ type,
333+ content,
334+ } ;
318335 } else {
319336 const cacheKey = `${ content } ${ type === AssetTypeEnum . RUNTIME ? '?runtime' : '' } ` ;
320337 // content will script url when type is AssetTypeEnum.EXTERNAL
321338 // eslint-disable-next-line no-return-assign
322- return cachedScriptsContent [ cacheKey ]
339+ return {
340+ type,
341+ content : cachedScriptsContent [ cacheKey ]
323342 /**
324343 * If code is being evaluated as a string with `eval` or via `new Function`,then the source origin
325344 * will be the page's origin. As a result, `//# sourceURL` appends to the generated code.
326345 * See https://sourcemaps.info/spec.html
327346 */
328- || ( cachedScriptsContent [ cacheKey ] = fetch ( content )
347+ || ( cachedScriptsContent [ cacheKey ] = await fetch ( content )
329348 . then ( ( res ) => res . text ( ) )
330- . then ( ( text ) => {
331- if ( type === AssetTypeEnum . RUNTIME && version && library ) {
332- const globalLib = `window['${ library } ']` ;
333- const backupLib = `window['__${ library } __']` ;
334- const versionedLib = `window['${ library } @${ version } ']` ;
335- return `;if (${ globalLib } ) {${ backupLib } = ${ globalLib } ;}
336- ${ text } ; ${ versionedLib } = ${ globalLib } ; if (${ backupLib } ) {${ globalLib } = ${ backupLib } ;${ backupLib } = undefined;}`;
337- }
338- return text ;
339- } )
340349 . then ( ( res ) => `${ res } \n //# sourceURL=${ content } ` )
341- ) ;
350+ ) ,
351+ } ;
342352 }
343353 } ) ) ;
354+ const scriptTexts = [ ] ;
355+ let hasInsertedBeforeRuntime = false ;
356+
357+ for ( let i = 0 ; i < result . length ; i ++ ) {
358+ const { type, content } = result [ i ] ;
359+
360+ // Insert jsBeforeRuntime before the first runtime script
361+ if ( type === AssetTypeEnum . RUNTIME && ! hasInsertedBeforeRuntime ) {
362+ scriptTexts . push ( jsBeforeRuntime ) ;
363+ hasInsertedBeforeRuntime = true ;
364+ }
365+ // Add the script content
366+ scriptTexts . push ( content ) ;
367+
368+ // Insert jsAfterRuntime after the runtime script
369+ if ( type === AssetTypeEnum . RUNTIME && result [ i + 1 ] ?. type !== AssetTypeEnum . RUNTIME ) {
370+ scriptTexts . push ( jsAfterRuntime ) ;
371+ }
372+ }
373+
374+ return scriptTexts ;
344375}
345376
346377// for prefetch
0 commit comments