Skip to content

Commit 9b0a109

Browse files
fi3eworkJSerFeng
andcommitted
Update website/docs/zh/guide/features/esm.mdx
Co-authored-by: Fy <[email protected]>
1 parent 1955ec0 commit 9b0a109

File tree

3 files changed

+116
-21
lines changed

3 files changed

+116
-21
lines changed

website/docs/en/guide/features/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"web-workers",
1010
"lazy-compilation",
1111
"builtin-swc-loader",
12-
"builtin-lightningcss-loader"
12+
"builtin-lightningcss-loader",
13+
"esm"
1314
]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { Stability } from '@components/ApiMeta';
2+
3+
# ESM Output
4+
5+
Rspack supports bundling ESM format output. By default, Rspack generates CommonJS-like output, but you can configure `output.module` to generate ESM format output.
6+
7+
Below are the main configuration options related to ESM and their descriptions, which are used when building both applications and libraries.
8+
9+
1. [output.module](/config/output#outputmodule): Set to `true` to generate ESM format output. When this option is enabled, it affects the following configurations:
10+
1. Affects [externalsType](/config/externals#externalstype) default value: When `output.module` is `true`, `externalsType` defaults to `'module-import'`
11+
2. Affects [output.filename](/config/output#outputfilename) default value: When `output.module` is `true`, the default filename is `'[name].mjs'` instead of `'[name].js'`
12+
3. Affects [output.chunkFilename](/config/output#outputchunkfilename) default value: When `output.module` is `true`, chunk filename defaults to `'[id].mjs'` instead of `'[id].js'`
13+
4. Affects [output.hotUpdateChunkFilename](/config/output#outputhotupdatechunkfilename) default value: When `output.module` is `true`, defaults to `'[id].[fullhash].hot-update.mjs'`
14+
5. Affects [output.hotUpdateMainFilename](/config/output#outputhotupdatemainfilename) default value: When `output.module` is `true`, defaults to `'[runtime].[fullhash].hot-update.json.mjs'`
15+
6. Affects [output.iife](/config/output#outputiife) default value: When `output.module` is `true`, `output.iife` defaults to `false`
16+
7. Affects [output.library.type](/config/output#outputlibrarytype) default value: When `output.module` is `true`, defaults to `'module'` instead of `'var'`
17+
8. Affects [output.scriptType](/config/output#outputscripttype) default value: When `output.module` is `true`, defaults to `'module'`
18+
9. Affects [output.environment.dynamicImport](/config/output#outputenvironmentdynamicimport) default value: Enabled when `output.module` is `true`
19+
10. Affects [output.environment.dynamicImportInWorker](/config/output#outputenvironmentdynamicimportinworker) default value: Enabled when `output.module` is `true`
20+
11. Affects [output.environment.module](/config/output#outputenvironmentmodule) default value: Enabled when `output.module` is `true`
21+
12. Affects Node.js related configuration defaults: In Node.js environment, when `output.module` is `true`, `__filename` and `__dirname` default to `'node-module'`
22+
2. [output.chunkFormat](/config/output#outputchunkformat): Set to `'module'` to use ESM format.
23+
3. [output.library.type](/config/output#outputlibrarytype): Set to `'modern-module'` for additional optimization of library ESM output format.
24+
4. [output.chunkLoading](/config/output#outputchunkloading): Set to `'import'` to use ESM `import` for loading chunks.
25+
5. [output.workerChunkLoading](/config/output#outputworkerchunkloading): Set to `'import'` to use ESM `import` for loading workers.
26+
6. [optimization.concatenateModules](/config/optimization#optimizationconcatenatemodules): modern-module depends on this option being enabled to ensure the output supports good tree-shaking and correct library exports.
27+
7. [optimization.avoidEntryIife](/config/optimization#optimizationavoidentryiife): In some cases, Rspack wraps ESM output in an IIFE, which breaks ESM's modular characteristics.
28+
8. [experiments.outputModule](/config/experiments#experimentsoutputmodule): Enable the experimental feature required for output.module.
29+
9. [HtmlWebpackPlugin.scriptLoading](/guide/tech/html#htmlwebpackplugin): Set to `'module'` to use ESM `<script type="module">` for loading `.mjs` modules.
30+
31+
## Building application ESM output
32+
33+
When building applications, Rspack generates CommonJS-like format output by default. If you need to generate ESM format output, you can configure it in `rspack.config.mjs` as follows:
34+
35+
```js title="rspack.config.mjs"
36+
export default {
37+
//...
38+
output: {
39+
module: true,
40+
chunkFormat: 'module',
41+
chunkLoading: 'import',
42+
workerChunkLoading: 'import',
43+
},
44+
experiments: {
45+
outputModule: true,
46+
},
47+
};
48+
```
49+
50+
You can check out examples of [building React applications to ESM output](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/react-refresh-esm) and [React ESM SSR](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/react-ssr-esm) in rspack-examples.
51+
52+
## Building library ESM output
53+
54+
We recommend using [Rslib](https://rslib.rs) to build library output. Rslib is built on top of Rspack and can build multiple formats including ES Module, CommonJS, and UMD. Rslib provides higher-level APIs that simplify the library building process. Compared to using Rspack directly, using Rslib for library building is more convenient and efficient.
55+
56+
If you need to use Rspack directly to build ESM format output for libraries (npm library), you need to configure the following:
57+
58+
```js title="rspack.config.mjs"
59+
export default {
60+
//...
61+
output: {
62+
module: true,
63+
chunkFormat: 'module',
64+
externalsType: "module-import",
65+
library: {
66+
type: 'modern-module',
67+
}
68+
chunkLoading: 'import',
69+
workerChunkLoading: 'import',
70+
},
71+
optimization: {
72+
concatenateModules: true,
73+
avoidEntryIife: true,
74+
minimize: false,
75+
},
76+
experiments: {
77+
outputModule: true,
78+
},
79+
};
80+
```
81+
82+
You can check out this [rspack-examples](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/library-esm) ESM library example.
83+
84+
Building ESM output also requires handling various detailed issues, which are not listed exhaustively here. You can refer to the [ESM format related configurations in Rslib](https://github.com/web-infra-dev/rslib/blob/f727b18805767b99fb85ae67ebff959aa644536e/packages/core/src/config.ts), or use Rslib directly for out-of-the-box library building.
85+
86+
## Future plans for ESM output
87+
88+
Currently, Rspack's support for ESM is still relatively basic, and ESM output cannot fully replace the current CommonJS output. In many scenarios, ESM output cannot work properly. The main limitations include:
89+
90+
1. Lack of static analysis friendly code splitting support
91+
2. Limited custom chunk splitting support in ESM format
92+
3. Lack of module-level side effect information preservation, affecting tree-shaking accuracy
93+
4. Re-exports of external modules (`export * from '...'`) cannot be properly preserved in ESM format
94+
95+
Rspack will continue to improve ESM support in the future, with the goal of treating ESM output as a first-class citizen, making ESM usage and configuration simpler and more intuitive, and better embracing modern JavaScript module systems.

website/docs/zh/guide/features/esm.mdx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { Stability } from '@components/ApiMeta';
22

33
# ESM 格式产物
44

5-
Rspack 支持打包 ESM 格式的产物。默认情况下,Rspack 会生成 CommonJS 格式的产物,但你可以通过配置 `output.module` 来生成 ESM 格式的产物。
5+
Rspack 支持打包 ESM 格式的产物。默认情况下,Rspack 会生成类似 CommonJS 格式的产物,但你可以通过配置 `output.module` 来生成 ESM 格式的产物。
66

7-
这里先列出与 ESM 相关的配置并解释,这在构建应用和库时都需要用到
7+
下面列出了与 ESM 相关的主要配置选项及其说明,这些配置在构建应用和库时都会用到
88

9-
1. [output.module](/config/output#outputmodule): 设置为 `true`,表示生成 ESM 格式的产物,开启时,会
9+
1. [output.module](/config/output#outputmodule): 设置为 `true`,表示生成 ESM 格式的产物。开启此选项后,会对以下配置产生影响:
1010
1. 影响 [externalsType](/config/externals#externalstype) 默认值:当 `output.module``true` 时,`externalsType` 默认为 `'module-import'`
1111
2. 影响 [output.filename](/config/output#outputfilename) 默认值:当 `output.module``true` 时,默认文件名为 `'[name].mjs'` 而不是 `'[name].js'`
1212
3. 影响 [output.chunkFilename](/config/output#outputchunkfilename) 默认值:当 `output.module``true` 时,chunk 文件名默认为 `'[id].mjs'` 而不是 `'[id].js'`
@@ -20,17 +20,17 @@ Rspack 支持打包 ESM 格式的产物。默认情况下,Rspack 会生成 Com
2020
11. 影响 [output.environment.module](/config/output#outputenvironmentmodule) 默认值:当 `output.module``true` 时会被启用
2121
12. 影响 Node.js 相关配置的默认值:在 Node.js 环境下,当 `output.module``true` 时,`__filename``__dirname` 默认为 `'node-module'`
2222
2. [output.chunkFormat](/config/output#outputchunkformat): 设置为 `'module'`,表示使用 ESM 格式。
23-
3. [output.library.type](/config/output#outputlibrarytype): 设置为 `'modern-module'`,表示对库的 ESM 产物格式进行了额外的优化
23+
3. [output.library.type](/config/output#outputlibrarytype): 设置为 `'modern-module'`,表示对库的 ESM 产物格式进行额外的优化
2424
4. [output.chunkLoading](/config/output#outputchunkloading): 设置为 `'import'`,表示使用 ESM 的 `import` 来加载 chunk。
2525
5. [output.workerChunkLoading](/config/output#outputworkerchunkloading): 设置为 `'import'`,表示使用 ESM 的 `import` 来加载 worker
26-
6. [optimization.concatenateModules](/config/optimization#optimizationconcatenatemodules): modern-module 依赖 concatenateModules 的开启来保证输出的产物支持良好的 tree-shaking 及库最终的正确导出
27-
7. [optimization.avoidEntryIife](/config/optimization#optimizationavoidentryiife): 在某些情况下,Rspack 会将 ESM 产物的输出包裹在一个 IIFE 中,这将破坏 ESM 的模块化特性
28-
8. [experiments.outputModule](/config/experiments#experimentsoutputmodule): 开启 output.module 需要手动开启的前置实验特性
26+
6. [optimization.concatenateModules](/config/optimization#optimizationconcatenatemodules): modern-module 依赖此选项的开启来保证输出的产物支持良好的 tree-shaking 和库的正确导出
27+
7. [optimization.avoidEntryIife](/config/optimization#optimizationavoidentryiife): 在某些情况下,Rspack 会将 ESM 产物的输出包裹在一个 IIFE 中,这会破坏 ESM 的模块化特性
28+
8. [experiments.outputModule](/config/experiments#experimentsoutputmodule): 开启 output.module 所需的前置实验特性
2929
9. [HtmlWebpackPlugin.scriptLoading](/guide/tech/html#htmlwebpackplugin): 设置为 `'module'`,表示使用 ESM 的 `<script type="module">` 来加载 `.mjs` 模块。
3030

3131
## 构建应用的 ESM 格式产物
3232

33-
在构建应用时,Rspack 会默认生成 CommonJS 格式的产物。如果你需要生成 ESM 格式的产物,可以在 `rspack.config.mjs` 中进行如下配置:
33+
在构建应用时,Rspack 默认生成类似 CommonJS 格式的产物。如果你需要生成 ESM 格式的产物,可以在 `rspack.config.mjs` 中进行如下配置:
3434

3535
```js title="rspack.config.mjs"
3636
export default {
@@ -47,13 +47,13 @@ export default {
4747
};
4848
```
4949

50-
你可以查看 [rspack-examples](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/react-refresh-esm) 中这个使用 React 应用构建到 ESM 产物的例子
50+
你可以在 rspack-examples 中查看 [React 应用构建到 ESM 产物](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/react-refresh-esm) [React ESM SSR](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/react-ssr-esm) 的示例
5151

5252
## 构建库的 ESM 格式产物
5353

54-
推荐使用 [Rslib](https://rslib.rs) 来构建库产物,Rslib 底层的实现基于 Rspack,能够构建 ES Module、CommonJS 和 UMD 等多种格式的产物。Rslib 提供了更高层次的 API,简化了库的构建过程。使用 Rslib 进行库的构建会比直接使用 Rspack 更加方便和高效
54+
推荐使用 [Rslib](https://rslib.rs) 来构建库产物,Rslib 底层基于 Rspack 实现,能够构建 ES Module、CommonJS 和 UMD 等多种格式的产物。Rslib 提供了更高层次的 API,简化了库的构建过程。相比直接使用 Rspack,使用 Rslib 进行库的构建会更加方便和高效
5555

56-
当直接使用 Rspack 支持构建库(npm library)的 ESM 格式产物时,需要同时进行以下配置:
56+
如果你需要直接使用 Rspack 构建库(npm library)的 ESM 格式产物,需要同时进行以下配置:
5757

5858
```js title="rspack.config.mjs"
5959
export default {
@@ -71,7 +71,7 @@ export default {
7171
optimization: {
7272
concatenateModules: true,
7373
avoidEntryIife: true,
74-
minimize: false, // 在构建库时禁用代码压缩
74+
minimize: false,
7575
},
7676
experiments: {
7777
outputModule: true,
@@ -81,16 +81,15 @@ export default {
8181

8282
你可以查看 [rspack-examples](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/library-esm) 中这个使用 ESM 库的例子。
8383

84-
要进行 ESM 产物的打包还需要处理各种细节问题,这里不再一一列举,可以参考 [Rslib 中对 ESM 格式的相关配置](https://github.com/web-infra-dev/rslib/blob/f727b18805767b99fb85ae67ebff959aa644536e/packages/core/src/config.ts),或者直接使用 Rslib 开箱即用进行库的构建
84+
进行 ESM 产物的打包还需要处理各种细节问题,这里不逐一列举。你可以参考 [Rslib 中对 ESM 格式的相关配置](https://github.com/web-infra-dev/rslib/blob/f727b18805767b99fb85ae67ebff959aa644536e/packages/core/src/config.ts),或者直接使用 Rslib 开箱即用地进行库的构建
8585

8686
## ESM 产物未来规划
8787

88-
现在 Rspack 对 ESM 的支持还很基本,ESM 的产物还没有达到能够完全替代当前的 CommonJS 产物的程度,在很多场景下 ESM 产物也无法正常工作。包括
88+
目前 Rspack 对 ESM 的支持还比较基础,ESM 产物还无法完全替代当前的 CommonJS 产物,在很多场景下 ESM 产物也无法正常工作。主要的限制包括
8989

90-
1. 没有对静态分析友好的代码分割支持
91-
2. 缺少模块级别的副作用信息保留,影响 tree-shaking 的精确性
92-
3. 缺少对 ESM 产物的 Live Bindings 支持,无法正确处理导出绑定的动态更新
93-
4. 循环依赖处理在 ESM 产物中存在问题
94-
5. 外部模块的重新导出(`export * from '...'`)在 ESM 格式下无法正确保留
90+
1. 缺少对静态分析友好的代码分割支持
91+
2. ESM 格式下的自定义 chunk splitting 支持有限
92+
3. 缺少模块级别的副作用信息保留,影响 tree-shaking 的精确性
93+
4. 外部模块的重新导出(`export * from '...'`)在 ESM 格式下无法正确保留
9594

96-
Rspack 未来会继续完善对 ESM 的支持,目标是将 ESM 产物视为一等公民,使 ESM 的使用及配置更加简单和直观,更好地拥抱现代 JavaScript 模块系统。
95+
Rspack 未来会继续完善对 ESM 的支持,目标是将 ESM 产物视为一等公民,使 ESM 的使用和配置更加简单直观,更好地拥抱现代 JavaScript 模块系统。

0 commit comments

Comments
 (0)