Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangHongEn committed Aug 15, 2024
1 parent c4482ec commit c1974ce
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/virtualModules/virtualRemoteEntry.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NormalizedModuleFederationOptions } from "../utils/normalizeModuleFederationOptions";
import { getLocalSharedImportMapId } from "./virtualShared_preBuild";
import { NormalizedModuleFederationOptions } from '../utils/normalizeModuleFederationOptions';
import { getLocalSharedImportMapId } from './virtualShared_preBuild';
const emptyPath = require.resolve('an-empty-js-file');

export const REMOTE_ENTRY_ID = "REMOTE_ENTRY_ID"
export const REMOTE_ENTRY_ID = 'REMOTE_ENTRY_ID';
export function generateRemoteEntry(options: NormalizedModuleFederationOptions): string {
const pluginImportNames = options.runtimePlugins.map((p, i) => [
`$runtimePlugin_${i}`,
Expand Down Expand Up @@ -36,9 +36,9 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
async function init(shared = {}) {
const localShared = {
${Object.keys(options.shared)
.map((key) => {
const shareItem = options.shared[key];
return `
.map((key) => {
const shareItem = options.shared[key];
return `
${JSON.stringify(key)}: {
name: ${JSON.stringify(shareItem.name)},
version: ${JSON.stringify(shareItem.version)},
Expand All @@ -49,8 +49,14 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
localShared[${JSON.stringify(key)}].loaded = true
const {${JSON.stringify(key)}: pkgDynamicImport} = localSharedImportMap
const res = await pkgDynamicImport()
const exportModule = {...res}
// All npm packages pre-built by vite will be converted to esm
Object.defineProperty(exportModule, "__esModule", {
value: true,
enumerable: false
})
return function () {
return res
return exportModule
}
},
shareConfig: {
Expand All @@ -59,24 +65,24 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
}
}
`;
})
.join(',')}
})
.join(',')}
}
const initRes = runtimeInit({
name: ${JSON.stringify(options.name)},
remotes: [${Object.keys(options.remotes)
.map((key) => {
const remote = options.remotes[key];
return `
.map((key) => {
const remote = options.remotes[key];
return `
{
entryGlobalName: ${JSON.stringify(remote.entryGlobalName)},
name: ${JSON.stringify(remote.name)},
type: ${JSON.stringify(remote.type)},
entry: ${JSON.stringify(remote.entry)},
}
`;
})
.join(',')}
})
.join(',')}
],
shared: localShared,
plugins: [${pluginImportNames.map((item) => `${item[0]}()`).join(', ')}]
Expand All @@ -96,8 +102,8 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
`;
}

export const WRAP_REMOTE_ENTRY_QUERY_STR = "__mf__wrapRemoteEntry__"
export const WRAP_REMOTE_ENTRY_PATH = emptyPath + '?' + WRAP_REMOTE_ENTRY_QUERY_STR
export const WRAP_REMOTE_ENTRY_QUERY_STR = '__mf__wrapRemoteEntry__';
export const WRAP_REMOTE_ENTRY_PATH = emptyPath + '?' + WRAP_REMOTE_ENTRY_QUERY_STR;
export function generateWrapRemoteEntry(): string {
return `
import {init, get} from "${REMOTE_ENTRY_ID}"
Expand All @@ -106,11 +112,11 @@ export function generateWrapRemoteEntry(): string {
}

/**
* Inject entry file, automatically init when used as host,
* Inject entry file, automatically init when used as host,
* and will not inject remoteEntry
*/
export const HOST_AUTO_INIT_QUERY_STR = "__mf__isHostInit"
export const HOST_AUTO_INIT_PATH = emptyPath + '?' + HOST_AUTO_INIT_QUERY_STR
export const HOST_AUTO_INIT_QUERY_STR = '__mf__isHostInit';
export const HOST_AUTO_INIT_PATH = emptyPath + '?' + HOST_AUTO_INIT_QUERY_STR;
export function generateWrapHostInit(): string {
return `
import {init} from "${REMOTE_ENTRY_ID}"
Expand Down

0 comments on commit c1974ce

Please sign in to comment.