Skip to content

Commit 42b9549

Browse files
fix(mf): validate legacy share-scope arrays consistently
1 parent 649d953 commit 42b9549

File tree

8 files changed

+94
-14
lines changed

8 files changed

+94
-14
lines changed

packages/rspack/src/container/ContainerReferencePlugin.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,43 @@ export type RemotesConfig = {
3030
shareScope?: string | string[];
3131
};
3232

33+
function hasMultipleShareScopes(shareScope?: string | string[]) {
34+
return Array.isArray(shareScope) && shareScope.filter(Boolean).length > 1;
35+
}
36+
3337
export class ContainerReferencePlugin extends RspackBuiltinPlugin {
3438
name = BuiltinPluginName.ContainerReferencePlugin;
3539
_options;
3640

3741
constructor(options: ContainerReferencePluginOptions) {
3842
super();
43+
const enhanced = options.enhanced ?? false;
44+
const remotes = parseOptions(
45+
options.remotes,
46+
(item) => ({
47+
external: Array.isArray(item) ? item : [item],
48+
shareScope: options.shareScope || 'default',
49+
}),
50+
(item) => ({
51+
external: Array.isArray(item.external)
52+
? item.external
53+
: [item.external],
54+
shareScope: item.shareScope || options.shareScope || 'default',
55+
}),
56+
);
57+
if (
58+
!enhanced &&
59+
(hasMultipleShareScopes(options.shareScope) ||
60+
remotes.some(([, config]) => hasMultipleShareScopes(config.shareScope)))
61+
) {
62+
throw new Error(
63+
'[ContainerReferencePlugin] Multiple share scopes are only supported in enhanced mode. Set `enhanced: true` or provide a single `shareScope`.',
64+
);
65+
}
3966
this._options = {
4067
remoteType: options.remoteType,
41-
remotes: parseOptions(
42-
options.remotes,
43-
(item) => ({
44-
external: Array.isArray(item) ? item : [item],
45-
shareScope: options.shareScope || 'default',
46-
}),
47-
(item) => ({
48-
external: Array.isArray(item.external)
49-
? item.external
50-
: [item.external],
51-
shareScope: item.shareScope || options.shareScope || 'default',
52-
}),
53-
),
54-
enhanced: options.enhanced ?? false,
68+
remotes,
69+
enhanced,
5570
};
5671
}
5772

packages/rspack/src/sharing/SharePlugin.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export type SharedConfig = {
3838

3939
export type NormalizedSharedOptions = [string, SharedConfig][];
4040

41+
function hasMultipleShareScopes(shareScope?: string | string[]) {
42+
return Array.isArray(shareScope) && shareScope.filter(Boolean).length > 1;
43+
}
44+
4145
export function normalizeSharedOptions(
4246
shared: Shared,
4347
): NormalizedSharedOptions {
@@ -114,6 +118,17 @@ export class SharePlugin {
114118
constructor(options: SharePluginOptions) {
115119
const enhanced = options.enhanced ?? false;
116120
const sharedOptions = normalizeSharedOptions(options.shared);
121+
if (
122+
!enhanced &&
123+
(hasMultipleShareScopes(options.shareScope) ||
124+
sharedOptions.some(([, config]) =>
125+
hasMultipleShareScopes(config.shareScope),
126+
))
127+
) {
128+
throw new Error(
129+
'[SharePlugin] Multiple share scopes are only supported in enhanced mode. Set `enhanced: true` or provide a single `shareScope`.',
130+
);
131+
}
117132
const consumes = createConsumeShareOptions(sharedOptions, enhanced);
118133
const provides = createProvideShareOptions(sharedOptions, enhanced);
119134
this._shareScope = options.shareScope;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
/\[ContainerReferencePlugin\] Multiple share scopes are only supported in enhanced mode\./,
3+
];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it('should fail before build for legacy multi-scope remote shareScope', () => {
2+
expect(true).toBe(true);
3+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { ModuleFederationPluginV1: ModuleFederationPlugin } =
2+
require('@rspack/core').container;
3+
4+
/** @type {import('@rspack/core').Configuration} */
5+
module.exports = {
6+
mode: 'development',
7+
entry: './index.js',
8+
plugins: [
9+
new ModuleFederationPlugin({
10+
name: 'legacy_multi_scope_validation_remotes',
11+
remotes: {
12+
remote: {
13+
external: 'remote@http://localhost:3000/remoteEntry.js',
14+
shareScope: ['default', 'ssr'],
15+
},
16+
},
17+
}),
18+
],
19+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
/\[SharePlugin\] Multiple share scopes are only supported in enhanced mode\./,
3+
];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it('should fail before build for legacy multi-scope shared shareScope', () => {
2+
expect(true).toBe(true);
3+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { ModuleFederationPluginV1: ModuleFederationPlugin } =
2+
require('@rspack/core').container;
3+
4+
/** @type {import('@rspack/core').Configuration} */
5+
module.exports = {
6+
mode: 'development',
7+
entry: './index.js',
8+
plugins: [
9+
new ModuleFederationPlugin({
10+
name: 'legacy_multi_scope_validation_shared',
11+
shared: {
12+
react: {
13+
import: 'react',
14+
shareScope: ['default', 'ssr'],
15+
},
16+
},
17+
}),
18+
],
19+
};

0 commit comments

Comments
 (0)