@@ -38,16 +38,22 @@ export function writeLocalSharedImportMap() {
3838export function generateLocalSharedImportMap ( ) {
3939 const options = getNormalizeModuleFederationOptions ( ) ;
4040 return `
41+ import {loadShare} from "@module-federation/runtime";
4142 const importMap = {
4243 ${ Array . from ( getUsedShares ( ) )
43- . map (
44- ( pkg ) => `
44+ . map ( ( pkg ) => {
45+ const shareItem = getNormalizeShareItem ( pkg ) ;
46+ return `
4547 ${ 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+ }
4854 }
49- `
50- )
55+ ` ;
56+ } )
5157 . join ( ',' ) }
5258 }
5359 const usedShared = {
@@ -63,8 +69,13 @@ export function generateLocalSharedImportMap() {
6369 loaded: false,
6470 from: ${ JSON . stringify ( options . name ) } ,
6571 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+ }
6677 usedShared[${ JSON . stringify ( key ) } ].loaded = true
67- const {${ JSON . stringify ( key ) } : pkgDynamicImport} = importMap
78+ const {${ JSON . stringify ( key ) } : pkgDynamicImport} = importMap
6879 const res = await pkgDynamicImport()
6980 const exportModule = {...res}
7081 // All npm packages pre-built by vite will be converted to esm
@@ -78,7 +89,8 @@ export function generateLocalSharedImportMap() {
7889 },
7990 shareConfig: {
8091 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,' : '' }
8294 }
8395 }
8496 ` ;
@@ -176,7 +188,7 @@ const hostAutoInitModule = new VirtualModule('hostAutoInit', HOST_AUTO_INIT_TAG)
176188export function writeHostAutoInit ( ) {
177189 hostAutoInitModule . writeSync ( `
178190 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.
180192 Promise.resolve(remoteEntryPromise)
181193 .then(remoteEntry => {
182194 return Promise.resolve(remoteEntry.__tla)
0 commit comments