11import { createFilter } from '@rollup/pluginutils' ;
22import { Plugin } from 'vite' ;
3+ import { mapCodeToCodeWithSourcemap } from '../utils/mapCodeToCodeWithSourcemap' ;
34import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFederationOptions' ;
5+ import { resolvePublicPath } from '../utils/publicPath' ;
46import {
57 generateExposes ,
68 generateRemoteEntry ,
@@ -9,7 +11,6 @@ import {
911 VIRTUAL_EXPOSES ,
1012} from '../virtualModules' ;
1113import { parsePromise } from './pluginModuleParseEnd' ;
12- import { resolvePublicPath } from '../utils/publicPath' ;
1314
1415const filter : ( id : string ) => boolean = createFilter ( ) ;
1516
@@ -46,25 +47,28 @@ export default function (): Plugin {
4647 return id ;
4748 }
4849 } ,
49- async transform ( code : string , id : string ) {
50- if ( ! filter ( id ) ) return ;
51- if ( id . includes ( REMOTE_ENTRY_ID ) ) {
52- return parsePromise . then ( ( _ ) => generateRemoteEntry ( getNormalizeModuleFederationOptions ( ) ) ) ;
53- }
54- if ( id === VIRTUAL_EXPOSES ) {
55- return generateExposes ( ) ;
56- }
57- if ( id . includes ( getHostAutoInitPath ( ) ) ) {
58- const options = getNormalizeModuleFederationOptions ( ) ;
59- if ( _command === 'serve' ) {
60- const host =
61- typeof viteConfig . server ?. host === 'string' && viteConfig . server . host !== '0.0.0.0'
62- ? viteConfig . server . host
63- : 'localhost' ;
64- const publicPath = JSON . stringify (
65- resolvePublicPath ( options , viteConfig . base ) + options . filename
50+ transform ( code : string , id : string ) {
51+ const transformedCode = ( ( ) => {
52+ if ( ! filter ( id ) ) return ;
53+ if ( id . includes ( REMOTE_ENTRY_ID ) ) {
54+ return parsePromise . then ( ( _ ) =>
55+ generateRemoteEntry ( getNormalizeModuleFederationOptions ( ) )
6656 ) ;
67- return `
57+ }
58+ if ( id === VIRTUAL_EXPOSES ) {
59+ return generateExposes ( ) ;
60+ }
61+ if ( id . includes ( getHostAutoInitPath ( ) ) ) {
62+ const options = getNormalizeModuleFederationOptions ( ) ;
63+ if ( _command === 'serve' ) {
64+ const host =
65+ typeof viteConfig . server ?. host === 'string' && viteConfig . server . host !== '0.0.0.0'
66+ ? viteConfig . server . host
67+ : 'localhost' ;
68+ const publicPath = JSON . stringify (
69+ resolvePublicPath ( options , viteConfig . base ) + options . filename
70+ ) ;
71+ return `
6872 const origin = (window && ${ ! options . ignoreOrigin } ) ? window.origin : "//${ host } :${ viteConfig . server ?. port } "
6973 const remoteEntryPromise = await import(origin + ${ publicPath } )
7074 // __tla only serves as a hack for vite-plugin-top-level-await.
@@ -74,9 +78,12 @@ export default function (): Plugin {
7478 .then(remoteEntry.init).catch(remoteEntry.init)
7579 })
7680 ` ;
81+ }
82+ return code ;
7783 }
78- return code ;
79- }
84+ } ) ( ) ;
85+
86+ return mapCodeToCodeWithSourcemap ( transformedCode ) ;
8087 } ,
8188 } ;
8289}
0 commit comments