1+ import pBtoa from 'btoa' ;
2+ import { ContainerManager } from '@module-federation/managers' ;
3+ import logger from './logger' ;
4+
15import type { Compiler , RspackPluginInstance } from '@rspack/core' ;
6+ import type { moduleFederationPlugin } from '@module-federation/sdk' ;
27// @ts -ignore
3- import pBtoa from 'btoa' ;
48
59const charMap : Record < string , string > = {
610 '<' : '\\u003C' ,
@@ -23,19 +27,29 @@ function escapeUnsafeChars(str: string) {
2327
2428export 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 }
0 commit comments