Skip to content

Commit e3d6f8c

Browse files
authored
docs: add documentation for output.module config (#5923)
1 parent 529ad86 commit e3d6f8c

File tree

6 files changed

+116
-12
lines changed

6 files changed

+116
-12
lines changed

examples/node/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@examples/node",
33
"private": true,
4-
"type": "commonjs",
4+
"type": "module",
55
"scripts": {
66
"build": "rsbuild build",
7-
"start": "rsbuild build && node ./dist/server/index.js"
7+
"start": "rsbuild build && node ./dist/index.js"
88
},
99
"devDependencies": {
1010
"@rsbuild/core": "workspace:*",

examples/node/rsbuild.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ import { defineConfig } from '@rsbuild/core';
33
export default defineConfig({
44
output: {
55
target: 'node',
6+
module: true,
7+
minify: false,
68
},
79
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# output.module
2+
3+
- **Type:** `boolean`
4+
- **Default:** `false`
5+
- **Version:** Added in v1.5.0
6+
7+
Whether to output JavaScript files in ES modules format.
8+
9+
:::tip
10+
11+
- This feature is currently experimental and only available when [output.target](/config/output/target) is `'node'`.
12+
- If you need to build JavaScript libraries in ESM format, we recommend using [Rslib](https://rslib.rs), which is an out-of-the-box library development tool built on top of Rsbuild.
13+
14+
:::
15+
16+
## Example
17+
18+
When building Node.js bundles, Rsbuild outputs CommonJS format by default. You can set `output.module` to `true` to output ES modules format:
19+
20+
```ts title="rsbuild.config.ts"
21+
export default {
22+
output: {
23+
target: 'node',
24+
module: true,
25+
},
26+
};
27+
```
28+
29+
## Running ESM bundles
30+
31+
To properly run ESM bundles in Node.js, you can choose either of the following approaches:
32+
33+
1. Set the `type` field in package.json to `'module'`:
34+
35+
```json title="package.json"
36+
{
37+
"type": "module"
38+
}
39+
```
40+
41+
2. Change the output JavaScript file extension to `.mjs`:
42+
43+
```ts title="rsbuild.config.ts"
44+
export default {
45+
output: {
46+
filename: {
47+
js: '[name].mjs',
48+
},
49+
},
50+
};
51+
```

website/docs/en/config/output/polyfill.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Control the injection mode of the polyfills.
1111

1212
### usage
1313

14-
When `output.polyfill` is configured as `'usage'`, Rsbuild will inject the polyfills based on the APIs used in each file.
14+
When `output.polyfill` is configured as `'usage'`, Rsbuild will inject the polyfills based on the APIs used in each file. This provides optimal bundle size as only needed polyfills are included.
1515

16-
```ts
16+
```ts title="rsbuild.config.ts"
1717
export default {
1818
output: {
1919
polyfill: 'usage',
@@ -23,9 +23,9 @@ export default {
2323

2424
### entry
2525

26-
When `output.polyfill` is configured as `'entry'`, Rsbuild will inject the polyfills in each entry file.
26+
When `output.polyfill` is configured as `'entry'`, Rsbuild will inject the polyfills in each entry file. This ensures all polyfills are available but may increase bundle size.
2727

28-
```ts
28+
```ts title="rsbuild.config.ts"
2929
export default {
3030
output: {
3131
polyfill: 'entry',
@@ -37,7 +37,7 @@ export default {
3737

3838
When `output.polyfill` is configured as `'off'`, Rsbuild will not inject the polyfills, and developers need to ensure code compatibility themselves.
3939

40-
```ts
40+
```ts title="rsbuild.config.ts"
4141
export default {
4242
output: {
4343
polyfill: 'off',
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# output.module
2+
3+
- **类型:** `boolean`
4+
- **默认值:** `false`
5+
- **版本:** 添加于 v1.5.0
6+
7+
是否以 ES 模块格式输出 JavaScript 文件。
8+
9+
:::tip
10+
11+
- 此功能目前为实验性功能,仅在 [output.target](/config/output/target)`'node'` 时可用。
12+
- 如果你需要构建 ESM 格式的 JavaScript 库,推荐使用 [Rslib](https://rslib.rs),它是一个开箱即用的库开发工具,基于 Rsbuild 实现。
13+
14+
:::
15+
16+
## 示例
17+
18+
在构建 Node.js bundles 时,Rsbuild 默认输出 CommonJS 格式的产物,你可以将 `output.module` 设置为 `true` 来输出 ES modules 格式:
19+
20+
```ts title="rsbuild.config.ts"
21+
export default {
22+
output: {
23+
target: 'node',
24+
module: true,
25+
},
26+
};
27+
```
28+
29+
## 运行 ESM 产物
30+
31+
为了在 Node.js 中正确运行 ESM 产物,你可以选择以下任一方式:
32+
33+
1. 将 package.json 的 `type` 字段设置为 `'module'`
34+
35+
```json title="package.json"
36+
{
37+
"type": "module"
38+
}
39+
```
40+
41+
2. 将输出的 JavaScript 文件扩展名改为 `.mjs`
42+
43+
```ts title="rsbuild.config.ts"
44+
export default {
45+
output: {
46+
filename: {
47+
js: '[name].mjs',
48+
},
49+
},
50+
};
51+
```

website/docs/zh/config/output/polyfill.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
### usage
1313

14-
`output.polyfill` 配置为 `'usage'` 时,Rsbuild 会在每个文件中根据代码中使用的 API 注入 polyfills。
14+
`output.polyfill` 配置为 `'usage'` 时,Rsbuild 会在每个文件中根据代码中使用的 API 注入 polyfills。这提供了最优的包体积,因为只包含所需的 polyfills。
1515

16-
```ts
16+
```ts title="rsbuild.config.ts"
1717
export default {
1818
output: {
1919
polyfill: 'usage',
@@ -23,9 +23,9 @@ export default {
2323

2424
### entry
2525

26-
`output.polyfill` 配置为 `'entry'` 时,Rsbuild 会在每个入口文件中注入 polyfills。
26+
`output.polyfill` 配置为 `'entry'` 时,Rsbuild 会在每个入口文件中注入 polyfills。这确保了所有 polyfills 都可用,但可能会增加包体积。
2727

28-
```ts
28+
```ts title="rsbuild.config.ts"
2929
export default {
3030
output: {
3131
polyfill: 'entry',
@@ -37,7 +37,7 @@ export default {
3737

3838
`output.polyfill` 配置为 `'off'` 时,Rsbuild 不会注入 polyfills,开发者需要自行保证代码的兼容性。
3939

40-
```ts
40+
```ts title="rsbuild.config.ts"
4141
export default {
4242
output: {
4343
polyfill: 'off',

0 commit comments

Comments
 (0)