1
+ import pBtoa from 'btoa' ;
2
+ import { ContainerManager } from '@module-federation/managers' ;
3
+ import logger from './logger' ;
4
+
1
5
import type { Compiler , RspackPluginInstance } from '@rspack/core' ;
6
+ import type { moduleFederationPlugin } from '@module-federation/sdk' ;
2
7
// @ts -ignore
3
- import pBtoa from 'btoa' ;
4
8
5
9
const charMap : Record < string , string > = {
6
10
'<' : '\\u003C' ,
@@ -23,19 +27,29 @@ function escapeUnsafeChars(str: string) {
23
27
24
28
export class RemoteEntryPlugin implements RspackPluginInstance {
25
29
readonly name = 'VmokRemoteEntryPlugin' ;
26
- private _name : string ;
27
- private _getPublicPath : string ;
30
+ _options : moduleFederationPlugin . ModuleFederationPluginOptions ;
28
31
29
- constructor ( name : string , getPublicPath : string ) {
30
- this . _name = name ;
31
- this . _getPublicPath = getPublicPath ;
32
+ constructor ( options : moduleFederationPlugin . ModuleFederationPluginOptions ) {
33
+ this . _options = options ;
32
34
}
33
35
34
36
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
+ }
35
49
let code ;
36
- const sanitizedPublicPath = escapeUnsafeChars ( this . _getPublicPath ) ;
50
+ const sanitizedPublicPath = escapeUnsafeChars ( getPublicPath ) ;
37
51
38
- if ( ! this . _getPublicPath . startsWith ( 'function' ) ) {
52
+ if ( ! getPublicPath . startsWith ( 'function' ) ) {
39
53
code = `${
40
54
compiler . webpack . RuntimeGlobals . publicPath
41
55
} = new Function(${ JSON . stringify ( sanitizedPublicPath ) } )()`;
@@ -47,7 +61,7 @@ export class RemoteEntryPlugin implements RspackPluginInstance {
47
61
48
62
compiler . hooks . afterPlugins . tap ( 'VmokRemoteEntryPlugin' , ( ) => {
49
63
new compiler . webpack . EntryPlugin ( compiler . context , dataUrl , {
50
- name : this . _name ,
64
+ name : name ,
51
65
} ) . apply ( compiler ) ;
52
66
} ) ;
53
67
}
0 commit comments