Skip to content

Commit

Permalink
feat: splitChunks.cacheGroups.filename supports function
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Dec 19, 2024
1 parent 0e749f0 commit c022d42
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 16 deletions.
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 @@ -10,6 +10,7 @@ use napi::{Either, JsString};
use napi_derive::napi;
use raw_split_chunk_name::normalize_raw_chunk_name;
use raw_split_chunk_name::RawChunkOptionName;
use rspack_binding_values::JsFilename;
use rspack_core::Filename;
use rspack_core::SourceType;
use rspack_core::DEFAULT_DELIMITER;
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 @@ -4131,7 +4131,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 @@ -6973,14 +6973,14 @@ 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;
type?: string | RegExp | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
defaultSizeTypes?: string[] | undefined;
Expand All @@ -7000,7 +7000,7 @@ export const rspackOptions: z.ZodObject<{
}, {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
type?: string | RegExp | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
defaultSizeTypes?: string[] | undefined;
Expand Down Expand Up @@ -7049,7 +7049,7 @@ export const rspackOptions: z.ZodObject<{
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
type?: string | RegExp | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
defaultSizeTypes?: string[] | undefined;
Expand Down Expand Up @@ -7092,7 +7092,7 @@ export const rspackOptions: z.ZodObject<{
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
type?: string | RegExp | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
defaultSizeTypes?: string[] | undefined;
Expand Down Expand Up @@ -7167,7 +7167,7 @@ export const rspackOptions: z.ZodObject<{
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
type?: string | RegExp | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
defaultSizeTypes?: string[] | undefined;
Expand Down Expand Up @@ -7233,7 +7233,7 @@ export const rspackOptions: z.ZodObject<{
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
type?: string | RegExp | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
defaultSizeTypes?: string[] | undefined;
Expand Down Expand Up @@ -8669,7 +8669,7 @@ export const rspackOptions: z.ZodObject<{
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
type?: string | RegExp | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
defaultSizeTypes?: string[] | undefined;
Expand Down Expand Up @@ -9271,7 +9271,7 @@ export const rspackOptions: z.ZodObject<{
cacheGroups?: Record<string, false | {
name?: string | false | ((args_0: Module | undefined, ...args: unknown[]) => unknown) | undefined;
type?: string | RegExp | undefined;
filename?: string | undefined;
filename?: string | ((args_0: PathData, args_1: JsAssetInfo | undefined, ...args: unknown[]) => string) | undefined;
chunks?: RegExp | "initial" | "async" | "all" | ((args_0: Chunk, ...args: unknown[]) => boolean) | undefined;
usedExports?: boolean | undefined;
defaultSizeTypes?: string[] | 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

0 comments on commit c022d42

Please sign in to comment.