Skip to content

Commit 18c447d

Browse files
committed
refactor
1 parent f18f94b commit 18c447d

File tree

2 files changed

+54
-50
lines changed

2 files changed

+54
-50
lines changed

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,7 @@ import { Stability } from '@components/ApiMeta';
22

33
# ESM output
44

5-
Rspack supports bundling with ESM format output. By default, Rspack generates bundled output that includes Rspack runtime code rather than standard CommonJS or ESM format. You can configure [output.module](/config/output#outputmodule) to generate standard 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.
5+
Rspack supports bundling with ESM format output. By default, Rspack generates bundled output that includes Rspack runtime code rather than standard CommonJS or ESM format. You can configure [output.module](/config/output#outputmodule) to generate standard ESM format output. The following sections introduce how to [build application ESM output](#building-application-esm-output) and [build library ESM output](#building-library-esm-output), and finally list the [configuration checklist](#configuration-checklist) related to ESM output.
306

317
## Building application ESM output
328

@@ -93,3 +69,29 @@ Currently, Rspack's ESM support is still being improved. In certain use cases, y
9369
4. Re-exports of external modules (`export * from '...'`) cannot be properly preserved in ESM format
9470

9571
We are continuously improving Rspack's ESM support to provide a comprehensive solution that addresses existing issues and makes ESM usage more simple and intuitive, better embracing the modern JavaScript module system.
72+
73+
## Configuration Checklist
74+
75+
Below are the main configuration options related to ESM and their descriptions, which are used when building both applications and libraries.
76+
77+
1. [output.module](/config/output#outputmodule): Set to `true` to generate ESM format output. When this option is enabled, it affects the following configurations:
78+
1. Affects [externalsType](/config/externals#externalstype) default value: When `output.module` is `true`, `externalsType` defaults to `'module-import'`
79+
2. Affects [output.filename](/config/output#outputfilename) default value: When `output.module` is `true`, the default filename is `'[name].mjs'` instead of `'[name].js'`
80+
3. Affects [output.chunkFilename](/config/output#outputchunkfilename) default value: When `output.module` is `true`, chunk filename defaults to `'[id].mjs'` instead of `'[id].js'`
81+
4. Affects [output.hotUpdateChunkFilename](/config/output#outputhotupdatechunkfilename) default value: When `output.module` is `true`, defaults to `'[id].[fullhash].hot-update.mjs'`
82+
5. Affects [output.hotUpdateMainFilename](/config/output#outputhotupdatemainfilename) default value: When `output.module` is `true`, defaults to `'[runtime].[fullhash].hot-update.json.mjs'`
83+
6. Affects [output.iife](/config/output#outputiife) default value: When `output.module` is `true`, `output.iife` defaults to `false`
84+
7. Affects [output.library.type](/config/output#outputlibrarytype) default value: When `output.module` is `true`, defaults to `'module'` instead of `'var'`
85+
8. Affects [output.scriptType](/config/output#outputscripttype) default value: When `output.module` is `true`, defaults to `'module'`
86+
9. Affects [output.environment.dynamicImport](/config/output#outputenvironmentdynamicimport) default value: Enabled when `output.module` is `true`
87+
10. Affects [output.environment.dynamicImportInWorker](/config/output#outputenvironmentdynamicimportinworker) default value: Enabled when `output.module` is `true`
88+
11. Affects [output.environment.module](/config/output#outputenvironmentmodule) default value: Enabled when `output.module` is `true`
89+
12. Affects Node.js related configuration defaults: In Node.js environment, when `output.module` is `true`, `__filename` and `__dirname` default to `'node-module'`
90+
2. [output.chunkFormat](/config/output#outputchunkformat): Set to `'module'` to use ESM format.
91+
3. [output.library.type](/config/output#outputlibrarytype): Set to `'modern-module'` for additional optimization of library ESM output format.
92+
4. [output.chunkLoading](/config/output#outputchunkloading): Set to `'import'` to use ESM `import` for loading chunks.
93+
5. [output.workerChunkLoading](/config/output#outputworkerchunkloading): Set to `'import'` to use ESM `import` for loading workers.
94+
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.
95+
7. [optimization.avoidEntryIife](/config/optimization#optimizationavoidentryiife): In some cases, Rspack wraps ESM output in an IIFE, which breaks ESM's modular characteristics.
96+
8. [experiments.outputModule](/config/experiments#experimentsoutputmodule): Enable the experimental feature required for output.module.
97+
9. [HtmlWebpackPlugin.scriptLoading](/guide/tech/html#htmlwebpackplugin): Set to `'module'` to use ESM `<script type="module">` for loading `.mjs` modules.

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,7 @@ import { Stability } from '@components/ApiMeta';
22

33
# ESM 格式产物
44

5-
Rspack 支持打包 ESM 格式的产物。默认情况下,Rspack 会生成类似 CommonJS 格式的产物,但你可以通过配置 `output.module` 来生成 ESM 格式的产物。
6-
7-
下面列出了与 ESM 相关的主要配置选项及其说明,这些配置在构建应用和库时都会用到。
8-
9-
1. [output.module](/config/output#outputmodule): 设置为 `true`,表示生成 ESM 格式的产物。开启此选项后,会对以下配置产生影响:
10-
1. 影响 [externalsType](/config/externals#externalstype) 默认值:当 `output.module``true` 时,`externalsType` 默认为 `'module-import'`
11-
2. 影响 [output.filename](/config/output#outputfilename) 默认值:当 `output.module``true` 时,默认文件名为 `'[name].mjs'` 而不是 `'[name].js'`
12-
3. 影响 [output.chunkFilename](/config/output#outputchunkfilename) 默认值:当 `output.module``true` 时,chunk 文件名默认为 `'[id].mjs'` 而不是 `'[id].js'`
13-
4. 影响 [output.hotUpdateChunkFilename](/config/output#outputhotupdatechunkfilename) 默认值:当 `output.module``true` 时,默认为 `'[id].[fullhash].hot-update.mjs'`
14-
5. 影响 [output.hotUpdateMainFilename](/config/output#outputhotupdatemainfilename) 默认值:当 `output.module``true` 时,默认为 `'[runtime].[fullhash].hot-update.json.mjs'`
15-
6. 影响 [output.iife](/config/output#outputiife) 默认值:当 `output.module``true` 时,`output.iife` 默认为 `false`
16-
7. 影响 [output.library.type](/config/output#outputlibrarytype) 默认值:当 `output.module``true` 时,默认为 `'module'` 而不是 `'var'`
17-
8. 影响 [output.scriptType](/config/output#outputscripttype) 默认值:当 `output.module``true` 时,默认为 `'module'`
18-
9. 影响 [output.environment.dynamicImport](/config/output#outputenvironmentdynamicimport) 默认值:当 `output.module``true` 时会被启用
19-
10. 影响 [output.environment.dynamicImportInWorker](/config/output#outputenvironmentdynamicimportinworker) 默认值:当 `output.module``true` 时会被启用
20-
11. 影响 [output.environment.module](/config/output#outputenvironmentmodule) 默认值:当 `output.module``true` 时会被启用
21-
12. 影响 Node.js 相关配置的默认值:在 Node.js 环境下,当 `output.module``true` 时,`__filename``__dirname` 默认为 `'node-module'`
22-
2. [output.chunkFormat](/config/output#outputchunkformat): 设置为 `'module'`,表示使用 ESM 格式。
23-
3. [output.library.type](/config/output#outputlibrarytype): 设置为 `'modern-module'`,表示对库的 ESM 产物格式进行额外的优化。
24-
4. [output.chunkLoading](/config/output#outputchunkloading): 设置为 `'import'`,表示使用 ESM 的 `import` 来加载 chunk。
25-
5. [output.workerChunkLoading](/config/output#outputworkerchunkloading): 设置为 `'import'`,表示使用 ESM 的 `import` 来加载 worker
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 所需的前置实验特性
29-
9. [HtmlWebpackPlugin.scriptLoading](/guide/tech/html#htmlwebpackplugin): 设置为 `'module'`,表示使用 ESM 的 `<script type="module">` 来加载 `.mjs` 模块。
5+
Rspack 支持打包 ESM 格式的产物。默认情况下,Rspack 会生成类似 CommonJS 格式的产物,但你可以通过配置 [output.module](/config/output#outputmodule) 来生成 ESM 格式的产物。下面的章节介绍了如何 [构建应用的 ESM 格式产物](#构建应用的-esm-格式产物)[构建库的 ESM 格式产物](#构建库的-esm-格式产物) 并在最后列出了与 ESM 产物相关的 [配置清单](#配置清单)
306

317
## 构建应用的 ESM 格式产物
328

@@ -93,3 +69,29 @@ export default {
9369
4. 外部模块的重新导出(`export * from '...'`)在 ESM 格式下无法正确保留
9470

9571
我们正在持续改进 Rspack 的 ESM 支持,致力于提供完善的 ESM 方案,解决现有问题,使 ESM 的使用和配置更加简单直观,更好地拥抱现代 JavaScript 模块系统。
72+
73+
## 配置清单
74+
75+
下面列出了与 ESM 相关的主要配置选项及其说明,这些配置在构建应用和库时都会用到。
76+
77+
1. [output.module](/config/output#outputmodule): 设置为 `true`,表示生成 ESM 格式的产物。开启此选项后,会对以下配置产生影响:
78+
1. 影响 [externalsType](/config/externals#externalstype) 默认值:当 `output.module``true` 时,`externalsType` 默认为 `'module-import'`
79+
2. 影响 [output.filename](/config/output#outputfilename) 默认值:当 `output.module``true` 时,默认文件名为 `'[name].mjs'` 而不是 `'[name].js'`
80+
3. 影响 [output.chunkFilename](/config/output#outputchunkfilename) 默认值:当 `output.module``true` 时,chunk 文件名默认为 `'[id].mjs'` 而不是 `'[id].js'`
81+
4. 影响 [output.hotUpdateChunkFilename](/config/output#outputhotupdatechunkfilename) 默认值:当 `output.module``true` 时,默认为 `'[id].[fullhash].hot-update.mjs'`
82+
5. 影响 [output.hotUpdateMainFilename](/config/output#outputhotupdatemainfilename) 默认值:当 `output.module``true` 时,默认为 `'[runtime].[fullhash].hot-update.json.mjs'`
83+
6. 影响 [output.iife](/config/output#outputiife) 默认值:当 `output.module``true` 时,`output.iife` 默认为 `false`
84+
7. 影响 [output.library.type](/config/output#outputlibrarytype) 默认值:当 `output.module``true` 时,默认为 `'module'` 而不是 `'var'`
85+
8. 影响 [output.scriptType](/config/output#outputscripttype) 默认值:当 `output.module``true` 时,默认为 `'module'`
86+
9. 影响 [output.environment.dynamicImport](/config/output#outputenvironmentdynamicimport) 默认值:当 `output.module``true` 时会被启用
87+
10. 影响 [output.environment.dynamicImportInWorker](/config/output#outputenvironmentdynamicimportinworker) 默认值:当 `output.module``true` 时会被启用
88+
11. 影响 [output.environment.module](/config/output#outputenvironmentmodule) 默认值:当 `output.module``true` 时会被启用
89+
12. 影响 Node.js 相关配置的默认值:在 Node.js 环境下,当 `output.module``true` 时,`__filename``__dirname` 默认为 `'node-module'`
90+
2. [output.chunkFormat](/config/output#outputchunkformat): 设置为 `'module'`,表示使用 ESM 格式。
91+
3. [output.library.type](/config/output#outputlibrarytype): 设置为 `'modern-module'`,表示对库的 ESM 产物格式进行额外的优化。
92+
4. [output.chunkLoading](/config/output#outputchunkloading): 设置为 `'import'`,表示使用 ESM 的 `import` 来加载 chunk。
93+
5. [output.workerChunkLoading](/config/output#outputworkerchunkloading): 设置为 `'import'`,表示使用 ESM 的 `import` 来加载 worker
94+
6. [optimization.concatenateModules](/config/optimization#optimizationconcatenatemodules): modern-module 依赖此选项的开启来保证输出的产物支持良好的 tree-shaking 和库的正确导出
95+
7. [optimization.avoidEntryIife](/config/optimization#optimizationavoidentryiife): 在某些情况下,Rspack 会将 ESM 产物的输出包裹在一个 IIFE 中,这会破坏 ESM 的模块化特性
96+
8. [experiments.outputModule](/config/experiments#experimentsoutputmodule): 开启 output.module 所需的前置实验特性
97+
9. [HtmlWebpackPlugin.scriptLoading](/guide/tech/html#htmlwebpackplugin): 设置为 `'module'`,表示使用 ESM 的 `<script type="module">` 来加载 `.mjs` 模块。

0 commit comments

Comments
 (0)