Skip to content

Commit

Permalink
fix: module-federation#180 Circular Import
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangHongEn committed Nov 25, 2024
1 parent 72403d5 commit 7b02a9a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/plugins/pluginAddEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const addEntry = ({
});
},
transformIndexHtml(c) {
if (inject !== 'html') return;
return c.replace(
'<head>',
`<head><script type="module" src=${JSON.stringify(
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/pluginProxyRemoteEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFed
import {
generateExposes,
generateRemoteEntry,
getHostAutoInitPath,
REMOTE_ENTRY_ID,
VIRTUAL_EXPOSES,
} from '../virtualModules';
Expand All @@ -12,16 +13,26 @@ import { parsePromise } from './pluginModuleParseEnd';
const filter: (id: string) => boolean = createFilter();

export default function (): Plugin {
let viteConfig: any, _command: string;
return {
name: 'proxyRemoteEntry',
enforce: 'post',
configResolved(config) {
viteConfig = config;
},
config(config, { command }) {
_command = command;
},
resolveId(id: string) {
if (id === REMOTE_ENTRY_ID) {
return REMOTE_ENTRY_ID;
}
if (id === VIRTUAL_EXPOSES) {
return VIRTUAL_EXPOSES;
}
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
return id;
}
},
load(id: string) {
if (id === REMOTE_ENTRY_ID) {
Expand All @@ -30,6 +41,9 @@ export default function (): Plugin {
if (id === VIRTUAL_EXPOSES) {
return generateExposes();
}
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
return id;
}
},
async transform(code: string, id: string) {
if (!filter(id)) return;
Expand All @@ -39,6 +53,16 @@ export default function (): Plugin {
if (id === VIRTUAL_EXPOSES) {
return generateExposes();
}
if (id.includes(getHostAutoInitPath())) {
const options = getNormalizeModuleFederationOptions();
if (_command === 'serve') {
return `
const {init} = await import("//localhost:${viteConfig.server?.port}${viteConfig.base + options.filename}")
init()
`;
}
return code;
}
},
};
}
19 changes: 16 additions & 3 deletions src/virtualModules/virtualRemoteEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,30 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
import {
initResolve
} from "${virtualRuntimeInitStatus.getImportId()}"
async function init(shared = {}) {
const initTokens = {}
const shareScopeName = ${JSON.stringify(options.shareScope)}
const mfName = ${JSON.stringify(options.name)}
async function init(shared = {}, initScope = []) {
const initRes = runtimeInit({
name: ${JSON.stringify(options.name)},
name: mfName,
remotes: usedRemotes,
shared: usedShared,
plugins: [${pluginImportNames.map((item) => `${item[0]}()`).join(', ')}],
${options.shareStrategy ? `shareStrategy: '${options.shareStrategy}'` : ''}
});
// handling circular init calls
var initToken = initTokens[shareScopeName];
if (!initToken)
initToken = initTokens[shareScopeName] = { from: mfName };
if (initScope.indexOf(initToken) >= 0) return;
initScope.push(initToken);
initRes.initShareScopeMap('${options.shareScope}', shared);
try {
await Promise.all(await initRes.initializeSharing('${options.shareScope}', {strategy: '${options.shareStrategy}'}));
await Promise.all(await initRes.initializeSharing('${options.shareScope}', {
strategy: '${options.shareStrategy}',
from: "build",
initScope
}));
} catch (e) {
console.error(e)
}
Expand Down

0 comments on commit 7b02a9a

Please sign in to comment.