Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: output.filename.css allow function #3752

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions e2e/cases/filename-function/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export default defineConfig({

return '/some-path/[name].js';
},
css: (pathData) => {
if (pathData.chunk?.name === 'index') {
return 'my-index.css';
}
return '/some-path/[name].css';
},
},
},
});
7 changes: 6 additions & 1 deletion packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ export function getFilename(
): NonNullable<FilenameConfig['js']>;
export function getFilename(
config: NormalizedConfig | NormalizedEnvironmentConfig,
type: Exclude<keyof FilenameConfig, 'js'>,
type: 'css',
isProd: boolean,
): NonNullable<FilenameConfig['css']>;
export function getFilename(
config: NormalizedConfig | NormalizedEnvironmentConfig,
type: Exclude<keyof FilenameConfig, 'js' | 'css'>,
isProd: boolean,
isServer?: boolean,
): string;
Expand Down
16 changes: 14 additions & 2 deletions packages/core/src/plugins/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ export const pluginOutput = (): RsbuildPlugin => ({

const cssPath = config.output.distPath.css;
const cssFilename = getFilename(config, 'css', isProd);
const isCssFilenameFn = typeof cssFilename === 'function';

const cssAsyncPath =
config.output.distPath.cssAsync ??
(cssPath ? `${cssPath}/async` : 'async');
Expand All @@ -149,8 +151,18 @@ export const pluginOutput = (): RsbuildPlugin => ({
.plugin(CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT)
.use(getCssExtractPlugin(), [
{
filename: posix.join(cssPath, cssFilename),
chunkFilename: posix.join(cssAsyncPath, cssFilename),
filename: isCssFilenameFn
? (...args) => {
const name = cssFilename(...args);
return posix.join(cssPath, name);
}
: posix.join(cssPath, cssFilename),
chunkFilename: isCssFilenameFn
? (...args) => {
const name = cssFilename(...args);
return posix.join(cssAsyncPath, name);
}
: posix.join(cssAsyncPath, cssFilename),
...extractPluginOptions,
},
]);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ export type FilenameConfig = {
* - dev: '[name].css'
* - prod: '[name].[contenthash:8].css'
*/
css?: string;
css?: NonNullable<Rspack.Configuration['output']>['cssFilename'];
/**
* The name of the SVG images.
* @default '[name].[contenthash:8].svg'
Expand Down
10 changes: 9 additions & 1 deletion website/docs/en/config/output/filename.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ After specifying the module name as above, the generated file will be `dist/stat

## Using Function

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

```js
export default {
Expand All @@ -144,6 +144,14 @@ export default {

return '/some-path/[name].js';
},
css: (pathData, assetInfo) => {
if (pathData.chunk?.name === 'index') {
const isProd = process.env.NODE_ENV === 'production';
return isProd ? '[name].[contenthash:8].css' : '[name].css';
}

return '/some-path/[name].css';
},
},
},
};
Expand Down
14 changes: 12 additions & 2 deletions website/docs/zh/config/output/filename.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ type FilenameConfig = {
js?:
| string
| ((pathData: Rspack.PathData, assetInfo: Rspack.JsAssetInfo) => string);
css?: string;
css?:
| string
| ((pathData: Rspack.PathData, assetInfo: Rspack.JsAssetInfo) => string);
svg?: string;
font?: string;
image?: string;
Expand Down Expand Up @@ -127,7 +129,7 @@ const { add } = await import(

## 使用函数

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

```js
export default {
Expand All @@ -143,6 +145,14 @@ export default {

return '/some-path/[name].js';
},
css: (pathData, assetInfo) => {
if (pathData.chunk?.name === 'index') {
const isProd = process.env.NODE_ENV === 'production';
return isProd ? '[name].[contenthash:8].css' : '[name].css';
}

return '/some-path/[name].css';
},
},
},
};
Expand Down
Loading