Skip to content

Commit 336f3d8

Browse files
authored
fix(enhanced): abort the compile process if not find the expose modules (#3373)
1 parent 4fd33fb commit 336f3d8

File tree

13 files changed

+134
-5
lines changed

13 files changed

+134
-5
lines changed

.changeset/shaggy-flowers-cry.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@module-federation/error-codes': patch
3+
'@module-federation/enhanced': patch
4+
'@module-federation/sdk': patch
5+
---
6+
7+
fix(enhanced): abort process if not find expose modules

apps/website-new/docs/en/guide/troubleshooting/_meta.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
"label": "Runtime",
77
"collapsed":true
88
},
9+
{
10+
"type": "dir",
11+
"name": "build",
12+
"label": "Build",
13+
"collapsed":true
14+
},
915
{
1016
"type": "dir",
1117
"name": "type",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import ErrorCodeTitle from '@components/ErrorCodeTitle';
2+
3+
<ErrorCodeTitle code='BUILD-001'/>
4+
5+
## Reason
6+
7+
The Expose module resource could not be found properly.
8+
9+
There are two reasons for this problem:
10+
1. The exposeModules file path set by `exposes` is incorrect and points to a non-existent address.
11+
2. When using `Next.js` or other frameworks with built-in webpack, the webpack address used by MF is incorrect.
12+
13+
## Solutions
14+
15+
There are corresponding solutions for the reasons:
16+
17+
1. Check whether the module file path corresponding to exposes is correct. Pay attention to the case here.
18+
2. Check whether FEDERATION_WEBPACK_PATH is consistent with the webpack address used by the framework. If not, you can check whether the dependency is installed normally, or set process.env.FEDERATION_WEBPACK_PATH to point to the actual webpack address used.

apps/website-new/docs/public/words-map.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,53 @@
156156
}
157157
}
158158
}
159+
},
160+
"exposeModules":{
161+
"id":"exposeModules",
162+
"lang":{
163+
"zh":{
164+
"description":"Module Federation 中 expose 所对应的模块。",
165+
"related_meta":{
166+
"docs":[],
167+
"links":[
168+
{
169+
"id":"webpack-modules-link",
170+
"title":"Webpack modules",
171+
"url":"https://webpack.docschina.org/concepts/modules/"
172+
}]
173+
}
174+
},
175+
"en":{
176+
"description":"The module corresponding to expose in Module Federation.",
177+
"related_meta":{
178+
"docs":[],
179+
"links":[
180+
{
181+
"id":"webpack-modules-link",
182+
"title":"Webpack modules",
183+
"url":"https://webpack.js.org/concepts/modules/#root"
184+
}]
185+
}
186+
}
187+
}
188+
},
189+
"FEDERATION_WEBPACK_PATH":{
190+
"id":"FEDERATION_WEBPACK_PATH",
191+
"lang":{
192+
"zh":{
193+
"description":"Module Federation 实际使用的 webpack 地址。",
194+
"related_meta":{
195+
"docs":[],
196+
"links":[]
197+
}
198+
},
199+
"en":{
200+
"description":"The webpack address actually used by Module Federation.",
201+
"related_meta":{
202+
"docs":[],
203+
"links":[]
204+
}
205+
}
206+
}
159207
}
160208
}

apps/website-new/docs/zh/guide/troubleshooting/_meta.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
"label": "运行时",
77
"collapsed":true
88
},
9+
{
10+
"type": "dir",
11+
"name": "build",
12+
"label": "构建",
13+
"collapsed":true
14+
},
915
{
1016
"type": "dir",
1117
"name": "type",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import ErrorCodeTitle from '@components/ErrorCodeTitle';
2+
3+
<ErrorCodeTitle code='BUILD-001'/>
4+
5+
## 原因
6+
7+
未能正常找到 Expose 模块资源。
8+
9+
该问题原因有两个:
10+
1. `exposes` 设置的 exposeModules 文件路径不正确,指向一个不存在的地址。
11+
2. 使用了 `Next.js` 或其他内置了 webpack 的框架,MF 使用的 webpack 地址与其不对
12+
13+
## 解决方法
14+
15+
针对原因,有对应的解决方法:
16+
17+
1. 检查 exposes 对应的模块文件路径是否正确,此处注意大小写。
18+
2. 检查 FEDERATION_WEBPACK_PATH 与框架使用的 webpack 地址是否一致,如果不对,可以查看依赖是否正常安装,或者设置 process.env.FEDERATION_WEBPACK_PATH 指向实际使用的 webpack 地址。
19+

packages/enhanced/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@module-federation/runtime-tools": "workspace:*",
8989
"@module-federation/manifest": "workspace:*",
9090
"@module-federation/managers": "workspace:*",
91+
"@module-federation/error-codes": "workspace:*",
9192
"@module-federation/dts-plugin": "workspace:*",
9293
"@module-federation/rspack": "workspace:*",
9394
"@module-federation/bridge-react-webpack-plugin": "workspace:*",

packages/enhanced/src/lib/container/ContainerEntryModule.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
'use strict';
77
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
8+
import { logger } from '@module-federation/sdk';
9+
import {
10+
getShortErrorMsg,
11+
buildDescMap,
12+
BUILD_001,
13+
} from '@module-federation/error-codes';
814
import type { containerPlugin } from '@module-federation/sdk';
915
import type { Compilation, Dependency } from 'webpack';
1016
import type {
@@ -229,9 +235,13 @@ class ContainerEntryModule extends Module {
229235

230236
let str;
231237
if (modules.some((m) => !m.module)) {
232-
str = runtimeTemplate.throwMissingModuleErrorBlock({
233-
request: modules.map((m) => m.request).join(', '),
234-
});
238+
logger.error(
239+
getShortErrorMsg(BUILD_001, buildDescMap, {
240+
exposeModules: modules.filter((m) => !m.module),
241+
FEDERATION_WEBPACK_PATH: process.env['FEDERATION_WEBPACK_PATH'],
242+
}),
243+
);
244+
process.exit(1);
235245
} else {
236246
str = `return ${runtimeTemplate.blockPromise({
237247
block,

packages/error-codes/src/desc.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
RUNTIME_007,
99
RUNTIME_008,
1010
TYPE_001,
11+
BUILD_001,
1112
} from './error-codes';
1213

1314
export const runtimeDescMap = {
@@ -25,7 +26,12 @@ export const typeDescMap = {
2526
[TYPE_001]: 'Failed to generate type declaration.',
2627
};
2728

29+
export const buildDescMap = {
30+
[BUILD_001]: 'Failed to find expose module.',
31+
};
32+
2833
export const errorDescMap = {
2934
...runtimeDescMap,
3035
...typeDescMap,
36+
...buildDescMap,
3137
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export const RUNTIME_007 = 'RUNTIME-007';
88
export const RUNTIME_008 = 'RUNTIME-008';
99

1010
export const TYPE_001 = 'TYPE-001';
11+
export const BUILD_001 = 'BUILD-001';

0 commit comments

Comments
 (0)