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: splitChunks.cacheGroups.filename supports function #8779

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ export interface RawCacheGroupOptions {
key: string
priority?: number
test?: RegExp | string | Function
filename?: string
filename?: JsFilename
idHint?: string
/** What kind of chunks should be selected. */
chunks?: RegExp | 'async' | 'initial' | 'all'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use self::raw_split_chunk_cache_group_test::RawCacheGroupTest;
use self::raw_split_chunk_chunks::{create_chunks_filter, Chunks};
use self::raw_split_chunk_name::default_chunk_option_name;
use self::raw_split_chunk_size::RawSplitChunkSizes;
use crate::JsFilename;

#[napi(object, object_to_js = false)]
#[derive(Debug)]
Expand Down Expand Up @@ -63,7 +64,7 @@ pub struct RawCacheGroupOptions {
#[napi(ts_type = "RegExp | string | Function")]
#[debug(skip)]
pub test: Option<RawCacheGroupTest>,
pub filename: Option<String>,
pub filename: Option<JsFilename>,
// pub enforce: bool,
pub id_hint: Option<String>,
/// What kind of chunks should be selected.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import "./shared1";
import "./common1";

it("should be able to load the split chunk on demand (shared)", () => {
return import(/* webpackChunkName: "theName" */ "./shared2");
});

it("should be able to load the split chunk on demand (common)", () => {
return Promise.all([
import(/* webpackChunkName: "otherName1" */ "./common2"),
import(/* webpackChunkName: "otherName2" */ "./common3")
]);
});

it("should have files", () => {
const files = require("fs").readdirSync(__dirname);
expect(files).toContain("a.js");
expect(files).toContain("b.js");
expect(files).toContain("common-common1_js.js");
expect(files).toContain("common-common2_js.js");
expect(files).toContain("common-common3_js.js");
expect(files).toContain("shared-shared-shared1_js.js");
expect(files).toContain("shared-shared-shared2_js.js");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "./shared1";
import "./shared2";
import "./common1";
import "./common2";
import "./common3";
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
mode: "development",
entry: {
a: "./a",
b: "./b"
},
output: {
filename: "[name].js",
libraryTarget: "commonjs2"
},
optimization: {
chunkIds: "named",
splitChunks: {
cacheGroups: {
shared: {
chunks: "all",
test: /shared/,
filename: (pathData, assetInfo) => {
expect(pathData).toBeDefined()
expect(typeof assetInfo).toBe('object')
return "shared-[name].js"
},
enforce: true
},
common: {
chunks: "all",
test: /common/,
enforce: true
}
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("../../../..").TConfigCaseConfig} */
module.exports = {
findBundle: function (i, options) {
return ["a.js"];
}
};
20 changes: 10 additions & 10 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4128,7 +4128,7 @@ export type OptimizationSplitChunksCacheGroup = {
test?: string | RegExp | ((module: Module) => unknown);
priority?: number;
enforce?: boolean;
filename?: string;
filename?: Filename;
reuseExistingChunk?: boolean;
type?: string | RegExp;
idHint?: string;
Expand Down Expand Up @@ -6968,13 +6968,13 @@ export const rspackOptions: z.ZodObject<{
test: z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>, z.ZodFunction<z.ZodTuple<[z.ZodType<Module, z.ZodTypeDef, Module>], z.ZodUnknown>, z.ZodUnknown>]>>;
priority: z.ZodOptional<z.ZodNumber>;
enforce: z.ZodOptional<z.ZodBoolean>;
filename: z.ZodOptional<z.ZodString>;
filename: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodFunction<z.ZodTuple<[z.ZodType<PathData, z.ZodTypeDef, PathData>, z.ZodOptional<z.ZodType<JsAssetInfo, z.ZodTypeDef, JsAssetInfo>>], z.ZodUnknown>, z.ZodString>]>>;
reuseExistingChunk: z.ZodOptional<z.ZodBoolean>;
type: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>>;
idHint: z.ZodOptional<z.ZodString>;
}, "strict", z.ZodTypeAny, {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
type?: string | RegExp | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
Expand All @@ -6994,7 +6994,7 @@ export const rspackOptions: z.ZodObject<{
idHint?: string | undefined;
}, {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
type?: string | RegExp | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
Expand Down Expand Up @@ -7043,7 +7043,7 @@ export const rspackOptions: z.ZodObject<{
defaultSizeTypes?: string[] | undefined;
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
type?: string | RegExp | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
Expand Down Expand Up @@ -7086,7 +7086,7 @@ export const rspackOptions: z.ZodObject<{
defaultSizeTypes?: string[] | undefined;
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
type?: string | RegExp | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
Expand Down Expand Up @@ -7166,7 +7166,7 @@ export const rspackOptions: z.ZodObject<{
defaultSizeTypes?: string[] | undefined;
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
type?: string | RegExp | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
Expand Down Expand Up @@ -7232,7 +7232,7 @@ export const rspackOptions: z.ZodObject<{
defaultSizeTypes?: string[] | undefined;
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
type?: string | RegExp | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
Expand Down Expand Up @@ -8855,7 +8855,7 @@ export const rspackOptions: z.ZodObject<{
defaultSizeTypes?: string[] | undefined;
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
type?: string | RegExp | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
Expand Down Expand Up @@ -9457,7 +9457,7 @@ export const rspackOptions: z.ZodObject<{
defaultSizeTypes?: string[] | undefined;
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
type?: string | RegExp | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ export type OptimizationSplitChunksCacheGroup = {
enforce?: boolean;

/** Allows to override the filename when and only when it's an initial chunk. */
filename?: string;
filename?: Filename;

/**
* Whether to reuse existing chunks when possible.
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ const optimizationSplitChunksCacheGroup = z.strictObject({
.optional(),
priority: z.number().optional(),
enforce: z.boolean().optional(),
filename: z.string().optional(),
filename: filename.optional(),
reuseExistingChunk: z.boolean().optional(),
type: z.string().or(z.instanceof(RegExp)).optional(),
idHint: z.string().optional(),
Expand Down
6 changes: 5 additions & 1 deletion website/docs/en/plugins/webpack/split-chunks-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ Sets the hint for chunk id. It will be added to chunk's filename.

#### splitChunks.cacheGroups.\{cacheGroup\}.filename

- **Type:** `string`
- **Type:** `string | function`

Allows to override the filename when and only when it's an initial chunk. All placeholders available in output.filename are also available here.

Expand All @@ -337,6 +337,10 @@ module.exports = {
cacheGroups: {
defaultVendors: {
filename: 'vendors-[name].js',
// or
filename: (pathData, assetInfo) => {
return `${pathData.chunk.name}-bundle.js`;
},
},
},
},
Expand Down
6 changes: 5 additions & 1 deletion website/docs/zh/plugins/webpack/split-chunks-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ module.exports = {

#### splitChunks.cacheGroups.\{cacheGroup\}.filename

- **类型:** `string`
- **类型:** `string | function`

仅在初始 chunk 时才允许覆盖文件名。 也可以在 output.filename 中使用所有占位符。

Expand All @@ -327,6 +327,10 @@ module.exports = {
cacheGroups: {
defaultVendors: {
filename: 'vendors-[name].js',
// or
filename: (pathData, assetInfo) => {
return `${pathData.chunk.name}-bundle.js`;
},
},
},
},
Expand Down
Loading