Skip to content

Commit

Permalink
fix: #68 (#71)
Browse files Browse the repository at this point in the history
* fix: #68

* fix: #68

* fix: windows Multi-Pack

* Optimization: Reduce file writing

* Optimization: code format

* fix: support nx monorepo

---------

Co-authored-by: 张洪恩 <[email protected]>
  • Loading branch information
zhangHongEn and 张洪恩 authored Aug 31, 2024
1 parent 0c81226 commit 1aa30c0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 42 deletions.
25 changes: 0 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from './utils/normalizeModuleFederationOptions';
import normalizeOptimizeDepsPlugin from './utils/normalizeOptimizeDeps';
import { HOST_AUTO_INIT_PATH, HOST_AUTO_INIT_QUERY_STR, REMOTE_ENTRY_ID, WRAP_REMOTE_ENTRY_PATH, WRAP_REMOTE_ENTRY_QUERY_STR } from './virtualModules/virtualRemoteEntry';
import { getLocalSharedImportMapId } from './virtualModules/virtualShared_preBuild';
import { getLocalSharedImportMapPath } from './virtualModules/virtualShared_preBuild';

function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
const options = normalizeModuleFederationOptions(mfUserOptions);
Expand All @@ -33,7 +33,7 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
pluginProxyRemoteEntry(),
pluginProxyRemotes(options),
...pluginModuleParseEnd(((id: string) => {
return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapId())
return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath())
})),
...proxySharedModule({
shared,
Expand Down
13 changes: 8 additions & 5 deletions src/plugins/pluginProxySharedModule_preBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { packageNameDecode } from '../utils/packageNameUtils';
import { PromiseStore } from "../utils/PromiseStore";
import { virtualPackageName } from '../utils/VirtualModule';
import { wrapManualChunks } from '../utils/wrapManualChunks';
import { addShare, generateLocalSharedImportMap, getLoadShareModulePath, getLocalSharedImportMapId, LOAD_SHARE_TAG, localSharedImportMapModule, PREBUILD_TAG, writeLoadShareModule, writeLocalSharedImportMap, writePreBuildLibPath } from '../virtualModules/virtualShared_preBuild';
import { addShare, generateLocalSharedImportMap, getLoadShareModulePath, getLocalSharedImportMapPath, LOAD_SHARE_TAG, PREBUILD_TAG, writeLoadShareModule, writeLocalSharedImportMap, writePreBuildLibPath } from '../virtualModules/virtualShared_preBuild';
export function proxySharedModule(
options: { shared?: NormalizedShared; include?: string | string[]; exclude?: string | string[] }
): Plugin[] {
Expand All @@ -19,16 +19,16 @@ export function proxySharedModule(
name: "generateLocalSharedImportMap",
enforce: "post",
resolveId(id) {
if (id.includes(getLocalSharedImportMapId()))
if (id.includes(getLocalSharedImportMapPath()))
return id
},
load(id) {
if (id.includes(getLocalSharedImportMapId())) {
if (id.includes(getLocalSharedImportMapPath())) {
return generateLocalSharedImportMap()
}
},
transform(code, id) {
if (id.includes(getLocalSharedImportMapId())) {
if (id.includes(getLocalSharedImportMapPath())) {
return generateLocalSharedImportMap()
}
}
Expand Down Expand Up @@ -95,14 +95,17 @@ export function proxySharedModule(
name: "watchLocalSharedImportMap",
apply: "serve",
config(config) {
config.optimizeDeps = defu(config.optimizeDeps, {
exclude: [getLocalSharedImportMapPath()]
});
config.server = defu(config.server, {
watch: {
ignored: [],
}
});
const watch = config.server.watch as WatchOptions
watch.ignored = [].concat(watch.ignored as any);
watch.ignored.push(`!**/node_modules/${localSharedImportMapModule.getImportId()}.js`);
watch.ignored.push(`!**${getLocalSharedImportMapPath()}**`);
},
},
{
Expand Down
7 changes: 6 additions & 1 deletion src/utils/VirtualModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ writeFileSync(resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.
*/
export default class VirtualModule {
originName: string
inited: boolean = false
constructor(name: string) {
this.originName = name
}
Expand All @@ -39,7 +40,11 @@ export default class VirtualModule {
getImportId() {
return `${virtualPackageName}/${packageNameEncode(this.originName)}`
}
writeSync(code: string) {
writeSync(code: string, force?: boolean) {
if (!force && this.inited) return
if (!this.inited) {
this.inited = true
}
writeFileSync(this.getPath() + ".js", code)
}
write(code: string) {
Expand Down
24 changes: 24 additions & 0 deletions src/utils/localSharedImportMap_windows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* https://github.com/module-federation/vite/issues/68
*/
import { mkdirSync, writeFileSync } from 'fs';
import path from 'pathe';
import { getNormalizeModuleFederationOptions } from './normalizeModuleFederationOptions';
import { packageNameEncode } from './packageNameUtils';


export function getLocalSharedImportMapPath_windows() {
const { name } = getNormalizeModuleFederationOptions()
return path.resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap")
}
export function writeLocalSharedImportMap_windows(content: string) {
const localSharedImportMapId = getLocalSharedImportMapPath_windows()
createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content)
}
function createFile(filePath: string, content: string) {
const dir = path.dirname(filePath);

mkdirSync(dir, { recursive: true });

writeFileSync(filePath, content);
}
14 changes: 7 additions & 7 deletions src/virtualModules/virtualRemoteEntry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NormalizedModuleFederationOptions } from '../utils/normalizeModuleFederationOptions';
import VirtualModule from '../utils/VirtualModule';
import { getLocalSharedImportMapId } from './virtualShared_preBuild';
import { getLocalSharedImportMapPath } from './virtualShared_preBuild';

export const REMOTE_ENTRY_ID = 'REMOTE_ENTRY_ID';
export function generateRemoteEntry(options: NormalizedModuleFederationOptions): string {
Expand Down Expand Up @@ -32,23 +32,23 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
})
.join(',')}
}
import localSharedImportMap from "${getLocalSharedImportMapId()}"
import localSharedImportMap from "${getLocalSharedImportMapPath()}"
async function init(shared = {}) {
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: localSharedImportMap,
plugins: [${pluginImportNames.map((item) => `${item[0]}()`).join(', ')}]
Expand Down
11 changes: 9 additions & 2 deletions src/virtualModules/virtualShared_preBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

import { parsePromise } from "../plugins/pluginModuleParseEnd";
import { getLocalSharedImportMapPath_windows, writeLocalSharedImportMap_windows } from "../utils/localSharedImportMap_windows";
import { getNormalizeModuleFederationOptions, ShareItem } from "../utils/normalizeModuleFederationOptions";
import { removePathFromNpmPackage } from "../utils/packageNameUtils";
import VirtualModule from "../utils/VirtualModule";
Expand All @@ -34,15 +35,21 @@ export function addShare(pkg: string) {
// *** Expose locally provided shared modules here
export const localSharedImportMapModule = new VirtualModule("localSharedImportMap")
localSharedImportMapModule.writeSync("")
export function getLocalSharedImportMapId() {
export function getLocalSharedImportMapPath() {
if (process.platform === "win32") {
return getLocalSharedImportMapPath_windows()
}
return localSharedImportMapModule.getPath()
}
let prevSharedCount = 0
export async function writeLocalSharedImportMap() {
const sharedCount = shareds.size
if (prevSharedCount !== sharedCount) {
prevSharedCount = sharedCount
return localSharedImportMapModule.writeSync(await generateLocalSharedImportMap())
if (process.platform === "win32") {
return writeLocalSharedImportMap_windows(await generateLocalSharedImportMap())
}
return localSharedImportMapModule.writeSync(await generateLocalSharedImportMap(), true)
}
}
export async function generateLocalSharedImportMap() {
Expand Down

0 comments on commit 1aa30c0

Please sign in to comment.