From c1974cebf87935c1f6528a1ff6ad39e67b3f2411 Mon Sep 17 00:00:00 2001 From: zhangHongEn Date: Thu, 15 Aug 2024 22:53:16 +0800 Subject: [PATCH] fix: #56 --- src/virtualModules/virtualRemoteEntry.ts | 44 ++++++++++++++---------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/virtualModules/virtualRemoteEntry.ts b/src/virtualModules/virtualRemoteEntry.ts index d720483..9d3e3da 100644 --- a/src/virtualModules/virtualRemoteEntry.ts +++ b/src/virtualModules/virtualRemoteEntry.ts @@ -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}`, @@ -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)}, @@ -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: { @@ -59,15 +65,15 @@ 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)}, @@ -75,8 +81,8 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions): entry: ${JSON.stringify(remote.entry)}, } `; - }) - .join(',')} + }) + .join(',')} ], shared: localShared, plugins: [${pluginImportNames.map((item) => `${item[0]}()`).join(', ')}] @@ -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}" @@ -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}"