Skip to content

Commit 2046e00

Browse files
docs: introduce the different default values between Rslib and Rsbuild (#651)
Co-authored-by: Wei <[email protected]>
1 parent 8d65f37 commit 2046e00

File tree

4 files changed

+194
-2
lines changed

4 files changed

+194
-2
lines changed

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ Set the size threshold to inline static assets such as images and fonts.
3232

3333
Set the directory of the dist files. Rsbuild will output files to the corresponding subdirectory according to the file type.
3434

35+
By default, Rslib sets `output.distPath` to:
36+
37+
```ts
38+
const defaultDistPath = {
39+
root: 'dist',
40+
js: './',
41+
jsAsync: './',
42+
css: './',
43+
cssAsync: './',
44+
svg: 'static/svg',
45+
font: 'static/font',
46+
wasm: 'static/wasm',
47+
image: 'static/image',
48+
media: 'static/media',
49+
assets: 'static/assets',
50+
};
51+
```
52+
3553
{/* ## output.emitAssets <RsbuildDocBadge path="/config/output/emit-assets" text="output.emitAssets" /> */}
3654

3755
{/* Control whether to emit static assets such as images, fonts, audio, video, etc. */}
@@ -44,16 +62,24 @@ Whether to emit CSS to the output bundles.
4462

4563
At build time, prevent some `import` dependencies from being packed into bundles in your code, and instead fetch them externally at runtime.
4664

65+
In bundle mode, Rslib will automatically add the dependencies listed in the `dependencies`, `optionalDependencies`, and `peerDependencies` fields of `package.json` to `output.externals`. See [lib.autoExternal](/config/lib/auto-external) for more information.
66+
67+
:::note
4768
It is important to note that `output.externals` differs from [resolve.alias](/config/rsbuild/resolve#resolvealias). Check out [resolve.alias](/config/rsbuild/resolve#resolvealias) documentation for more information.
69+
:::
4870

4971
## output.filenameHash <RsbuildDocBadge path="/config/output/filename-hash" text="output.filenameHash" />
5072

5173
Whether to add a hash value to the filename after the production build.
5274

75+
Rslib sets `output.filenameHash` to `false` by default.
76+
5377
## output.filename <RsbuildDocBadge path="/config/output/filename" text="output.filename" />
5478

5579
Sets the filename of dist files.
5680

81+
By default, Rslib sets `output.filename` based on [format](/config/lib/format), see [autoExtension](/config/lib/auto-extension) for more information.
82+
5783
{/* ## output.injectStyles <RsbuildDocBadge path="/config/output/inject-styles" text="output.injectStyles" /> */}
5884

5985
{/* Whether to inject styles into DOM. */}
@@ -78,10 +104,74 @@ Whether to generate a manifest file that contains information of all assets, and
78104

79105
Configure whether to enable code minification in production mode, or to configure minimizer options.
80106

107+
When `output.minify` is not specified, Rslib will use a sane default value.
108+
109+
- When format is `esm`, `cjs` and `umd`, only dead code elimination and unused code elimination will be performed. The default value is:
110+
111+
```ts
112+
export default defineConfig({
113+
output: {
114+
minify: {
115+
js: true,
116+
css: false,
117+
jsOptions: {
118+
minimizerOptions: {
119+
mangle: false,
120+
minify: false,
121+
compress: {
122+
defaults: false,
123+
unused: true,
124+
dead_code: true,
125+
toplevel: true,
126+
},
127+
format: {
128+
comments: 'some',
129+
preserve_annotations: true,
130+
},
131+
},
132+
},
133+
},
134+
},
135+
});
136+
```
137+
138+
- When format is `mf`, since MF assets are loaded over the network, which means they will not be compressed by the application project. Therefore, they need to be minified in Rslib. The default value is:
139+
140+
```ts
141+
export default defineConfig({
142+
output: {
143+
minify: {
144+
js: true,
145+
css: false,
146+
jsOptions: {
147+
minimizerOptions: {
148+
mangle: false,
149+
// Enable minification
150+
minify: true,
151+
compress: {
152+
defaults: false,
153+
unused: true,
154+
dead_code: true,
155+
// Avoid remoteEntry's global variable being tree-shaken
156+
toplevel: false,
157+
},
158+
format: {
159+
comments: 'some',
160+
preserve_annotations: true,
161+
},
162+
},
163+
},
164+
},
165+
},
166+
});
167+
```
168+
81169
## output.overrideBrowserslist <RsbuildDocBadge path="/config/output/override-browserslist" text="output.overrideBrowserslist" />
82170

83171
Specifies the range of target browsers that the project is compatible with.
84172

173+
Rslib will generate `output.overrideBrowserslist` based on [syntax](/config/lib/syntax), see [ESX_TO_BROWSERSLIST](https://github.com/web-infra-dev/rslib/blob/8d65f3728d60254bcf1a8e24d72902ad79dae959/packages/core/src/utils/syntax.ts#L42-L153) to get the mapping value.
174+
85175
## output.polyfill <RsbuildDocBadge path="/config/output/polyfill" text="output.polyfill" />
86176

87177
Through the `output.polyfill` option, you can control the injection mode of the polyfills.
@@ -98,6 +188,8 @@ Used to set whether to generate source map files, and which format of source map
98188

99189
Setting the build target of Rsbuild.
100190

191+
Rslib sets `output.target` to `node` by default.
192+
101193
:::info
102194
Check out the [Solution](/guide/solution/) to learn more about the build target.
103195
:::

website/docs/en/config/rsbuild/source.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ Include additional files that should be treated as static assets.
1313

1414
Used to configure the decorators syntax.
1515

16+
If [experimentalDecorators](https://www.typescriptlang.org/tsconfig/#experimentalDecorators) is enabled in `tsconfig.json`, Rslib will set `source.decorators.version` to `legacy` by default.
17+
1618
## source.define <RsbuildDocBadge path="/config/source/define" text="source.define" />
1719

1820
Replaces variables in your code with other values or expressions at compile time. This can be useful for allowing different behavior between development builds and production builds.
1921

2022
## source.entry <RsbuildDocBadge path="/config/source/entry" text="source.entry" />
2123

22-
Used to set the entry modules for building. In Rslib, the default value of `source.entry` is:
24+
Used to set the entry modules for building.
25+
26+
In Rslib, the default value is:
2327

2428
- bundle mode: `src/index.(ts|js|tsx|jsx|mjs|cjs)`
2529
- bundleless mode: `src/**`

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
2828

2929
设置构建产物的输出目录,Rsbuild 会根据产物的类型输出到对应的子目录下。
3030

31+
Rslib 默认将 `output.distPath` 设置为:
32+
33+
```ts
34+
const defaultDistPath = {
35+
root: 'dist',
36+
js: './',
37+
jsAsync: './',
38+
css: './',
39+
cssAsync: './',
40+
svg: 'static/svg',
41+
font: 'static/font',
42+
wasm: 'static/wasm',
43+
image: 'static/image',
44+
media: 'static/media',
45+
assets: 'static/assets',
46+
};
47+
```
48+
3149
## output.emitCss <RsbuildDocBadge path="/config/output/emit-css" text="output.emitCss" />
3250

3351
是否将 CSS 输出到产物中。
@@ -36,16 +54,24 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
3654

3755
在构建时,防止将代码中某些 `import` 的依赖包打包到 bundle 中,而是在运行时再去从外部获取这些依赖。
3856

57+
在 bundle 模式下,Rslib 会默认将 `package.json``dependencies``optionalDependencies``peerDependencies` 字段下的三方依赖添加到 `output.externals` 中, 查看 [lib.autoExternal](/config/lib/auto-external) 了解更多信息。
58+
59+
:::note
3960
需要注意的是,`output.externals`[resolve.alias](/config/rsbuild/resolve#resolvealias) 有所不同。请查看 [resolve.alias](/config/rsbuild/resolve#resolvealias) 文档以了解更多信息。
61+
:::
4062

4163
## output.filenameHash <RsbuildDocBadge path="/config/output/filename-hash" text="output.filenameHash" />
4264

4365
在生产模式构建后,是否在产物的文件名中添加 hash 值。
4466

67+
Rslib 默认将 `output.filenameHash` 设置为 `false`
68+
4569
## output.filename <RsbuildDocBadge path="/config/output/filename" text="output.filename" />
4670

4771
设置构建产物的名称。
4872

73+
Rslib 默认会根据 [format](/config/lib/format) 设置 `output.filename`,详情可查看 [autoExtension](/config/lib/auto-extension)
74+
4975
## output.inlineScripts <RsbuildDocBadge path="/config/output/inline-scripts" text="output.inlineScripts" />
5076

5177
用来控制是否用 `<script>` 标签将产物中的 script 文件(.js 文件)inline 到 HTML 中。
@@ -66,10 +92,74 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
6692

6793
用于设置是否在生产模式下开启代码压缩,或是配置压缩工具的选项。
6894

95+
在未指定 `output.minify` 时,Rslib 会使用一个合理的默认值。
96+
97+
- 在 format 为 `esm`, `cjs` 以及 `umd` 时,只会执行死代码消除和未使用代码消除,默认值为:
98+
99+
```ts
100+
export default defineConfig({
101+
output: {
102+
minify: {
103+
js: true,
104+
css: false,
105+
jsOptions: {
106+
minimizerOptions: {
107+
mangle: false,
108+
minify: false,
109+
compress: {
110+
defaults: false,
111+
unused: true,
112+
dead_code: true,
113+
toplevel: true,
114+
},
115+
format: {
116+
comments: 'some',
117+
preserve_annotations: true,
118+
},
119+
},
120+
},
121+
},
122+
},
123+
});
124+
```
125+
126+
- 在 format 为 `mf` 时,由于 MF 资源通过网络加载,这意味着它们不会被应用项目压缩。因此,需要在 Rslib 中对它们进行压缩。默认值为:
127+
128+
```ts
129+
export default defineConfig({
130+
output: {
131+
minify: {
132+
js: true,
133+
css: false,
134+
jsOptions: {
135+
minimizerOptions: {
136+
mangle: false,
137+
// 启用压缩
138+
minify: true,
139+
compress: {
140+
defaults: false,
141+
unused: true,
142+
dead_code: true,
143+
// 避免 remoteEntry 的全局变量被 tree-shaking
144+
toplevel: false,
145+
},
146+
format: {
147+
comments: 'some',
148+
preserve_annotations: true,
149+
},
150+
},
151+
},
152+
},
153+
},
154+
});
155+
```
156+
69157
## output.overrideBrowserslist <RsbuildDocBadge path="/config/output/override-browserslist" text="output.overrideBrowserslist" />
70158

71159
指定项目兼容的目标浏览器范围。
72160

161+
Rslib 默认会根据 [syntax](/config/lib/syntax) 生成 `output.overrideBrowserslist`,查看 [ESX_TO_BROWSERSLIST](https://github.com/web-infra-dev/rslib/blob/8d65f3728d60254bcf1a8e24d72902ad79dae959/packages/core/src/utils/syntax.ts#L42-L153) 获取映射值。
162+
73163
## output.polyfill <RsbuildDocBadge path="/config/output/polyfill" text="output.polyfill" />
74164

75165
控制 polyfills 的注入方式。
@@ -86,6 +176,8 @@ Rsbuild 的 `output.polyfill` 会将 polyfills 注入到全局作用域中,这
86176

87177
用于设置 Rsbuild 的构建产物类型。
88178

179+
Rslib 默认将 `output.target` 设置为 `node`
180+
89181
:::info
90182
请查看 [解决方案](/guide/solution/) 了解更多关于构建产物的信息。
91183
:::

website/docs/zh/config/rsbuild/source.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
1212

1313
用于配置装饰器语法。
1414

15+
如果在 `tsconfig.json` 中启用了 [experimentalDecorators](https://www.typescriptlang.org/tsconfig/#experimentalDecorators),Rslib 会默认将 `source.decorators.version` 设置为 `legacy`
16+
1517
## source.define <RsbuildDocBadge path="/config/source/define" text="source.define" />
1618

1719
构建时将代码中的变量替换成其它值或者表达式,可以用于在代码逻辑中区分开发模式与生产模式等场景。
1820

1921
## source.entry <RsbuildDocBadge path="/config/source/entry" text="source.entry" />
2022

21-
用于设置构建的入口模块。在 Rslib 中,`source.entry` 的默认值为:
23+
用于设置构建的入口模块。
24+
25+
在 Rslib 中,默认值为:
2226

2327
- bundle 模式:`src/index.(ts|js|tsx|jsx|mjs|cjs)`
2428
- bundleless 模式:`src/**`

0 commit comments

Comments
 (0)