@@ -38,16 +38,22 @@ export function writeLocalSharedImportMap() {
38
38
export function generateLocalSharedImportMap ( ) {
39
39
const options = getNormalizeModuleFederationOptions ( ) ;
40
40
return `
41
+ import {loadShare} from "@module-federation/runtime";
41
42
const importMap = {
42
43
${ Array . from ( getUsedShares ( ) )
43
- . map (
44
- ( pkg ) => `
44
+ . map ( ( pkg ) => {
45
+ const shareItem = getNormalizeShareItem ( pkg ) ;
46
+ return `
45
47
${ JSON . stringify ( pkg ) } : async () => {
46
- let pkg = await import("${ getPreBuildLibImportId ( pkg ) } ")
47
- return pkg
48
+ ${
49
+ shareItem ?. shareConfig . import === false
50
+ ? `throw new Error(\`Shared module '\${${ JSON . stringify ( pkg ) } }' must be provided by host\`);`
51
+ : `let pkg = await import("${ getPreBuildLibImportId ( pkg ) } ");
52
+ return pkg;`
53
+ }
48
54
}
49
- `
50
- )
55
+ ` ;
56
+ } )
51
57
. join ( ',' ) }
52
58
}
53
59
const usedShared = {
@@ -63,8 +69,13 @@ export function generateLocalSharedImportMap() {
63
69
loaded: false,
64
70
from: ${ JSON . stringify ( options . name ) } ,
65
71
async get () {
72
+ if (${ shareItem . shareConfig . import === false } ) {
73
+ const shared = await loadShare(${ JSON . stringify ( key ) } );
74
+ if (shared) return () => shared;
75
+ throw new Error(\`Shared module '\${${ JSON . stringify ( key ) } }' must be provided by host\`);
76
+ }
66
77
usedShared[${ JSON . stringify ( key ) } ].loaded = true
67
- const {${ JSON . stringify ( key ) } : pkgDynamicImport} = importMap
78
+ const {${ JSON . stringify ( key ) } : pkgDynamicImport} = importMap
68
79
const res = await pkgDynamicImport()
69
80
const exportModule = {...res}
70
81
// All npm packages pre-built by vite will be converted to esm
@@ -78,7 +89,8 @@ export function generateLocalSharedImportMap() {
78
89
},
79
90
shareConfig: {
80
91
singleton: ${ shareItem . shareConfig . singleton } ,
81
- requiredVersion: ${ JSON . stringify ( shareItem . shareConfig . requiredVersion ) }
92
+ requiredVersion: ${ JSON . stringify ( shareItem . shareConfig . requiredVersion ) } ,
93
+ ${ shareItem . shareConfig . import === false ? 'import: false,' : '' }
82
94
}
83
95
}
84
96
` ;
@@ -176,7 +188,7 @@ const hostAutoInitModule = new VirtualModule('hostAutoInit', HOST_AUTO_INIT_TAG)
176
188
export function writeHostAutoInit ( ) {
177
189
hostAutoInitModule . writeSync ( `
178
190
const remoteEntryPromise = import("${ REMOTE_ENTRY_ID } ")
179
- // __tla only serves as a hack for vite-plugin-top-level-await.
191
+ // __tla only serves as a hack for vite-plugin-top-level-await.
180
192
Promise.resolve(remoteEntryPromise)
181
193
.then(remoteEntry => {
182
194
return Promise.resolve(remoteEntry.__tla)
0 commit comments