Skip to content

Commit 6692b42

Browse files
authored
feat: output.filename.css allow function (#3752)
1 parent bb81fb0 commit 6692b42

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

e2e/cases/filename-function/rsbuild.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export default defineConfig({
1010

1111
return '/some-path/[name].js';
1212
},
13+
css: (pathData) => {
14+
if (pathData.chunk?.name === 'index') {
15+
return 'my-index.css';
16+
}
17+
return '/some-path/[name].css';
18+
},
1319
},
1420
},
1521
});

packages/core/src/helpers/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,12 @@ export function getFilename(
221221
): NonNullable<FilenameConfig['js']>;
222222
export function getFilename(
223223
config: NormalizedConfig | NormalizedEnvironmentConfig,
224-
type: Exclude<keyof FilenameConfig, 'js'>,
224+
type: 'css',
225+
isProd: boolean,
226+
): NonNullable<FilenameConfig['css']>;
227+
export function getFilename(
228+
config: NormalizedConfig | NormalizedEnvironmentConfig,
229+
type: Exclude<keyof FilenameConfig, 'js' | 'css'>,
225230
isProd: boolean,
226231
isServer?: boolean,
227232
): string;

packages/core/src/plugins/output.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ export const pluginOutput = (): RsbuildPlugin => ({
141141

142142
const cssPath = config.output.distPath.css;
143143
const cssFilename = getFilename(config, 'css', isProd);
144+
const isCssFilenameFn = typeof cssFilename === 'function';
145+
144146
const cssAsyncPath =
145147
config.output.distPath.cssAsync ??
146148
(cssPath ? `${cssPath}/async` : 'async');
@@ -149,8 +151,18 @@ export const pluginOutput = (): RsbuildPlugin => ({
149151
.plugin(CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT)
150152
.use(getCssExtractPlugin(), [
151153
{
152-
filename: posix.join(cssPath, cssFilename),
153-
chunkFilename: posix.join(cssAsyncPath, cssFilename),
154+
filename: isCssFilenameFn
155+
? (...args) => {
156+
const name = cssFilename(...args);
157+
return posix.join(cssPath, name);
158+
}
159+
: posix.join(cssPath, cssFilename),
160+
chunkFilename: isCssFilenameFn
161+
? (...args) => {
162+
const name = cssFilename(...args);
163+
return posix.join(cssAsyncPath, name);
164+
}
165+
: posix.join(cssAsyncPath, cssFilename),
154166
...extractPluginOptions,
155167
},
156168
]);

packages/core/src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ export type FilenameConfig = {
674674
* - dev: '[name].css'
675675
* - prod: '[name].[contenthash:8].css'
676676
*/
677-
css?: string;
677+
css?: NonNullable<Rspack.Configuration['output']>['cssFilename'];
678678
/**
679679
* The name of the SVG images.
680680
* @default '[name].[contenthash:8].svg'

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ After specifying the module name as above, the generated file will be `dist/stat
128128

129129
## Using Function
130130

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

133133
```js
134134
export default {
@@ -144,6 +144,14 @@ export default {
144144

145145
return '/some-path/[name].js';
146146
},
147+
css: (pathData, assetInfo) => {
148+
if (pathData.chunk?.name === 'index') {
149+
const isProd = process.env.NODE_ENV === 'production';
150+
return isProd ? '[name].[contenthash:8].css' : '[name].css';
151+
}
152+
153+
return '/some-path/[name].css';
154+
},
147155
},
148156
},
149157
};

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ type FilenameConfig = {
88
js?:
99
| string
1010
| ((pathData: Rspack.PathData, assetInfo: Rspack.JsAssetInfo) => string);
11-
css?: string;
11+
css?:
12+
| string
13+
| ((pathData: Rspack.PathData, assetInfo: Rspack.JsAssetInfo) => string);
1214
svg?: string;
1315
font?: string;
1416
image?: string;
@@ -127,7 +129,7 @@ const { add } = await import(
127129

128130
## 使用函数
129131

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

132134
```js
133135
export default {
@@ -143,6 +145,14 @@ export default {
143145

144146
return '/some-path/[name].js';
145147
},
148+
css: (pathData, assetInfo) => {
149+
if (pathData.chunk?.name === 'index') {
150+
const isProd = process.env.NODE_ENV === 'production';
151+
return isProd ? '[name].[contenthash:8].css' : '[name].css';
152+
}
153+
154+
return '/some-path/[name].css';
155+
},
146156
},
147157
},
148158
};

0 commit comments

Comments
 (0)