Skip to content

Commit 01fb893

Browse files
docs: how to generate esm outputs for mf format (#1059)
Co-authored-by: Wei <[email protected]>
1 parent c919ea7 commit 01fb893

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

website/docs/en/guide/advanced/module-federation.mdx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ In this way, we have completed the integration of Rslib Module as a producer. Af
8787

8888
In the above example we added a new `format: 'mf'` , which will help you add an additional Module Federation product, while also configuring the format of `cjs` and `esm` , which does not conflict.
8989

90-
However, if you want this Rslib Module to consume other producers at the same time, do not use the build configuration `remote` parameter, because in other formats, this may cause errors, please refer to the example below using the Module Federation runtime
90+
However, if you want this Rslib Module to consume other producers at the same time, do not use the build configuration `remote` parameter, because in other formats, this may cause errors, please refer to the example below using the Module Federation runtime.
9191

9292
## Develop MF remote module
9393

@@ -313,6 +313,8 @@ This ensures that modules can be loaded as expected in multiple formats.
313313

314314
## FAQs
315315

316+
### How to control the loading strategy of shared dependencies when the producer and consumer build patterns are different
317+
316318
If the Rslib producer is built with build, this means that the `process.env.NODE_ENV` of the producer is `production` . If the consumer is started in dev mode at this time,
317319
due to the shared loading strategy of Module Federation being `version-first` by default, there may be problems loading into different modes of react and react-dom (e.g. react in development mode, react-dom in production mode).
318320
You can set up [shareStrategy](https://module-federation.io/configure/sharestrategy) at the consumer to solve this problem, but make sure you fully understand this configuration
@@ -324,6 +326,29 @@ pluginModuleFederation({
324326
}),
325327
```
326328

329+
### How to make module federated outputs generate export of ES modules
330+
331+
If you want Rslib producers' module federated outputs to generate the export of ES Modules, you can additionally configure as follows:
332+
333+
```ts title='rslib.config.ts'
334+
export default defineConfig({
335+
lib: [
336+
{
337+
format: 'mf',
338+
// ...
339+
// [!code highlight:7]
340+
tools: {
341+
rspack(config) {
342+
config.experiments = {
343+
outputModule: true,
344+
};
345+
},
346+
},
347+
},
348+
],
349+
});
350+
```
351+
327352
## Examples
328353

329354
[Rslib Module Federation Example](https://github.com/web-infra-dev/rslib/tree/main/examples/module-federation)

website/docs/zh/guide/advanced/module-federation.mdx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default defineConfig({
8787

8888
在上面的例子中,我们添加了一个新的 `format: 'mf'` ,它将添加一个额外的模块联邦产物,同时还配置了 `cjs``esm` 的格式,它们是不冲突的。
8989

90-
但是,如果你希望此 Rslib 模块同时消费其他生产者,请不要使用构建配置 `remote` 参数,因为在其他格式下,这可能会导致错误,请参考下面使用 Module Federation 运行时的示例
90+
但是,如果你希望此 Rslib 模块同时消费其他生产者,请不要使用构建配置 `remote` 参数,因为在其他格式下,这可能会导致错误,请参考下面使用 Module Federation 运行时的示例
9191

9292
## 开发 MF 远程模块
9393

@@ -313,6 +313,8 @@ export const Counter: React.FC = () => {
313313

314314
## FAQs
315315

316+
### 生产者与消费者构建模式不同时如何控制共享依赖的加载策略
317+
316318
如果 Rslib 生产者是用 build 构建的, 这意味着生产者中的 `process.env.NODE_ENV``production` 。如果这时消费者是使用的开发模式启动,由于模块联邦默认使用共享的加载策略,可能会有 react 和 react-dom 加载模式不一致的问题 (比如 react 在 development mode, react-dom 在 production mode)。
317319
你可以在消费者设置 [shareStrategy](https://module-federation.io/zh/configure/sharestrategy) 来解决这个问题,这需要你确保已经完全理解了这个配置。
318320

@@ -323,6 +325,29 @@ pluginModuleFederation({
323325
}),
324326
```
325327

328+
### 如何让模块联邦产物生成 ES modules 的导出
329+
330+
如果你希望 Rslib 生产者的模块联邦产物生成 ES Modules 的导出,可以额外配置如下:
331+
332+
```ts title='rslib.config.ts'
333+
export default defineConfig({
334+
lib: [
335+
{
336+
format: 'mf',
337+
// ...
338+
// [!code highlight:7]
339+
tools: {
340+
rspack(config) {
341+
config.experiments = {
342+
outputModule: true,
343+
};
344+
},
345+
},
346+
},
347+
],
348+
});
349+
```
350+
326351
## 示例
327352

328353
[Rslib 模块联邦示例](https://github.com/web-infra-dev/rslib/tree/main/examples/module-federation)

0 commit comments

Comments
 (0)