Skip to content

Commit

Permalink
fix: #47 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangHongEn authored Aug 13, 2024
1 parent c7c2615 commit 10e1f83
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/virtualModules/virtualShared_preBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ const emptyNpmDir = resolve(require.resolve("an-empty-js-file"), "../")
/**
* The original shared module is proxied by getLoadShareModulePath, and the new shared module is prebuilt here
*/
const cacheMap2:Record<string, string> = {}
export function getPreBuildLibPath(pkg: string) : string {
const cacheMap2: Record<string, string> = {}
export function getPreBuildLibPath(pkg: string): string {
if (!cacheMap2[pkg]) cacheMap2[pkg] = `__mf__prebuildwrap_${npmPackageNameToFileName(pkg)}.js`
const filename = cacheMap2[pkg]
return filename
}

function getLocalSharedImportMapFileName() {
const {name} = getNormalizeModuleFederationOptions()
const { name } = getNormalizeModuleFederationOptions()
return npmPackageNameToFileName(name) + "_" + "__mf__localSharedImportMap.js"
}
// Only npm package name import can trigger pre-build, absolute path cannot
export function getLocalSharedImportMapId() {
return `an-empty-js-file/${getLocalSharedImportMapFileName()}`
}
export function getLocalSharedImportMapPath() {
return resolve(emptyNpmDir, getLocalSharedImportMapFileName())
return resolve(emptyNpmDir, getLocalSharedImportMapFileName())
}
export function writeLocalSharedImportMap(pkgList: string[]) {
writeFileSync(getLocalSharedImportMapPath(), `
Expand All @@ -46,16 +46,18 @@ export function writeLocalSharedImportMap(pkgList: string[]) {
/**
* generate loadShare virtual module
*/
const cacheMap1:Record<string, string> = {}
export function getLoadShareModulePath(pkg: string) : string {
const {name} = getNormalizeModuleFederationOptions()
const cacheMap1: Record<string, string> = {}
export function getLoadShareModulePath(pkg: string): string {
const { name } = getNormalizeModuleFederationOptions()
if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + `__mf__loadShare_${npmPackageNameToFileName(pkg)}.js`
const filename = cacheMap1[pkg]
return resolve(emptyNpmDir, filename)
}
export function writeLoadShareModule(pkg: string, shareItem: ShareItem, command: string) {

writeFileSync(getLoadShareModulePath(pkg), `
// dev uses dynamic import to separate chunks
${command !== "build" ? `;() => import(${JSON.stringify(pkg)}).catch(() => {});` : ''}
const {loadShare} = require("@module-federation/runtime")
const res = loadShare(${JSON.stringify(pkg)}, {
customShareInfo: {shareConfig:{
Expand Down

0 comments on commit 10e1f83

Please sign in to comment.