Skip to content

Commit 3eb5fe7

Browse files
authored
fix: automatically switch to fetch mode when runtime is set (#768)
* fix: automatically switch to fetch mode when runtime is set * fix: improve runtime asset handling when runtime is loaded * fix: optimize log
1 parent 087a721 commit 3eb5fe7

5 files changed

Lines changed: 22 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
See [https://github.com/ice-lab/icestark/releases](https://github.com/ice-lab/icestark/releases) for what has changed in each version of icestark.
44

5+
# 2.8.4
6+
7+
- [fix] automatically switch to "fetch" mode when runtime is set.
8+
- [fix] improve runtime asset handling when runtime is loaded.
9+
510
# 2.8.3
611

712
- [feat] support `runtime.url` as an array for loading multiple types of resources.

packages/icestark/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ice/stark",
3-
"version": "2.8.3",
3+
"version": "2.8.4",
44
"description": "Icestark is a JavaScript library for multiple projects, Ice workbench solution.",
55
"scripts": {
66
"build": "rm -rf lib && tsc",

packages/icestark/src/AppRoute.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ export default class AppRoute extends React.Component<AppRouteProps, AppRouteSta
8686
};
8787
const { loadScriptMode, runtime } = props;
8888

89-
if (loadScriptMode && loadScriptMode !== 'fetch' && runtime) {
90-
console.error('[icestark] runtime option can only be used when loadScriptMode is set to "fetch"');
89+
if (runtime) {
90+
if (loadScriptMode && loadScriptMode !== 'fetch') {
91+
console.error('[icestark] runtime option can only be used when loadScriptMode is set to "fetch"');
92+
} else if (!loadScriptMode) {
93+
console.log('[icestark] Runtime option detected but loadScriptMode not set - automatically switching to "fetch" mode');
94+
}
9195
}
9296
}
9397

packages/icestark/src/apps.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export async function loadAppModule(appConfig: AppConfig) {
218218
const runtimeJsList = [];
219219
// Filter and map runtime libraries that haven't been registered in window
220220
const runtimeLibs = runtime?.filter((config) => {
221-
return config.version && config.library && !window[`${config.library}@${config.version}`];
221+
return config.version && config.library;
222222
}).map((config) => {
223223
// Handle both string and array URL formats
224224
const urls = Array.isArray(config.url) ? config.url : [config.url];
@@ -250,6 +250,7 @@ export async function loadAppModule(appConfig: AppConfig) {
250250
return {
251251
content: mainJs,
252252
type: AssetTypeEnum.RUNTIME,
253+
loaded: Boolean(config.version && config.library && window[`${config.library}@${config.version}`]),
253254
library: config.library,
254255
version: config.version,
255256
};
@@ -374,14 +375,14 @@ function mergeThenUpdateAppConfig(name: string, configuration?: StartConfigurati
374375
return;
375376
}
376377

377-
const { umd, sandbox } = appConfig;
378+
const { umd, sandbox, runtime } = appConfig;
378379

379380
// Generate appSandbox
380381
const appSandbox = createSandbox(sandbox) as Sandbox;
381382

382383
// Merge loadScriptMode
383384
const sandboxEnabled = sandbox && !appSandbox.sandboxDisabled;
384-
const loadScriptMode = appConfig.loadScriptMode ?? (umd || sandboxEnabled ? 'fetch' : 'script');
385+
const loadScriptMode = appConfig.loadScriptMode ?? (umd || sandboxEnabled || (runtime && runtime.length > 0) ? 'fetch' : 'script');
385386

386387
// Merge global configuration
387388
const cfgs = {

packages/icestark/src/util/handleAssets.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export enum AssetCommentEnum {
3333

3434
export interface Asset {
3535
module?: boolean;
36+
loaded?: boolean;
3637
type: AssetTypeEnum;
3738
/** Only used when type is AssetTypeEnum.RUNTIME */
3839
library?: string;
@@ -313,19 +314,20 @@ export function getUrlAssets(urls: string | string[]) {
313314
export async function fetchScripts(jsList: Asset[], fetch: Fetch = defaultFetch): Promise<string[]> {
314315
let jsBeforeRuntime = '';
315316
let jsAfterRuntime = '';
316-
317317
jsList.forEach((asset) => {
318318
if (asset.type === AssetTypeEnum.RUNTIME) {
319319
const { library, version } = asset;
320320
const globalLib = `window['${library}']`;
321321
const backupLib = `window['__${library}__']`;
322322
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`;
323+
const backupCode = `if (${globalLib}) {${backupLib} = ${globalLib};}\n`;
324+
const restoreCode = `if (${backupLib}) {${globalLib} = ${backupLib};${backupLib} = undefined;}\n`;
325+
jsBeforeRuntime = `${jsBeforeRuntime}${backupCode}${asset.loaded ? `${globalLib} = ${versionedLib};` : ''}`;
326+
jsAfterRuntime = `${jsAfterRuntime}${asset.loaded ? '' : `${versionedLib} = ${globalLib};`}${restoreCode}`;
325327
}
326328
});
327329

328-
const result = await Promise.all(jsList.map(async (asset) => {
330+
const result = await Promise.all(jsList.filter((asset) => !asset.loaded).map(async (asset) => {
329331
const { type, content } = asset;
330332
if (type === AssetTypeEnum.INLINE) {
331333
return {

0 commit comments

Comments
 (0)