Skip to content

Commit b5c8b8c

Browse files
authored
docs: update mf usage docs, add static dir (#6926)
1 parent f1cd095 commit b5c8b8c

File tree

4 files changed

+103
-11
lines changed

4 files changed

+103
-11
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
4646

4747
export default createModuleFederationConfig({
4848
name: 'remote',
49-
filename: 'remoteEntry.js',
49+
manifest: {
50+
filePath:'static',
51+
},
52+
filename: 'static/remoteEntry.js',
5053
exposes: {
5154
'./app': './src/export-App.tsx',
5255
},
@@ -109,4 +112,4 @@ You can refer to the example here: [Modern.js & Module Federation Application-Le
109112

110113
## Related Documentation
111114

112-
- [Module Federation Bridge](https://module-federation.io/zh/practice/bridge/index.html)
115+
- [Module Federation Bridge](https://module-federation.io/zh/practice/bridge/index.html)

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

+47-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
5454

5555
export default createModuleFederationConfig({
5656
name: 'remote',
57-
filename: 'remoteEntry.js',
57+
manifest: {
58+
filePath: 'static',
59+
},
60+
filename: 'static/remoteEntry.js',
5861
exposes: {
5962
'./Button': './src/components/Button.tsx',
6063
},
@@ -65,6 +68,10 @@ export default createModuleFederationConfig({
6568
});
6669
```
6770

71+
:::tip
72+
In the above code block, we have prefixed both the manifest and remoteEntry.js exported by Module Federation with `static`. This is because Modern.js requires all resources that need to be exposed to be placed in the `static/` directory, and Modern.js's server will only host the `static/` directory in production environments.
73+
:::
74+
6875
Additionally, modify `modern.config.ts` to provide a development environment port for the producer, allowing the consumer to access the producer's resources through this port:
6976

7077
```ts title="modern.config.ts"
@@ -99,7 +106,7 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
99106
export default createModuleFederationConfig({
100107
name: 'host',
101108
remotes: {
102-
remote: 'remote@http://localhost:3051/mf-manifest.json',
109+
remote: 'remote@http://localhost:3051/static/mf-manifest.json',
103110
},
104111
shared: {
105112
react: { singleton: true },
@@ -173,8 +180,45 @@ After modifying the producer's code, the consumer will automatically fetch the p
173180

174181
Access `http://localhost:8080/remote`, and you will see that the page includes the `Button` component from the producer's remote module.
175182

183+
We can also execute `modern serve` locally to simulate the production environment.
184+
185+
Because the Module Federation plugin will automatically read Modern.js's `output.assetPrefix` configuration as the access address for remote modules, and this value defaults to `/` after building in the production environment.
186+
187+
If we want to simulate the production environment in local, but not configure `output.assetPrefix`, consumers will pull the entry file of the remote module from their own domain. So We can add the following configuration:
188+
189+
```ts
190+
import { appTools, defineConfig } from '@modern-js/app-tools';
191+
import { moduleFederationPlugin } from '@module-federation/modern-js';
192+
193+
const isLocal = process.env.IS_LOCAL === 'true';
194+
195+
// https://modernjs.dev/en/configure/app/usage
196+
export default defineConfig({
197+
server: {
198+
port: 3051,
199+
},
200+
runtime: {
201+
router: true,
202+
},
203+
output: {
204+
// Now this configuration is only used in the local when you run modern serve command.
205+
// If you want to deploy the application to the platform, use your own domain name.
206+
// 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' : '/',
208+
},
209+
plugins: [
210+
appTools({
211+
bundler: 'rspack', // Set to 'webpack' to enable webpack
212+
}),
213+
moduleFederationPlugin(),
214+
],
215+
});
216+
```
217+
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.
219+
176220
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).
177221

178222
## Related Documentation
179223

180-
- [Module Federation Official Documentation](https://module-federation.io/zh/guide/framework/modernjs.html)
224+
- [Module Federation Official Documentation](https://module-federation.io/zh/guide/framework/modernjs.html)

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
4646

4747
export default createModuleFederationConfig({
4848
name: 'remote',
49-
filename: 'remoteEntry.js',
49+
manifest: {
50+
filePath:'static',
51+
},
52+
filename: 'static/remoteEntry.js',
5053
exposes: {
5154
'./app': './src/export-App.tsx',
5255
},
@@ -109,4 +112,4 @@ export default RemoteApp;
109112

110113
## 相关文档
111114

112-
- [Module Federation Bridge](https://module-federation.io/zh/practice/bridge/index.html)
115+
- [Module Federation Bridge](https://module-federation.io/zh/practice/bridge/index.html)

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

+46-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
5555

5656
export default createModuleFederationConfig({
5757
name: 'remote',
58-
filename: 'remoteEntry.js',
58+
manifest: {
59+
filePath: 'static',
60+
},
61+
filename: 'static/remoteEntry.js',
5962
exposes: {
6063
'./Button': './src/components/Button.tsx',
6164
},
@@ -66,6 +69,10 @@ export default createModuleFederationConfig({
6669
});
6770
```
6871

72+
:::tip
73+
在上述代码块中,我们为 Module Federation 导出的 manifest 和 remoteEntry.js 都设置了 `static` 前缀,这是因为 Modern.js 要求将所有需要暴露的资源都放在 `static/` 目录下,Modern.js 的服务器在生产环境时也只会托管 `static/` 目录。
74+
:::
75+
6976
另外,我们还需要修改 `modern.config.ts`,为生产者提供一个开发环境的端口,让消费者可以通过此端口访问生产者的资源:
7077

7178
```ts title="modern.config.ts"
@@ -100,7 +107,7 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
100107
export default createModuleFederationConfig({
101108
name: 'host',
102109
remotes: {
103-
remote: 'remote@http://localhost:3051/mf-manifest.json',
110+
remote: 'remote@http://localhost:3051/static/mf-manifest.json',
104111
},
105112
shared: {
106113
react: { singleton: true },
@@ -142,7 +149,7 @@ export default Index;
142149
}
143150
}
144151
}
145-
```
152+
```
146153

147154
:::tip
148155
在消费者中,我们通过 `remote/Button` 来引用远程模块。这里简单介绍下这个路径具体代表了什么,可以先将它抽象成 `[remoteAlias]/[remoteExpose]`
@@ -174,8 +181,43 @@ export default Index;
174181

175182
访问 `http://localhost:8080/remote`,可以看到页面中已经包含了生产者的远程模块 `Button` 组件。
176183

184+
我们也可以在本地执行 `modern serve` 来模拟生产环境。
185+
186+
因为 Module Federation 插件会自动读取 Modern.js 的 `output.assetPrefix` 配置作为远程模块的访问地址,而该值在生产环境下构建后默认是 `/`。如果不做特殊处理,消费者将从自己的域名下拉取远程模块的入口文件。我们可以添加如下配置:
187+
188+
```ts
189+
import { appTools, defineConfig } from '@modern-js/app-tools';
190+
import { moduleFederationPlugin } from '@module-federation/modern-js';
191+
192+
const isLocal = process.env.IS_LOCAL === 'true';
193+
194+
// https://modernjs.dev/en/configure/app/usage
195+
export default defineConfig({
196+
server: {
197+
port: 3051,
198+
},
199+
runtime: {
200+
router: true,
201+
},
202+
output: {
203+
// Now this configuration is only used in the local when you run modern serve command.
204+
// If you want to deploy the application to the platform, use your own domain name.
205+
// 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' : '/',
207+
},
208+
plugins: [
209+
appTools({
210+
bundler: 'rspack', // Set to 'webpack' to enable webpack
211+
}),
212+
moduleFederationPlugin(),
213+
],
214+
});
215+
```
216+
217+
现在,在生产者中运行 `IS_LOCAL=true modern build && modern serve`,在消费者中运行 `modern build && modern serve`,即可在本地模拟生产环境,访问到远程模块。
218+
177219
上述用例可以参考:[Modern.js & Module Federation 基础用法示例](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/base)
178220

179221
## 相关文档
180222

181-
- [Module Federation 官方文档](https://module-federation.io/zh/guide/framework/modernjs.html)
223+
- [Module Federation 官方文档](https://module-federation.io/zh/guide/framework/modernjs.html)

0 commit comments

Comments
 (0)