Skip to content

Commit bf91f28

Browse files
authored
fix(moduleFederation): remove split chunks patch (#6572)
1 parent fdbc74e commit bf91f28

File tree

4 files changed

+1
-108
lines changed

4 files changed

+1
-108
lines changed

packages/core/src/plugins/moduleFederation.ts

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,5 @@
1-
import { isRegExp } from 'node:util/types';
2-
import type { RspackPluginInstance } from '@rspack/core';
31
import { rspack } from '../rspack';
4-
import type { RsbuildPlugin, Rspack } from '../types';
5-
6-
/**
7-
* Force remote entry not be affected by user's chunkSplit strategy,
8-
* Otherwise, the remote chunk will not be loaded correctly.
9-
* @see https://github.com/web-infra-dev/rsbuild/discussions/1461#discussioncomment-8329790
10-
*/
11-
class PatchSplitChunksPlugin implements RspackPluginInstance {
12-
name: string;
13-
14-
constructor(name: string) {
15-
this.name = name;
16-
}
17-
18-
apply(compiler: Rspack.Compiler): void {
19-
const { splitChunks } = compiler.options.optimization;
20-
21-
if (!splitChunks) {
22-
return;
23-
}
24-
25-
const applyPatch = (
26-
config:
27-
| Rspack.OptimizationSplitChunksCacheGroup
28-
| Rspack.OptimizationSplitChunksOptions,
29-
) => {
30-
if (typeof config !== 'object' || isRegExp(config)) {
31-
return;
32-
}
33-
34-
// cacheGroup.chunks will inherit splitChunks.chunks
35-
// so we only need to modify the chunks that are set separately.
36-
const { chunks } = config;
37-
if (!chunks || chunks === 'async') {
38-
return;
39-
}
40-
41-
if (typeof chunks === 'function') {
42-
const prevChunks = chunks;
43-
44-
config.chunks = (chunk) => {
45-
if (chunk.name && chunk.name === this.name) {
46-
return false;
47-
}
48-
return prevChunks(chunk);
49-
};
50-
return;
51-
}
52-
53-
if (chunks === 'all') {
54-
config.chunks = (chunk) => {
55-
if (chunk.name && chunk.name === this.name) {
56-
return false;
57-
}
58-
return true;
59-
};
60-
return;
61-
}
62-
63-
if (chunks === 'initial') {
64-
config.chunks = (chunk) => {
65-
if (chunk.name && chunk.name === this.name) {
66-
return false;
67-
}
68-
return chunk.isOnlyInitial();
69-
};
70-
return;
71-
}
72-
};
73-
74-
// patch splitChunk.chunks
75-
applyPatch(splitChunks);
76-
77-
const { cacheGroups } = splitChunks;
78-
if (!cacheGroups) {
79-
return;
80-
}
81-
82-
// patch splitChunk.cacheGroups[key].chunks
83-
for (const cacheGroupKey of Object.keys(cacheGroups)) {
84-
if (cacheGroups[cacheGroupKey]) {
85-
applyPatch(cacheGroups[cacheGroupKey]);
86-
}
87-
}
88-
}
89-
}
2+
import type { RsbuildPlugin } from '../types';
903

914
export function pluginModuleFederation(): RsbuildPlugin {
925
return {
@@ -177,12 +90,6 @@ export function pluginModuleFederation(): RsbuildPlugin {
17790
.use(rspack.container.ModuleFederationPlugin, [options]);
17891

17992
if (options.name) {
180-
if (options.exposes) {
181-
chain
182-
.plugin('mf-patch-split-chunks')
183-
.use(PatchSplitChunksPlugin, [options.name]);
184-
}
185-
18693
// `uniqueName` is required for react refresh to work
18794
if (!chain.output.get('uniqueName')) {
18895
chain.output.set('uniqueName', options.name);

packages/core/tests/__snapshots__/moduleFederation.test.ts.snap

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ exports[`plugin-module-federation > should set environment module federation con
3535
},
3636
},
3737
},
38-
PatchSplitChunksPlugin {
39-
"name": "remote",
40-
},
4138
],
4239
},
4340
{
@@ -97,9 +94,6 @@ exports[`plugin-module-federation > should set module federation and environment
9794
},
9895
},
9996
},
100-
PatchSplitChunksPlugin {
101-
"name": "remote",
102-
},
10397
],
10498
},
10599
{
@@ -132,9 +126,6 @@ exports[`plugin-module-federation > should set module federation and environment
132126
},
133127
},
134128
},
135-
PatchSplitChunksPlugin {
136-
"name": "remote",
137-
},
138129
],
139130
},
140131
]
@@ -174,9 +165,6 @@ exports[`plugin-module-federation > should set module federation config 1`] = `
174165
},
175166
},
176167
},
177-
PatchSplitChunksPlugin {
178-
"name": "remote",
179-
},
180168
],
181169
}
182170
`;

website/docs/en/config/module-federation/options.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ When you set the `moduleFederation.options` option, Rsbuild will take the follow
1919
- Set the default value of the provider's [dev.assetPrefix](/config/dev/asset-prefix) configuration to `true`. This will ensure that the static asset URL is correct for remote modules.
2020
- Set the default value of Rspack's [output.uniqueName](https://rspack.rs/config/output#outputuniquename) configuration to `moduleFederation.options.name`, this allows HMR to work as expected.
2121
- Turn off the `split-by-experience` rules in Rsbuild's [performance.chunkSplit](/config/performance/chunk-split) as it may conflict with shared modules, refer to [#3161](https://github.com/module-federation/module-federation-examples/issues/3161).
22-
- Turn off the splitChunks rule of remote entry.
2322

2423
## Usage
2524

website/docs/zh/config/module-federation/options.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
- 将 provider 的 [dev.assetPrefix](/config/dev/asset-prefix) 配置的默认值设置为 `true`,这可以确保 remote modules 的静态资源 URL 是正确的。
2020
- 将 Rspack [output.uniqueName](https://rspack.rs/zh/config/output#outputuniquename) 配置的默认值设置为 `moduleFederation.options.name`,使 HMR 可以正常工作。
2121
- 关闭 Rsbuild [performance.chunkSplit](/config/performance/chunk-split)`split-by-experience` 相关的规则,因为这可能会与 shared modules 冲突,参考 [#3161](https://github.com/module-federation/module-federation-examples/issues/3161)
22-
- 关闭 remote entry 的 splitChunks 规则。
2322

2423
## 用法
2524

0 commit comments

Comments
 (0)