Skip to content

Commit 24bf84d

Browse files
authored
feat(runtime-core): add RUNTIME-010 error code and handling for name option inconsistency (#4493)
1 parent 5a4f503 commit 24bf84d

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

.changeset/dry-pants-say.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/runtime-core': patch
3+
'@module-federation/error-codes': patch
4+
---
5+
6+
feat(runtime-core): add RUNTIME-010 error code and handling for name option inconsistency

apps/website-new/docs/en/guide/troubleshooting/runtime.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This page collects runtime-related error codes and common troubleshooting guidan
1717
- [RUNTIME-007](#runtime-007)
1818
- [RUNTIME-008](#runtime-008)
1919
- [RUNTIME-009](#runtime-009)
20+
- [RUNTIME-010](#runtime-010)
2021

2122
## RUNTIME-001
2223

@@ -161,6 +162,19 @@ Not use build plugin, but directly call runtime api.
161162

162163
<Runtime />
163164

165+
## RUNTIME-010
166+
167+
<ErrorCodeTitle code='RUNTIME-010'/>
168+
169+
### Reason
170+
171+
`initOptions` was called with a `name` different from the one used during initialization. The `name` is the unique identifier of the instance and cannot be changed after initialization.
172+
173+
### Solution
174+
175+
- If you need an instance with a different `name`, use the `createInstance` API to create a new instance instead of modifying the existing one.
176+
- If changing the `name` is not required, omit the `name` field in the `initOptions` call or keep it consistent with the value used during initialization.
177+
164178
## Common Issues (No Error Code)
165179

166180
### Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

apps/website-new/docs/zh/guide/troubleshooting/runtime.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Runtime from '@components/zh/runtime/index';
1717
- [RUNTIME-007](#runtime-007)
1818
- [RUNTIME-008](#runtime-008)
1919
- [RUNTIME-009](#runtime-009)
20+
- [RUNTIME-010](#runtime-010)
2021

2122
## RUNTIME-001
2223

@@ -160,6 +161,19 @@ Runtime 运行时资源加载失败报错,可能原因为网络不稳定导致
160161

161162
<Runtime />
162163

164+
## RUNTIME-010
165+
166+
<ErrorCodeTitle code='RUNTIME-010'/>
167+
168+
### 原因
169+
170+
在实例已初始化后,调用 `initOptions` 时传入了与初始化时不同的 `name``name` 是实例的唯一标识,初始化后不可更改。
171+
172+
### 解决方法
173+
174+
- 若需要使用不同 `name` 的实例,请使用 `createInstance` API 创建新实例,而非修改现有实例的 `name`
175+
- 若无需更改 `name`,则在调用 `initOptions` 时去掉 `name` 字段或保持与初始化时一致。
176+
163177
## 常见问题(无错误码)
164178

165179
### Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

packages/error-codes/src/desc.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
RUNTIME_007,
99
RUNTIME_008,
1010
RUNTIME_009,
11+
RUNTIME_010,
1112
TYPE_001,
1213
BUILD_001,
1314
BUILD_002,
@@ -23,6 +24,8 @@ export const runtimeDescMap = {
2324
[RUNTIME_007]: 'Failed to get remote snapshot.',
2425
[RUNTIME_008]: 'Failed to load script resources.',
2526
[RUNTIME_009]: 'Please call createInstance first.',
27+
[RUNTIME_010]:
28+
'The name option cannot be changed after initialization. If you want to create a new instance with a different name, please use "createInstance" api.',
2629
};
2730

2831
export const typeDescMap = {

packages/error-codes/src/error-codes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const RUNTIME_006 = 'RUNTIME-006';
77
export const RUNTIME_007 = 'RUNTIME-007';
88
export const RUNTIME_008 = 'RUNTIME-008';
99
export const RUNTIME_009 = 'RUNTIME-009';
10+
export const RUNTIME_010 = 'RUNTIME-010';
1011

1112
export const TYPE_001 = 'TYPE-001';
1213
export const BUILD_001 = 'BUILD-001';

packages/runtime-core/src/core.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import {
1818
RemoteEntryInitOptions,
1919
CallFrom,
2020
} from './type';
21-
import { getBuilderId, registerPlugins, getRemoteEntry } from './utils';
21+
import { getBuilderId, registerPlugins, getRemoteEntry, error } from './utils';
22+
import {
23+
getShortErrorMsg,
24+
RUNTIME_010,
25+
runtimeDescMap,
26+
} from '@module-federation/error-codes';
2227
import { Module } from './module';
2328
import {
2429
AsyncHook,
@@ -202,6 +207,9 @@ export class ModuleFederation {
202207
}
203208

204209
initOptions(userOptions: UserOptions): Options {
210+
if (userOptions.name && userOptions.name !== this.options.name) {
211+
error(getShortErrorMsg(RUNTIME_010, runtimeDescMap));
212+
}
205213
this.registerPlugins(userOptions.plugins);
206214
const options = this.formatOptions(this.options, userOptions);
207215

packages/runtime-core/src/utils/load.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ async function loadEntryScript({
156156
throw e;
157157
});
158158
}
159-
160159
async function loadEntryDom({
161160
remoteInfo,
162161
remoteEntryExports,

0 commit comments

Comments
 (0)