Skip to content

Commit 9efb9b9

Browse files
authored
fix(enhanced): apply getPublicPath only if exposes is set (#3717)
1 parent 92882ec commit 9efb9b9

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

.changeset/dull-buttons-sneeze.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/enhanced': patch
3+
'@module-federation/rspack': patch
4+
---
5+
6+
fix(enhanced): apply getPublicPath only if exposes is set

packages/enhanced/src/lib/container/ModuleFederationPlugin.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
7979
apply(compiler: Compiler): void {
8080
const { _options: options } = this;
8181
// must before ModuleFederationPlugin
82-
if (options.getPublicPath && options.name) {
83-
new RemoteEntryPlugin(options.name, options.getPublicPath).apply(
84-
// @ts-ignore
85-
compiler,
86-
);
87-
}
82+
new RemoteEntryPlugin(options).apply(
83+
// @ts-ignore
84+
compiler,
85+
);
8886
if (options.experiments?.provideExternalRuntime) {
8987
if (options.exposes) {
9088
throw new Error(

packages/rspack/src/ModuleFederationPlugin.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ export class ModuleFederationPlugin implements RspackPluginInstance {
7777
}
7878

7979
// must before ModuleFederationPlugin
80-
if (options.getPublicPath && options.name) {
81-
new RemoteEntryPlugin(options.name, options.getPublicPath).apply(
82-
compiler,
83-
);
84-
}
80+
new RemoteEntryPlugin(options).apply(compiler);
8581

8682
if (options.experiments?.provideExternalRuntime) {
8783
if (options.exposes) {

packages/rspack/src/RemoteEntryPlugin.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import pBtoa from 'btoa';
2+
import { ContainerManager } from '@module-federation/managers';
3+
import logger from './logger';
4+
15
import type { Compiler, RspackPluginInstance } from '@rspack/core';
6+
import type { moduleFederationPlugin } from '@module-federation/sdk';
27
// @ts-ignore
3-
import pBtoa from 'btoa';
48

59
const charMap: Record<string, string> = {
610
'<': '\\u003C',
@@ -23,19 +27,29 @@ function escapeUnsafeChars(str: string) {
2327

2428
export class RemoteEntryPlugin implements RspackPluginInstance {
2529
readonly name = 'VmokRemoteEntryPlugin';
26-
private _name: string;
27-
private _getPublicPath: string;
30+
_options: moduleFederationPlugin.ModuleFederationPluginOptions;
2831

29-
constructor(name: string, getPublicPath: string) {
30-
this._name = name;
31-
this._getPublicPath = getPublicPath;
32+
constructor(options: moduleFederationPlugin.ModuleFederationPluginOptions) {
33+
this._options = options;
3234
}
3335

3436
apply(compiler: Compiler): void {
37+
const { name, getPublicPath } = this._options;
38+
if (!getPublicPath || !name) {
39+
return;
40+
}
41+
const containerManager = new ContainerManager();
42+
containerManager.init(this._options);
43+
if (!containerManager.enable) {
44+
logger.warn(
45+
"Detect you don't set exposes, 'getPublicPath' will not have effect.",
46+
);
47+
return;
48+
}
3549
let code;
36-
const sanitizedPublicPath = escapeUnsafeChars(this._getPublicPath);
50+
const sanitizedPublicPath = escapeUnsafeChars(getPublicPath);
3751

38-
if (!this._getPublicPath.startsWith('function')) {
52+
if (!getPublicPath.startsWith('function')) {
3953
code = `${
4054
compiler.webpack.RuntimeGlobals.publicPath
4155
} = new Function(${JSON.stringify(sanitizedPublicPath)})()`;
@@ -47,7 +61,7 @@ export class RemoteEntryPlugin implements RspackPluginInstance {
4761

4862
compiler.hooks.afterPlugins.tap('VmokRemoteEntryPlugin', () => {
4963
new compiler.webpack.EntryPlugin(compiler.context, dataUrl, {
50-
name: this._name,
64+
name: name,
5165
}).apply(compiler);
5266
});
5367
}

packages/rspack/src/logger.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createLogger } from '@module-federation/sdk';
2+
3+
const logger = createLogger('[ Module Federation Rspack Plugin ]');
4+
5+
export default logger;

0 commit comments

Comments
 (0)