File tree Expand file tree Collapse file tree 9 files changed +59
-33
lines changed Expand file tree Collapse file tree 9 files changed +59
-33
lines changed Original file line number Diff line number Diff line change 4
4
"scripts" : {
5
5
"dev" : " rsbuild dev" ,
6
6
"build" : " rsbuild build" ,
7
- "preview" : " npm run build && rsbuild"
7
+ "preview" : " npm run build && rsbuild preview "
8
8
},
9
9
"dependencies" : {
10
10
"@module-federation/enhanced" : " 0.2.5" ,
Original file line number Diff line number Diff line change 1
- import React from 'react' ;
2
- import ReactDOM from 'react-dom/client' ;
3
- import App from './App' ;
4
-
5
- //@ts -ignore
6
- const root = ReactDOM . createRoot ( document . getElementById ( 'app' ) ) ;
7
- root . render (
8
- < React . StrictMode >
9
- < App />
10
- </ React . StrictMode >
11
- ) ;
Original file line number Diff line number Diff line change 17
17
"dev" : " microbundle watch --no-sourcemap --compress=false" ,
18
18
"build" : " rimraf lib && microbundle --no-sourcemap --compress=false" ,
19
19
"dev-rv" : " pnpm -filter 'examples-rust-vite*' run dev" ,
20
+ "preview-rv" : " pnpm -filter 'examples-rust-vite*' run preview" ,
20
21
"dev-vv" : " pnpm -filter 'examples-vite-vite*' run dev" ,
21
22
"dev-nv" : " pnpm -filter 'examples-nuxt-vite-host' -filter 'examples-vite-vite-remote' run dev" ,
22
23
"preview-vv" : " pnpm -filter 'examples-vite-vite*' run preview" ,
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import {
19
19
initVirtualModules ,
20
20
REMOTE_ENTRY_ID ,
21
21
} from './virtualModules' ;
22
+ import { VIRTUAL_EXPOSES } from './virtualModules/virtualExposes' ;
22
23
23
24
function federation ( mfUserOptions : ModuleFederationOptions ) : Plugin [ ] {
24
25
const options = normalizeModuleFederationOptions ( mfUserOptions ) ;
@@ -38,6 +39,10 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
38
39
entryName : 'hostInit' ,
39
40
entryPath : getHostAutoInitPath ( ) ,
40
41
} ) ,
42
+ ...addEntry ( {
43
+ entryName : 'virtualExposes' ,
44
+ entryPath : VIRTUAL_EXPOSES ,
45
+ } ) ,
41
46
pluginProxyRemoteEntry ( ) ,
42
47
pluginProxyRemotes ( options ) ,
43
48
...pluginModuleParseEnd ( ( id : string ) => {
Original file line number Diff line number Diff line change 3
3
* This plugin allows me to wait until all modules are built, and then expose them together.
4
4
*/
5
5
import { Plugin } from 'vite' ;
6
+ import { VIRTUAL_EXPOSES } from '../virtualModules' ;
6
7
7
8
let _resolve : any ,
8
9
_reject : any ,
@@ -11,6 +12,7 @@ let _resolve: any,
11
12
_reject = reject ;
12
13
} ) ;
13
14
let parsePromise = promise ;
15
+ let exposesParseEnd = false ;
14
16
15
17
const parseStartSet = new Set ( ) ;
16
18
const parseEndSet = new Set ( ) ;
@@ -44,8 +46,12 @@ export default function (excludeFn: Function): Plugin[] {
44
46
if ( excludeFn ( id ) ) {
45
47
return ;
46
48
}
49
+ if ( id === VIRTUAL_EXPOSES ) {
50
+ // When the entry JS file is empty and only contains exposes export code, it’s necessary to wait for the exposes modules to be resolved in order to collect the dependencies being used.
51
+ exposesParseEnd = true ;
52
+ }
47
53
parseEndSet . add ( id ) ;
48
- if ( parseStartSet . size === parseEndSet . size ) {
54
+ if ( exposesParseEnd && parseStartSet . size === parseEndSet . size ) {
49
55
_resolve ( 1 ) ;
50
56
}
51
57
} ,
Original file line number Diff line number Diff line change 1
1
import { createFilter } from '@rollup/pluginutils' ;
2
2
import { Plugin } from 'vite' ;
3
3
import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFederationOptions' ;
4
- import { generateRemoteEntry , REMOTE_ENTRY_ID } from '../virtualModules' ;
4
+ import {
5
+ generateExposes ,
6
+ generateRemoteEntry ,
7
+ REMOTE_ENTRY_ID ,
8
+ VIRTUAL_EXPOSES ,
9
+ } from '../virtualModules' ;
5
10
import { parsePromise } from './pluginModuleParseEnd' ;
6
11
7
12
const filter : ( id : string ) => boolean = createFilter ( ) ;
@@ -14,17 +19,26 @@ export default function (): Plugin {
14
19
if ( id === REMOTE_ENTRY_ID ) {
15
20
return REMOTE_ENTRY_ID ;
16
21
}
22
+ if ( id === VIRTUAL_EXPOSES ) {
23
+ return VIRTUAL_EXPOSES ;
24
+ }
17
25
} ,
18
26
load ( id : string ) {
19
27
if ( id === REMOTE_ENTRY_ID ) {
20
28
return parsePromise . then ( ( _ ) => generateRemoteEntry ( getNormalizeModuleFederationOptions ( ) ) ) ;
21
29
}
30
+ if ( id === VIRTUAL_EXPOSES ) {
31
+ return generateExposes ( ) ;
32
+ }
22
33
} ,
23
34
async transform ( code : string , id : string ) {
24
35
if ( ! filter ( id ) ) return ;
25
36
if ( id . includes ( REMOTE_ENTRY_ID ) ) {
26
37
return parsePromise . then ( ( _ ) => generateRemoteEntry ( getNormalizeModuleFederationOptions ( ) ) ) ;
27
38
}
39
+ if ( id === VIRTUAL_EXPOSES ) {
40
+ return generateExposes ( ) ;
41
+ }
28
42
} ,
29
43
} ;
30
44
}
Original file line number Diff line number Diff line change @@ -29,6 +29,8 @@ export {
29
29
writePreBuildLibPath ,
30
30
} from './virtualShared_preBuild' ;
31
31
32
+ export { generateExposes , VIRTUAL_EXPOSES } from './virtualExposes' ;
33
+
32
34
export { virtualRuntimeInitStatus } from './virtualRuntimeInitStatus' ;
33
35
34
36
export function initVirtualModules ( ) {
Original file line number Diff line number Diff line change
1
+ import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFederationOptions' ;
2
+
3
+ export const VIRTUAL_EXPOSES = 'virtual:mf-exposes' ;
4
+ export function generateExposes ( ) {
5
+ const options = getNormalizeModuleFederationOptions ( ) ;
6
+ return `
7
+ export default {
8
+ ${ Object . keys ( options . exposes )
9
+ . map ( ( key ) => {
10
+ return `
11
+ ${ JSON . stringify ( key ) } : async () => {
12
+ const importModule = await import(${ JSON . stringify ( options . exposes [ key ] . import ) } )
13
+ const exportModule = {}
14
+ Object.assign(exportModule, importModule)
15
+ Object.defineProperty(exportModule, "__esModule", {
16
+ value: true,
17
+ enumerable: false
18
+ })
19
+ return exportModule
20
+ }
21
+ ` ;
22
+ } )
23
+ . join ( ',' ) }
24
+ }
25
+ ` ;
26
+ }
Original file line number Diff line number Diff line change 8
8
NormalizedModuleFederationOptions ,
9
9
} from '../utils/normalizeModuleFederationOptions' ;
10
10
import VirtualModule from '../utils/VirtualModule' ;
11
+ import { VIRTUAL_EXPOSES } from './virtualExposes' ;
11
12
import { getUsedRemotesMap } from './virtualRemotes' ;
12
13
import { virtualRuntimeInitStatus } from './virtualRuntimeInitStatus' ;
13
14
import { getPreBuildLibImportId } from './virtualShared_preBuild' ;
@@ -114,25 +115,7 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
114
115
return `
115
116
import {init as runtimeInit, loadRemote} from "@module-federation/runtime";
116
117
${ pluginImportNames . map ( ( item ) => item [ 1 ] ) . join ( '\n' ) }
117
-
118
- const exposesMap = {
119
- ${ Object . keys ( options . exposes )
120
- . map ( ( key ) => {
121
- return `
122
- ${ JSON . stringify ( key ) } : async () => {
123
- const importModule = await import(${ JSON . stringify ( options . exposes [ key ] . import ) } )
124
- const exportModule = {}
125
- Object.assign(exportModule, importModule)
126
- Object.defineProperty(exportModule, "__esModule", {
127
- value: true,
128
- enumerable: false
129
- })
130
- return exportModule
131
- }
132
- ` ;
133
- } )
134
- . join ( ',' ) }
135
- }
118
+ import exposesMap from "${ VIRTUAL_EXPOSES } "
136
119
import {usedShared, usedRemotes} from "${ getLocalSharedImportMapPath ( ) } "
137
120
import {
138
121
initResolve
You can’t perform that action at this time.
0 commit comments