@@ -4,6 +4,7 @@ import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFed
44import {
55 generateExposes ,
66 generateRemoteEntry ,
7+ getHostAutoInitPath ,
78 REMOTE_ENTRY_ID ,
89 VIRTUAL_EXPOSES ,
910} from '../virtualModules' ;
@@ -12,16 +13,26 @@ import { parsePromise } from './pluginModuleParseEnd';
1213const filter : ( id : string ) => boolean = createFilter ( ) ;
1314
1415export default function ( ) : Plugin {
16+ let viteConfig : any , _command : string ;
1517 return {
1618 name : 'proxyRemoteEntry' ,
1719 enforce : 'post' ,
20+ configResolved ( config ) {
21+ viteConfig = config ;
22+ } ,
23+ config ( config , { command } ) {
24+ _command = command ;
25+ } ,
1826 resolveId ( id : string ) {
1927 if ( id === REMOTE_ENTRY_ID ) {
2028 return REMOTE_ENTRY_ID ;
2129 }
2230 if ( id === VIRTUAL_EXPOSES ) {
2331 return VIRTUAL_EXPOSES ;
2432 }
33+ if ( _command === 'serve' && id . includes ( getHostAutoInitPath ( ) ) ) {
34+ return id ;
35+ }
2536 } ,
2637 load ( id : string ) {
2738 if ( id === REMOTE_ENTRY_ID ) {
@@ -30,6 +41,9 @@ export default function (): Plugin {
3041 if ( id === VIRTUAL_EXPOSES ) {
3142 return generateExposes ( ) ;
3243 }
44+ if ( _command === 'serve' && id . includes ( getHostAutoInitPath ( ) ) ) {
45+ return id ;
46+ }
3347 } ,
3448 async transform ( code : string , id : string ) {
3549 if ( ! filter ( id ) ) return ;
@@ -39,6 +53,16 @@ export default function (): Plugin {
3953 if ( id === VIRTUAL_EXPOSES ) {
4054 return generateExposes ( ) ;
4155 }
56+ if ( id . includes ( getHostAutoInitPath ( ) ) ) {
57+ const options = getNormalizeModuleFederationOptions ( ) ;
58+ if ( _command === 'serve' ) {
59+ return `
60+ const {init} = await import("//localhost:${ viteConfig . server ?. port } ${ viteConfig . base + options . filename } ")
61+ init()
62+ ` ;
63+ }
64+ return code ;
65+ }
4266 } ,
4367 } ;
4468}
0 commit comments