Skip to content

Commit ca3a8c2

Browse files
committed
feat: add support for disabling imports for shared modules
- Add import flag to shareConfig interface and normalization - Handle import: false modules in virtual remote entry generation
1 parent f76706b commit ca3a8c2

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"packageManager": "[email protected]",
4848
"dependencies": {
4949
"@module-federation/runtime": "^0.17.1",
50+
"@module-federation/sdk": "^0.18.3",
5051
"@rollup/pluginutils": "^5.1.0",
5152
"defu": "^6.1.4",
5253
"estree-walker": "^2",

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/normalizeModuleFederationOptions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SharedConfig, ShareStrategy } from '@module-federation/runtime/types';
2+
import type { sharePlugin } from '@module-federation/sdk';
23

34
export type RemoteEntryType =
45
| 'var'
@@ -104,7 +105,7 @@ export interface ShareItem {
104105
version: string | undefined;
105106
scope: string;
106107
from: string;
107-
shareConfig: SharedConfig;
108+
shareConfig: SharedConfig & sharePlugin.SharedConfig;
108109
}
109110

110111
function removePathFromNpmPackage(packageString: string): string {
@@ -157,6 +158,7 @@ function normalizeShareItem(
157158
| string
158159
| {
159160
name: string;
161+
import: sharePlugin.SharedConfig['import'];
160162
version?: string;
161163
shareScope?: string;
162164
singleton?: boolean;
@@ -192,6 +194,7 @@ function normalizeShareItem(
192194
scope: 'default',
193195
from: '',
194196
shareConfig: {
197+
import: undefined,
195198
singleton: false,
196199
requiredVersion: version ? `^${version}` : '*',
197200
},
@@ -203,6 +206,7 @@ function normalizeShareItem(
203206
version: shareItem.version || version,
204207
scope: shareItem.shareScope || 'default',
205208
shareConfig: {
209+
import: typeof shareItem === 'object' ? shareItem.import : undefined,
206210
singleton: shareItem.singleton || false,
207211
requiredVersion: shareItem.requiredVersion || (version ? `^${version}` : '*'),
208212
strictVersion: !!shareItem.strictVersion,
@@ -289,6 +293,7 @@ export type ModuleFederationOptions = {
289293
singleton?: boolean;
290294
requiredVersion?: string;
291295
strictVersion?: boolean;
296+
import?: sharePlugin.SharedConfig['import'];
292297
}
293298
>
294299
| undefined;

src/virtualModules/virtualRemoteEntry.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
176188
export 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

Comments
 (0)