Skip to content

Commit

Permalink
fix: configure the default assetPrefix for MF apps correctly (#3094)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyqykk authored Oct 21, 2024
1 parent 765a53c commit cf14509
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-chefs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/rsbuild-plugin': patch
---

fix: configure the default assetPrefix for MF apps correctly
36 changes: 20 additions & 16 deletions packages/rsbuild-plugin/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parseOptions } from '@module-federation/enhanced';
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
import { isRequiredVersion } from '@module-federation/sdk';

import { isRegExp } from '../utils/index';
import { isRegExp, DEFAULT_ASSET_PREFIX } from '../utils/index';
import pkgJson from '../../package.json';

import type {
Expand Down Expand Up @@ -44,16 +44,26 @@ export const pluginModuleFederation = (
);

api.modifyRsbuildConfig((config) => {
// If this is a provider app, Rsbuild should send the ws request to the provider's dev server.
// This allows the provider to do HMR when the provider module is loaded in the consumer's page.
if (
moduleFederationOptions.exposes &&
config.server?.port &&
!config.dev?.client?.port
) {
// Change some default configs for remote modules
if (moduleFederationOptions.exposes) {
config.dev ||= {};
config.dev.client ||= {};
config.dev.client.port = config.server.port;

// For remote modules, Rsbuild should send the ws request to the provider's dev server.
// This allows the provider to do HMR when the provider module is loaded in the consumer's page.
if (config.server?.port && !config.dev.client?.port) {
config.dev.client ||= {};
config.dev.client.port = config.server.port;
}

// Change the default assetPrefix to `true` for remote modules.
// This ensures that the remote module's assets can be requested by consumer apps with the correct URL.
const originalConfig = api.getRsbuildConfig('original');
if (
originalConfig.dev?.assetPrefix === undefined &&
config.dev.assetPrefix === DEFAULT_ASSET_PREFIX
) {
config.dev.assetPrefix = true;
}
}
});

Expand Down Expand Up @@ -147,12 +157,6 @@ export const pluginModuleFederation = (
if (!chain.output.get('uniqueName')) {
chain.output.set('uniqueName', moduleFederationOptions.name);
}

const publicPath = chain.output.get('publicPath');
// set the default publicPath to 'auto' to make MF work
if (publicPath === '/') {
chain.output.set('publicPath', 'auto');
}
});
},
});
1 change: 1 addition & 0 deletions packages/rsbuild-plugin/src/utils/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_ASSET_PREFIX = '/';
2 changes: 2 additions & 0 deletions packages/rsbuild-plugin/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import util from 'util';
export function isRegExp(target: any) {
return util.types.isRegExp(target);
}

export * from './constant';

0 comments on commit cf14509

Please sign in to comment.