Skip to content

Commit d3852cc

Browse files
authored
docs: improve mf deployment docs (#7189)
1 parent c7476bf commit d3852cc

File tree

4 files changed

+170
-22
lines changed

4 files changed

+170
-22
lines changed
Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# Deployment
22

3-
Typically, deploying a Module Federation application requires adjusting the remote module address on the consumer side to its online address.
3+
In general, when deploying a Module Federation application, there are two key points to consider:
44

5-
For example, if the producer is deployed to the domain `https://my-remote-module`, you can modify the consumer's `module-federation.config.ts` file as follows:
5+
1. Ensure that the remote module addresses in the consumer's configuration file are correct, and that the consumer can correctly access the producer's `manifest` file.
6+
2. Ensure that all resources in the producer's `manifest` file can be accessed correctly.
7+
8+
We recommend using Modern.js's [Node Server](/guides/basic-features/deploy.html#using-modernjs-built-in-nodejs-server) to deploy Module Federation applications for an out-of-the-box experience.
9+
10+
## Consumer
11+
12+
For the consumer of Module Federation, its connection with the producer is the remote module address in the configuration file.
13+
14+
For example, if the producer is deployed under the domain `https://my-remote-module`, the developer needs to modify the consumer's configuration file:
615

716
```ts title="module-federation.config.ts"
817
import { createModuleFederationConfig } from '@module-federation/modern-js';
918

1019
export default createModuleFederationConfig({
1120
name: 'host',
1221
remotes: {
13-
remote: 'remote@http://my-remote-module/mf-manifest.json',
22+
remote: 'remote@https://my-remote-module/static/mf-manifest.json',
1423
},
1524
shared: {
1625
react: { singleton: true },
@@ -19,10 +28,77 @@ export default createModuleFederationConfig({
1928
});
2029
```
2130

22-
At this point, the consumer will load the `manifest` configuration file of the `remote` module in the production environment.
31+
At this point, the consumer will load the `manifest` configuration file of the remote module production environment.
32+
33+
:::note
34+
In the above code, the address of the remote module is `/static/mf-manifest.json`, which is just an example using the default output path of Modern.js. In actual projects, developers need to configure according to the actual access path.
35+
:::
36+
37+
## Producer
38+
39+
For the producer of Module Federation, developers need to correctly configure the [`output.assetPrefix`](/configure/app/output/asset-prefix) configuration, which affects:
40+
41+
1. The `publicPath` defined in `mf-manifest.json`, which determines the access path of other resources of the remote module.
42+
2. The access path of the `mf-manifest.json` file when hosted directly by the Modern.js server.
43+
44+
In the production environment, developers need to configure `output.assetPrefix` as the access path of the production environment. For example, if we deploy the producer under the domain `https://my-remote-module`, we need to configure `output.assetPrefix` as `https://my-remote-module`.
45+
46+
```ts title="modern.config.ts"
47+
import { defineConfig } from '@modern-js/app-tools';
48+
49+
export default defineConfig({
50+
output: {
51+
assetPrefix: 'https://my-remote-module',
52+
},
53+
});
54+
```
55+
56+
At this point, the `publicPath` defined in the producer's build output `mf-manifest.json` is `https://my-remote-module`, for example:
57+
58+
```json
59+
{
60+
"id": "remote",
61+
"name": "remote",
62+
"metaData": {
63+
"name": "remote",
64+
"publicPath": "https://my-remote-module/"
65+
},
66+
"shared": [ /* xxx */ ],
67+
"remotes": [],
68+
"exposes": [ /* xxx */ ]
69+
}
70+
```
71+
72+
When the consumer accesses the remote module, it will automatically prepend the `publicPath` to the resource path of the remote module.
73+
74+
This configuration will also affect the access path of the producer's `mf-manifest.json`. For example, if this value is set to `MyDomain/module-a`, the hosting path of `mf-manifest.json` becomes `MyDomain/module-a/static/mf-manifest.json`.
75+
76+
At this point, the consumer needs to configure the following address when configuring the remote module:
77+
78+
```ts title="module-federation.config.ts"
79+
import { createModuleFederationConfig } from '@module-federation/modern-js';
80+
81+
export default createModuleFederationConfig({
82+
name: 'host',
83+
remotes: {
84+
remote: 'remote@MyDomain/module-a/static/mf-manifest.json',
85+
},
86+
});
87+
```
88+
89+
## Local Deployment Verification
2390

24-
## Deployment via Platform
91+
Modern.js provides the `modern deploy` command, which can easily generate products that can run in a Node.js environment.
2592

26-
The above deployment method is merely the simplest practice. In real-world scenarios, there are many constraints, such as version management, release sequencing, and more. Within ByteDance, we have set up a deployment process for Module Federation applications on our deployment platform, which helps developers address these issues.
93+
```bash
94+
modern deploy
95+
```
96+
97+
After executing the command, you can see the following output in the console:
98+
99+
```bash
100+
Static directory: .output/static
101+
You can preview this build by node .output/index
102+
```
27103

28-
We will continue to keep an eye on platforms with similar functionalities in the community and, in the future, enhance the documentation for deploying Modern.js + Module Federation on these types of platforms.
104+
At this point, the developer only needs to run `node .output/index` to preview the effect locally. Whether it is a CSR or an SSR application, all Module Federation files can be accessed correctly.

packages/document/main-doc/docs/en/guides/topic-detail/module-federation/usage.mdx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ If we want to simulate the production environment in local, but not configure `o
190190
import { appTools, defineConfig } from '@modern-js/app-tools';
191191
import { moduleFederationPlugin } from '@module-federation/modern-js';
192192

193-
const isLocal = process.env.IS_LOCAL === 'true';
194-
195193
// https://modernjs.dev/en/configure/app/usage
196194
export default defineConfig({
197195
server: {
@@ -204,7 +202,7 @@ export default defineConfig({
204202
// Now this configuration is only used in the local when you run modern serve command.
205203
// If you want to deploy the application to the platform, use your own domain name.
206204
// Module federation will automatically write it to mf-manifest.json, which influences consumer to fetch remoteEntry.js.
207-
assetPrefix: isLocal ? 'http://127.0.0.1:3051' : '/',
205+
assetPrefix: 'http://127.0.0.1:3051',
208206
},
209207
plugins: [
210208
appTools({
@@ -215,7 +213,7 @@ export default defineConfig({
215213
});
216214
```
217215

218-
Now, in the producer, run `IS_LOCAL=true modern build && modern serve`, and in the consumer, run `modern build && modern serve` to simulate the production environment locally and access the remote modules.
216+
Now, in the producer, run `modern build && modern serve`, and in the consumer, run `modern build && modern serve` to simulate the production environment locally and access the remote modules.
219217

220218
You can refer to this example: [Modern.js & Module Federation Basic Example](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/base).
221219

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# 部署
22

3-
通常情况下,部署 Module Federation 应用,需要在消费者上将远程模块地址,调整为其线上地址。
3+
通常情况下,部署 Module Federation 应用,需要注意两点:
44

5-
例如已经将生产者部署到 `https://my-remote-module` 这个域名下,可以这样修改消费者的 `module-federation.config.ts` 文件:
5+
1. 保证消费者配置文件中的远程模块地址无误,消费者能够正确访问到生产者的 `manifest` 文件。
6+
2. 保证生产者 `manifest` 文件中各个资源能被正确访问到。
7+
8+
我们推荐使用 Modern.js 的 [Node 服务](/guides/basic-features/deploy.html#modernjs-内置-nodejs-服务器)来部署 Module Federation 应用,以获得开箱即用的体验。
9+
10+
## 消费者
11+
12+
对于 Module Federation 的消费者来说,它与生产者的联系就是在配置文件中的远程模块地址。
13+
14+
例如生产者部署在 `https://my-remote-module` 这个域名下,开发者需要修改消费者的配置文件:
615

716
```ts title="module-federation.config.ts"
817
import { createModuleFederationConfig } from '@module-federation/modern-js';
918

1019
export default createModuleFederationConfig({
1120
name: 'host',
1221
remotes: {
13-
remote: 'remote@http://my-remote-module/mf-manifest.json',
22+
remote: 'remote@https://my-remote-module/static/mf-manifest.json',
1423
},
1524
shared: {
1625
react: { singleton: true },
@@ -19,10 +28,77 @@ export default createModuleFederationConfig({
1928
});
2029
```
2130

22-
此时,消费者将加载生产环境的 `remote` 模块的 `manifest` 配置文件。
31+
此时,消费者将加载远程模块生产环境的 `manifest` 配置文件。
32+
33+
:::note
34+
上述代码中,远程模块的地址是 `/static/mf-manifest.json`,这只是以 Modern.js 默认的产物路径举例。在实际项目中,开发者需要根据实际的访问路径进行配置。
35+
:::
36+
37+
## 生产者
38+
39+
对于 Module Federation 的生产者,开发者需要正确的配置 [`output.assetPrefix`](/configure/app/output/asset-prefix) 配置,它会影响到:
40+
41+
1. `mf-manifest.json` 中定义的 `publicPath`,它决定了远程模块其他资源的访问路径。
42+
2. 通过 Modern.js 服务直接托管产物时,`mf-manifest.json` 文件的访问路径。
43+
44+
在生产环境,开发者需要将 `output.assetPrefix` 配置为生产环境的访问路径。例如我们将生产者部署在 `https://my-remote-module` 这个域名下,需要将 `output.assetPrefix` 配置为 `https://my-remote-module`
45+
46+
```ts title="modern.config.ts"
47+
import { defineConfig } from '@modern-js/app-tools';
48+
49+
export default defineConfig({
50+
output: {
51+
assetPrefix: 'https://my-remote-module',
52+
},
53+
});
54+
```
55+
56+
此时,生产者构建产物 `mf-manifest.json` 中定义的 `publicPath``https://my-remote-module`,例如:
57+
58+
```json
59+
{
60+
"id": "remote",
61+
"name": "remote",
62+
"metaData": {
63+
"name": "remote",
64+
"publicPath": "https://my-remote-module/"
65+
},
66+
"shared": [ /* xxx */ ],
67+
"remotes": [],
68+
"exposes": [ /* xxx */ ]
69+
}
70+
```
71+
72+
消费者在访问远程模块时,会自动将 `publicPath` 拼接在远程模块的资源路径前。
73+
74+
该配置也会影响到生产者 `mf-manifest.json` 的访问路径。例如将这个值设置为 `MyDomain/module-a` 时,`mf-manifest.json` 的托管路径变为 `MyDomain/module-a/static/mf-manifest.json`
75+
76+
此时,消费者在配置远程模块时,需要配置以下地址:
77+
78+
```ts title="module-federation.config.ts"
79+
import { createModuleFederationConfig } from '@module-federation/modern-js';
80+
81+
export default createModuleFederationConfig({
82+
name: 'host',
83+
remotes: {
84+
remote: 'remote@MyDomain/module-a/static/mf-manifest.json',
85+
},
86+
});
87+
```
88+
89+
## 本地验证部署
2390

24-
## 通过平台部署
91+
Modern.js 提供了 `modern deploy` 命令,可以方便生成可运行在 Node.js 环境的产物。
2592

26-
上述部署方式只是最简单的实践,在真实场景还有很多限制,例如版本管理、发布时序等。在字节跳动内部,我们在部署平台上搭建了 Module Federation 应用的部署流程,可以帮助开发者解决这些问题。
93+
```bash
94+
modern deploy
95+
```
96+
97+
执行命令后,可以在控制台看到以下输出:
98+
99+
```bash
100+
Static directory: .output/static
101+
You can preview this build by node .output/index
102+
```
27103

28-
我们会持续关注社区里类似功能的平台,在未来完善 Modern.js + Module Federation 在这些类平台上的部署文档
104+
此时,开发者只需要运行 `node .output/index` 即可在本地预览部署后的效果。无论是 CSR 应用或是 SSR 应用,所有的 Module Federation 产物都能够被正确的访问

packages/document/main-doc/docs/zh/guides/topic-detail/module-federation/usage.mdx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ export default Index;
189189
import { appTools, defineConfig } from '@modern-js/app-tools';
190190
import { moduleFederationPlugin } from '@module-federation/modern-js';
191191

192-
const isLocal = process.env.IS_LOCAL === 'true';
193-
194192
// https://modernjs.dev/en/configure/app/usage
195193
export default defineConfig({
196194
server: {
@@ -203,7 +201,7 @@ export default defineConfig({
203201
// Now this configuration is only used in the local when you run modern serve command.
204202
// If you want to deploy the application to the platform, use your own domain name.
205203
// Module federation will automatically write it to mf-manifest.json, which influences consumer to fetch remoteEntry.js.
206-
assetPrefix: isLocal ? 'http://127.0.0.1:3051' : '/',
204+
assetPrefix: 'http://127.0.0.1:3051',
207205
},
208206
plugins: [
209207
appTools({
@@ -214,7 +212,7 @@ export default defineConfig({
214212
});
215213
```
216214

217-
现在,在生产者中运行 `IS_LOCAL=true modern build && modern serve`,在消费者中运行 `modern build && modern serve`,即可在本地模拟生产环境,访问到远程模块。
215+
现在,在生产者中运行 `modern build && modern serve`,在消费者中运行 `modern build && modern serve`,即可在本地模拟生产环境,访问到远程模块。
218216

219217
上述用例可以参考:[Modern.js & Module Federation 基础用法示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/base)
220218

0 commit comments

Comments
 (0)