Skip to content

Commit 2562d28

Browse files
committed
fix: refactor fetchScripts for improved runtime asset handling
1 parent abebebc commit 2562d28

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ See [https://github.com/ice-lab/icestark/releases](https://github.com/ice-lab/ic
55
# 2.8.3
66

77
- [feat] support `runtime.url` as an array for loading multiple types of resources.
8+
- [fix] refactor fetchScripts for improved runtime asset handling.
89

910
# 2.8.2
1011

packages/icestark/src/util/handleAssets.ts

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)