@@ -40,14 +40,19 @@ export function generateLocalSharedImportMap() {
4040 return `
4141 const importMap = {
4242 ${ Array . from ( getUsedShares ( ) )
43- . map (
44- ( pkg ) => `
43+ . map ( ( pkg ) => {
44+ const shareItem = getNormalizeShareItem ( pkg ) ;
45+ return `
4546 ${ JSON . stringify ( pkg ) } : async () => {
46- let pkg = await import("${ getPreBuildLibImportId ( pkg ) } ")
47- return pkg
47+ ${
48+ shareItem ?. shareConfig . import === false
49+ ? `throw new Error(\`Shared module '\${${ JSON . stringify ( pkg ) } }' must be provided by host\`);`
50+ : `let pkg = await import("${ getPreBuildLibImportId ( pkg ) } ");
51+ return pkg;`
52+ }
4853 }
49- `
50- )
54+ ` ;
55+ } )
5156 . join ( ',' ) }
5257 }
5358 const usedShared = {
@@ -63,8 +68,14 @@ export function generateLocalSharedImportMap() {
6368 loaded: false,
6469 from: ${ JSON . stringify ( options . name ) } ,
6570 async get () {
71+ if (${ shareItem . shareConfig . import === false } ) {
72+ const {loadShare} = require("@module-federation/runtime");
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