@@ -7,11 +7,11 @@ type FilenameConfig = {
7
7
html? : string ;
8
8
js? : string | Function ;
9
9
css? : string | Function ;
10
- svg? : string ;
11
- font? : string ;
12
- image? : string ;
13
- media? : string ;
14
- assets? : string ;
10
+ svg? : string | Function ;
11
+ font? : string | Function ;
12
+ image? : string | Function ;
13
+ media? : string | Function ;
14
+ assets? : string | Function ;
15
15
};
16
16
```
17
17
@@ -49,7 +49,7 @@ After the production build, Rsbuild will add a hash in the middle of the filenam
49
49
50
50
The following are the details of each filename:
51
51
52
- - ` html ` : The name of the HTML file .
52
+ - ` html ` : The name of the HTML files .
53
53
- ` js ` : The name of the JavaScript files.
54
54
- ` css ` : The name of the CSS files.
55
55
- ` svg ` : The name of the SVG images.
@@ -137,42 +137,72 @@ After specifying the module name as above, the generated file will be `dist/stat
137
137
138
138
## Using function
139
139
140
- 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 dynamically set the filename based on the file information.
141
141
142
142
The function receives two parameters:
143
143
144
144
- ` pathData ` : An object containing file path information.
145
145
- ` assetInfo ` : An optional object containing additional assets information.
146
146
147
+ Dynamically set the filename for JavaScript files:
148
+
147
149
``` ts title="rsbuild.config.ts"
150
+ const isProd = process .env .NODE_ENV === ' production' ;
151
+
148
152
export default {
149
153
output: {
150
154
filename: {
151
155
js : (pathData , assetInfo ) => {
152
156
console .log (pathData ); // You can check the contents of pathData here
153
157
154
158
if (pathData .chunk ?.name === ' index' ) {
155
- const isProd = process .env .NODE_ENV === ' production' ;
156
159
return isProd ? ' [name].[contenthash:8].js' : ' [name].js' ;
157
160
}
158
-
159
161
return ' /some-path/[name].js' ;
160
162
},
163
+ },
164
+ },
165
+ };
166
+ ```
167
+
168
+ Dynamically set the filename for CSS files:
169
+
170
+ ``` ts title="rsbuild.config.ts"
171
+ const isProd = process .env .NODE_ENV === ' production' ;
172
+
173
+ export default {
174
+ output: {
175
+ filename: {
161
176
css : (pathData , assetInfo ) => {
162
177
if (pathData .chunk ?.name === ' index' ) {
163
- const isProd = process .env .NODE_ENV === ' production' ;
164
178
return isProd ? ' [name].[contenthash:8].css' : ' [name].css' ;
165
179
}
166
-
167
180
return ' /some-path/[name].css' ;
168
181
},
169
182
},
170
183
},
171
184
};
172
185
```
173
186
187
+ Dynamically set the filename for image files:
188
+
189
+ ``` ts title="rsbuild.config.ts"
190
+ export default {
191
+ output: {
192
+ filename: {
193
+ image : (pathData ) => {
194
+ if (pathData .filename ?.includes (' foo' )) {
195
+ return ' /foo/[name][ext]' ;
196
+ }
197
+ return ' /bar/[name][ext]' ;
198
+ },
199
+ },
200
+ },
201
+ };
202
+ ```
203
+
174
204
:::tip
175
- Except for ` output.filename.js ` and ` output.filename.css ` , other types of files currently do not support using functions.
205
+ ` output.filename.html ` does not support using functions yet .
176
206
:::
177
207
178
208
## Query hash
0 commit comments