Skip to content

Commit a8a0102

Browse files
authored
fix: manage implementation option + fix circular import issue (#191)
1 parent 72403d5 commit a8a0102

File tree

7 files changed

+52
-8
lines changed

7 files changed

+52
-8
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export default defineConfig({
6767
shared: ["vue"],
6868
}),
6969
],
70+
server: {
71+
origin: "http://localhost:{Your port}"
72+
},
7073
// Do you need to support build targets lower than chrome89?
7174
// You can use 'vite-plugin-top-level-await' plugin for that.
7275
build: {
@@ -106,6 +109,9 @@ export default defineConfig({
106109
shared: ["vue"],
107110
}),
108111
],
112+
server: {
113+
origin: "http://localhost:{Your port}"
114+
},
109115
// Do you need to support build targets lower than chrome89?
110116
// You can use 'vite-plugin-top-level-await' plugin for that.
111117
build: {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
6868
// TODO: singleton
6969
(config.resolve as any).alias.push({
7070
find: '@module-federation/runtime',
71-
replacement: require.resolve('@module-federation/runtime'),
71+
replacement: options.implementation,
7272
});
7373

7474
config.optimizeDeps?.include?.push('@module-federation/runtime');

src/plugins/pluginAddEntry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const addEntry = ({
5555
});
5656
},
5757
transformIndexHtml(c) {
58+
if (inject !== 'html') return;
5859
return c.replace(
5960
'<head>',
6061
`<head><script type="module" src=${JSON.stringify(

src/plugins/pluginProxyRemoteEntry.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFed
44
import {
55
generateExposes,
66
generateRemoteEntry,
7+
getHostAutoInitPath,
78
REMOTE_ENTRY_ID,
89
VIRTUAL_EXPOSES,
910
} from '../virtualModules';
@@ -12,16 +13,26 @@ import { parsePromise } from './pluginModuleParseEnd';
1213
const filter: (id: string) => boolean = createFilter();
1314

1415
export default function (): Plugin {
16+
let viteConfig: any, _command: string;
1517
return {
1618
name: 'proxyRemoteEntry',
1719
enforce: 'post',
20+
configResolved(config) {
21+
viteConfig = config;
22+
},
23+
config(config, { command }) {
24+
_command = command;
25+
},
1826
resolveId(id: string) {
1927
if (id === REMOTE_ENTRY_ID) {
2028
return REMOTE_ENTRY_ID;
2129
}
2230
if (id === VIRTUAL_EXPOSES) {
2331
return VIRTUAL_EXPOSES;
2432
}
33+
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
34+
return id;
35+
}
2536
},
2637
load(id: string) {
2738
if (id === REMOTE_ENTRY_ID) {
@@ -30,6 +41,9 @@ export default function (): Plugin {
3041
if (id === VIRTUAL_EXPOSES) {
3142
return generateExposes();
3243
}
44+
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
45+
return id;
46+
}
3347
},
3448
async transform(code: string, id: string) {
3549
if (!filter(id)) return;
@@ -39,6 +53,16 @@ export default function (): Plugin {
3953
if (id === VIRTUAL_EXPOSES) {
4054
return generateExposes();
4155
}
56+
if (id.includes(getHostAutoInitPath())) {
57+
const options = getNormalizeModuleFederationOptions();
58+
if (_command === 'serve') {
59+
return `
60+
const {init} = await import("//localhost:${viteConfig.server?.port}${viteConfig.base + options.filename}")
61+
init()
62+
`;
63+
}
64+
return code;
65+
}
4266
},
4367
};
4468
}

src/utils/__tests__/normalizeModuleFederationOption.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('normalizeModuleFederationOption', () => {
2020
shared: {},
2121
runtime: undefined,
2222
runtimePlugins: [],
23-
implementation: undefined,
23+
implementation: require.resolve('@module-federation/runtime'),
2424
manifest: false,
2525
dev: undefined,
2626
dts: undefined,

src/utils/normalizeModuleFederationOptions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export type ModuleFederationOptions = {
245245
| undefined;
246246
runtimePlugins?: string[];
247247
getPublicPath?: string;
248-
implementation?: any;
248+
implementation?: string;
249249
manifest?: ManifestOptions | boolean;
250250
dev?: boolean | PluginDevOptions;
251251
dts?: boolean | PluginDtsOptions;
@@ -263,7 +263,7 @@ export interface NormalizedModuleFederationOptions {
263263
shareScope: string;
264264
shared: NormalizedShared;
265265
runtimePlugins: string[];
266-
implementation: any;
266+
implementation: string;
267267
manifest: ManifestOptions | boolean;
268268
dev?: boolean | PluginDevOptions;
269269
dts?: boolean | PluginDtsOptions;
@@ -338,7 +338,7 @@ export function normalizeModuleFederationOptions(
338338
shareScope: options.shareScope || 'default',
339339
shared: normalizeShared(options.shared),
340340
runtimePlugins: options.runtimePlugins || [],
341-
implementation: options.implementation,
341+
implementation: options.implementation || require.resolve('@module-federation/runtime'),
342342
manifest: normalizeManifest(options.manifest),
343343
dev: options.dev,
344344
dts: options.dts,

src/virtualModules/virtualRemoteEntry.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,30 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
120120
import {
121121
initResolve
122122
} from "${virtualRuntimeInitStatus.getImportId()}"
123-
async function init(shared = {}) {
123+
const initTokens = {}
124+
const shareScopeName = ${JSON.stringify(options.shareScope)}
125+
const mfName = ${JSON.stringify(options.name)}
126+
async function init(shared = {}, initScope = []) {
124127
const initRes = runtimeInit({
125-
name: ${JSON.stringify(options.name)},
128+
name: mfName,
126129
remotes: usedRemotes,
127130
shared: usedShared,
128131
plugins: [${pluginImportNames.map((item) => `${item[0]}()`).join(', ')}],
129132
${options.shareStrategy ? `shareStrategy: '${options.shareStrategy}'` : ''}
130133
});
134+
// handling circular init calls
135+
var initToken = initTokens[shareScopeName];
136+
if (!initToken)
137+
initToken = initTokens[shareScopeName] = { from: mfName };
138+
if (initScope.indexOf(initToken) >= 0) return;
139+
initScope.push(initToken);
131140
initRes.initShareScopeMap('${options.shareScope}', shared);
132141
try {
133-
await Promise.all(await initRes.initializeSharing('${options.shareScope}', {strategy: '${options.shareStrategy}'}));
142+
await Promise.all(await initRes.initializeSharing('${options.shareScope}', {
143+
strategy: '${options.shareStrategy}',
144+
from: "build",
145+
initScope
146+
}));
134147
} catch (e) {
135148
console.error(e)
136149
}

0 commit comments

Comments
 (0)