Skip to content

Commit cf14509

Browse files
authored
fix: configure the default assetPrefix for MF apps correctly (#3094)
1 parent 765a53c commit cf14509

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

.changeset/happy-chefs-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/rsbuild-plugin': patch
3+
---
4+
5+
fix: configure the default assetPrefix for MF apps correctly

packages/rsbuild-plugin/src/cli/index.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parseOptions } from '@module-federation/enhanced';
22
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
33
import { isRequiredVersion } from '@module-federation/sdk';
44

5-
import { isRegExp } from '../utils/index';
5+
import { isRegExp, DEFAULT_ASSET_PREFIX } from '../utils/index';
66
import pkgJson from '../../package.json';
77

88
import type {
@@ -44,16 +44,26 @@ export const pluginModuleFederation = (
4444
);
4545

4646
api.modifyRsbuildConfig((config) => {
47-
// If this is a provider app, Rsbuild should send the ws request to the provider's dev server.
48-
// This allows the provider to do HMR when the provider module is loaded in the consumer's page.
49-
if (
50-
moduleFederationOptions.exposes &&
51-
config.server?.port &&
52-
!config.dev?.client?.port
53-
) {
47+
// Change some default configs for remote modules
48+
if (moduleFederationOptions.exposes) {
5449
config.dev ||= {};
55-
config.dev.client ||= {};
56-
config.dev.client.port = config.server.port;
50+
51+
// For remote modules, Rsbuild should send the ws request to the provider's dev server.
52+
// This allows the provider to do HMR when the provider module is loaded in the consumer's page.
53+
if (config.server?.port && !config.dev.client?.port) {
54+
config.dev.client ||= {};
55+
config.dev.client.port = config.server.port;
56+
}
57+
58+
// Change the default assetPrefix to `true` for remote modules.
59+
// This ensures that the remote module's assets can be requested by consumer apps with the correct URL.
60+
const originalConfig = api.getRsbuildConfig('original');
61+
if (
62+
originalConfig.dev?.assetPrefix === undefined &&
63+
config.dev.assetPrefix === DEFAULT_ASSET_PREFIX
64+
) {
65+
config.dev.assetPrefix = true;
66+
}
5767
}
5868
});
5969

@@ -147,12 +157,6 @@ export const pluginModuleFederation = (
147157
if (!chain.output.get('uniqueName')) {
148158
chain.output.set('uniqueName', moduleFederationOptions.name);
149159
}
150-
151-
const publicPath = chain.output.get('publicPath');
152-
// set the default publicPath to 'auto' to make MF work
153-
if (publicPath === '/') {
154-
chain.output.set('publicPath', 'auto');
155-
}
156160
});
157161
},
158162
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DEFAULT_ASSET_PREFIX = '/';

packages/rsbuild-plugin/src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ import util from 'util';
33
export function isRegExp(target: any) {
44
return util.types.isRegExp(target);
55
}
6+
7+
export * from './constant';

0 commit comments

Comments
 (0)