diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 3326a32ce7..36bcc60abd 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -39,8 +39,6 @@ export type { Build, BuildOptions, BundlerPluginInstance, - CacheGroup, - CacheGroups, Charset, ClientConfig, CliShortcut, diff --git a/packages/core/src/plugins/moduleFederation.ts b/packages/core/src/plugins/moduleFederation.ts index 0f1c14d381..0917f2342e 100644 --- a/packages/core/src/plugins/moduleFederation.ts +++ b/packages/core/src/plugins/moduleFederation.ts @@ -1,7 +1,7 @@ import { isRegExp } from 'node:util/types'; import { rspack } from '@rspack/core'; import type { RspackPluginInstance } from '@rspack/core'; -import type { CacheGroup, RsbuildPlugin, Rspack } from '../types'; +import type { RsbuildPlugin, Rspack } from '../types'; /** * Force remote entry not be affected by user's chunkSplit strategy, @@ -22,14 +22,18 @@ class PatchSplitChunksPlugin implements RspackPluginInstance { return; } - const applyPatch = (cacheGroup: CacheGroup) => { - if (typeof cacheGroup !== 'object' || isRegExp(cacheGroup)) { + const applyPatch = ( + config: + | Rspack.OptimizationSplitChunksCacheGroup + | Rspack.OptimizationSplitChunksOptions, + ) => { + if (typeof config !== 'object' || isRegExp(config)) { return; } // cacheGroup.chunks will inherit splitChunks.chunks // so we only need to modify the chunks that are set separately. - const { chunks } = cacheGroup; + const { chunks } = config; if (!chunks || chunks === 'async') { return; } @@ -37,7 +41,7 @@ class PatchSplitChunksPlugin implements RspackPluginInstance { if (typeof chunks === 'function') { const prevChunks = chunks; - cacheGroup.chunks = (chunk) => { + config.chunks = (chunk) => { if (chunk.name && chunk.name === this.name) { return false; } @@ -47,7 +51,7 @@ class PatchSplitChunksPlugin implements RspackPluginInstance { } if (chunks === 'all') { - cacheGroup.chunks = (chunk) => { + config.chunks = (chunk) => { if (chunk.name && chunk.name === this.name) { return false; } @@ -57,7 +61,7 @@ class PatchSplitChunksPlugin implements RspackPluginInstance { } if (chunks === 'initial') { - cacheGroup.chunks = (chunk) => { + config.chunks = (chunk) => { if (chunk.name && chunk.name === this.name) { return false; } @@ -77,7 +81,9 @@ class PatchSplitChunksPlugin implements RspackPluginInstance { // patch splitChunk.cacheGroups[key].chunks for (const cacheGroupKey of Object.keys(cacheGroups)) { - applyPatch(cacheGroups[cacheGroupKey]); + if (cacheGroups[cacheGroupKey]) { + applyPatch(cacheGroups[cacheGroupKey]); + } } } } diff --git a/packages/core/src/plugins/splitChunks.ts b/packages/core/src/plugins/splitChunks.ts index 6d3b9388e8..1cf76a50f8 100644 --- a/packages/core/src/plugins/splitChunks.ts +++ b/packages/core/src/plugins/splitChunks.ts @@ -1,14 +1,16 @@ import assert from 'node:assert'; import { NODE_MODULES_REGEX } from '../constants'; import type { - CacheGroups, ForceSplitting, Polyfill, RsbuildChunkSplit, RsbuildPlugin, + Rspack, SplitChunks, } from '../types'; +type CacheGroups = Record; + // We expose three layers to specify Rspack chunk-split config: // 1. By strategy. Some best practices strategies. // 2. By forceSplitting config, which is designed to split chunks by user defined rules.That's easier to use than Rspack raw config. diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index 28e639d77d..f341a38eb6 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -616,24 +616,6 @@ export type SplitChunks = Configuration extends { ? P : never; -export type CacheGroups = Configuration extends { - optimization?: { - splitChunks?: - | { - cacheGroups?: infer P; - } - | false; - }; -} - ? P - : never; - -export type CacheGroup = CacheGroups extends { - [key: string]: infer P; -} - ? P - : null; - export type ForceSplitting = RegExp[] | Record; export interface BaseSplitRules { diff --git a/packages/plugin-react/src/splitChunks.ts b/packages/plugin-react/src/splitChunks.ts index fea64cd00a..71f5efdee1 100644 --- a/packages/plugin-react/src/splitChunks.ts +++ b/packages/plugin-react/src/splitChunks.ts @@ -1,4 +1,4 @@ -import type { CacheGroups, RsbuildPluginAPI, SplitChunks } from '@rsbuild/core'; +import type { RsbuildPluginAPI, Rspack, SplitChunks } from '@rsbuild/core'; import type { SplitReactChunkOptions } from './index.js'; const isPlainObject = (obj: unknown): obj is Record => @@ -24,7 +24,10 @@ export const applySplitChunksRule = ( return; } - const extraGroups: CacheGroups = {}; + const extraGroups: Record< + string, + Rspack.OptimizationSplitChunksCacheGroup + > = {}; if (options.react) { extraGroups.react = { diff --git a/packages/plugin-vue/src/splitChunks.ts b/packages/plugin-vue/src/splitChunks.ts index c4fc641556..d594d30cc8 100644 --- a/packages/plugin-vue/src/splitChunks.ts +++ b/packages/plugin-vue/src/splitChunks.ts @@ -1,4 +1,4 @@ -import type { CacheGroups, RsbuildPluginAPI, SplitChunks } from '@rsbuild/core'; +import type { RsbuildPluginAPI, Rspack, SplitChunks } from '@rsbuild/core'; import type { SplitVueChunkOptions } from './index.js'; const isPlainObject = (obj: unknown): obj is Record => @@ -24,7 +24,10 @@ export const applySplitChunksRule = ( return; } - const extraGroups: CacheGroups = {}; + const extraGroups: Record< + string, + Rspack.OptimizationSplitChunksCacheGroup + > = {}; if (options.vue) { extraGroups.vue = {