Skip to content

Commit 23bf4c6

Browse files
authored
docs: improve filename configuration (#4655)
1 parent 239255a commit 23bf4c6

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,8 @@
55
```ts
66
type FilenameConfig = {
77
html?: string;
8-
js?:
9-
| string
10-
| ((
11-
pathData: Rspack.PathData,
12-
assetInfo: Rspack.JsAssetInfo | undefined,
13-
) => string);
14-
css?:
15-
| string
16-
| ((
17-
pathData: Rspack.PathData,
18-
assetInfo: Rspack.JsAssetInfo | undefined,
19-
) => string);
8+
js?: string | Function;
9+
css?: string | Function;
2010
svg?: string;
2111
font?: string;
2212
image?: string;
@@ -72,16 +62,27 @@ The following are the details of each filename:
7262
7363
## Example
7464

75-
To set the name of the JavaScript file to `[name]_script.js`, use the following configuration:
65+
To set the name of the JavaScript files to `[name]_script.js`:
66+
67+
```ts title="rsbuild.config.ts"
68+
export default {
69+
output: {
70+
filename: {
71+
js: '[name]_script.js',
72+
},
73+
},
74+
};
75+
```
76+
77+
Set different filenames for development and production builds:
78+
79+
```ts title="rsbuild.config.ts"
80+
const isProd = process.env.NODE_ENV === 'production';
7681

77-
```js
7882
export default {
7983
output: {
8084
filename: {
81-
js:
82-
process.env.NODE_ENV === 'production'
83-
? '[name]_script.[contenthash:8].js'
84-
: '[name]_script.js',
85+
js: isProd ? '[name]_script.[contenthash:8].js' : '[name]_script.js',
8586
},
8687
},
8788
};
@@ -136,9 +137,14 @@ After specifying the module name as above, the generated file will be `dist/stat
136137

137138
## Using function
138139

139-
You can pass a function to `output.filename.js` or `output.filename.css`, allowing you to dynamically generate filenames based on file information:
140+
You can pass a function to `output.filename.js` or `output.filename.css`, allowing you to dynamically generate filenames based on file information.
140141

141-
```js
142+
The function receives two parameters:
143+
144+
- `pathData`: An object containing file path information.
145+
- `assetInfo`: An optional object containing additional assets information.
146+
147+
```ts title="rsbuild.config.ts"
142148
export default {
143149
output: {
144150
filename: {
@@ -173,7 +179,7 @@ Except for `output.filename.js` and `output.filename.css`, other types of files
173179

174180
To generate hash values on the URL query of assets, refer to:
175181

176-
```js
182+
```ts title="rsbuild.config.ts"
177183
const isProd = process.env.NODE_ENV === 'production';
178184

179185
export default {

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@
55
```ts
66
type FilenameConfig = {
77
html?: string;
8-
js?:
9-
| string
10-
| ((
11-
pathData: Rspack.PathData,
12-
assetInfo: Rspack.JsAssetInfo | undefined,
13-
) => string);
14-
css?:
15-
| string
16-
| ((
17-
pathData: Rspack.PathData,
18-
assetInfo: Rspack.JsAssetInfo | undefined,
19-
) => string);
8+
js?: string | Function;
9+
css?: string | Function;
2010
svg?: string;
2111
font?: string;
2212
image?: string;
2313
media?: string;
14+
assets?: string;
2415
};
2516
```
2617

@@ -73,14 +64,25 @@ const prodDefaultFilename = {
7364

7465
修改 JavaScript 文件的名称为 `[name]_script.js`
7566

76-
```js
67+
```ts title="rsbuild.config.ts"
68+
export default {
69+
output: {
70+
filename: {
71+
js: '[name]_script.js',
72+
},
73+
},
74+
};
75+
```
76+
77+
为开发和生产构建设置不同的文件名:
78+
79+
```ts title="rsbuild.config.ts"
80+
const isProd = process.env.NODE_ENV === 'production';
81+
7782
export default {
7883
output: {
7984
filename: {
80-
js:
81-
process.env.NODE_ENV === 'production'
82-
? '[name]_script.[contenthash:8].js'
83-
: '[name]_script.js',
85+
js: isProd ? '[name]_script.[contenthash:8].js' : '[name]_script.js',
8486
},
8587
},
8688
};
@@ -135,9 +137,14 @@ const { add } = await import(
135137

136138
## 使用函数
137139

138-
`output.filename.js``output.filename.css` 可以传入一个函数,这允许你根据文件信息动态生成文件名
140+
`output.filename.js``output.filename.css` 可以传入一个函数,这允许你根据文件信息动态生成文件名
139141

140-
```js
142+
函数接收两个参数:
143+
144+
- `pathData`:一个对象,包含文件路径信息。
145+
- `assetInfo`:一个可选的对象,包含额外的资源信息。
146+
147+
```ts title="rsbuild.config.ts"
141148
export default {
142149
output: {
143150
filename: {
@@ -172,7 +179,7 @@ export default {
172179

173180
如果你需要在资源的 URL query 上生成 hash 值,可以参考以下配置:
174181

175-
```js
182+
```ts title="rsbuild.config.ts"
176183
const isProd = process.env.NODE_ENV === 'production';
177184

178185
export default {

0 commit comments

Comments
 (0)