Skip to content
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
2 changes: 0 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export type {
Build,
BuildOptions,
BundlerPluginInstance,
CacheGroup,
CacheGroups,
Charset,
ClientConfig,
CliShortcut,
Expand Down
22 changes: 14 additions & 8 deletions packages/core/src/plugins/moduleFederation.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -22,22 +22,26 @@ 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;
}

if (typeof chunks === 'function') {
const prevChunks = chunks;

cacheGroup.chunks = (chunk) => {
config.chunks = (chunk) => {
if (chunk.name && chunk.name === this.name) {
return false;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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]);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/splitChunks.ts
Original file line number Diff line number Diff line change
@@ -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<string, Rspack.OptimizationSplitChunksCacheGroup>;

// 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.
Expand Down
18 changes: 0 additions & 18 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, RegExp>;

export interface BaseSplitRules {
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-react/src/splitChunks.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> =>
Expand All @@ -24,7 +24,10 @@ export const applySplitChunksRule = (
return;
}

const extraGroups: CacheGroups = {};
const extraGroups: Record<
string,
Rspack.OptimizationSplitChunksCacheGroup
> = {};

if (options.react) {
extraGroups.react = {
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-vue/src/splitChunks.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> =>
Expand All @@ -24,7 +24,10 @@ export const applySplitChunksRule = (
return;
}

const extraGroups: CacheGroups = {};
const extraGroups: Record<
string,
Rspack.OptimizationSplitChunksCacheGroup
> = {};

if (options.vue) {
extraGroups.vue = {
Expand Down