Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: monorepo #72

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
normalizeModuleFederationOptions
} 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 { getLocalSharedImportMapPath } from './virtualModules/virtualShared_preBuild';
import { getHostAutoInitImportId, getHostAutoInitPath, getLocalSharedImportMapPath, getWrapRemoteEntryImportId, getWrapRemoteEntryPath, initVirtualModules, REMOTE_ENTRY_ID } from './virtualModules';

function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
const options = normalizeModuleFederationOptions(mfUserOptions);
initVirtualModules()
const { name, remotes, shared, filename } = options;
if (!name) throw new Error("name is required")

Expand All @@ -23,17 +23,17 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
normalizeOptimizeDepsPlugin,
...addEntry({
entryName: 'remoteEntry',
entryPath: WRAP_REMOTE_ENTRY_PATH,
entryPath: getWrapRemoteEntryPath(),
fileName: filename,
}),
...addEntry({
entryName: 'hostInit',
entryPath: HOST_AUTO_INIT_PATH,
entryPath: getHostAutoInitPath(),
}),
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(getLocalSharedImportMapPath())
return id.includes(getHostAutoInitImportId()) || id.includes(getWrapRemoteEntryImportId()) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath())
})),
...proxySharedModule({
shared,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/pluginProxyRemoteEntry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createFilter } from '@rollup/pluginutils';
import { Plugin } from 'vite';
import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFederationOptions';
import { generateRemoteEntry, REMOTE_ENTRY_ID } from '../virtualModules/virtualRemoteEntry';
import { generateRemoteEntry, REMOTE_ENTRY_ID } from '../virtualModules';

const filter: (id: string) => boolean = createFilter();

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/pluginProxyRemotes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createFilter } from "@rollup/pluginutils";
import { Plugin } from "vite";
import { NormalizedModuleFederationOptions } from "../utils/normalizeModuleFederationOptions";
import { generateRemotes, remoteVirtualModule } from "../virtualModules/virtualRemotes";
import { generateRemotes, remoteVirtualModule } from "../virtualModules";
const filter: (id: string) => boolean = createFilter();

export default function (options: NormalizedModuleFederationOptions): Plugin {
Expand Down
18 changes: 10 additions & 8 deletions src/plugins/pluginProxySharedModule_preBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { defu } from 'defu';
import { walk } from 'estree-walker';
import MagicString from 'magic-string';
import { Plugin, UserConfig, WatchOptions } from 'vite';
import { NormalizedShared } from '../utils/normalizeModuleFederationOptions';
import { getNormalizeModuleFederationOptions, NormalizedShared } from '../utils/normalizeModuleFederationOptions';
import { packageNameDecode } from '../utils/packageNameUtils';
import { PromiseStore } from "../utils/PromiseStore";
import { virtualPackageName } from '../utils/VirtualModule';
import { wrapManualChunks } from '../utils/wrapManualChunks';
import { addShare, generateLocalSharedImportMap, getLoadShareModulePath, getLocalSharedImportMapPath, LOAD_SHARE_TAG, PREBUILD_TAG, writeLoadShareModule, writeLocalSharedImportMap, writePreBuildLibPath } from '../virtualModules/virtualShared_preBuild';
import { addShare, generateLocalSharedImportMap, getLoadShareModulePath, getLocalSharedImportMapPath, LOAD_SHARE_TAG, PREBUILD_TAG, writeLoadShareModule, writeLocalSharedImportMap, writePreBuildLibPath } from '../virtualModules';
export function proxySharedModule(
options: { shared?: NormalizedShared; include?: string | string[]; exclude?: string | string[] }
): Plugin[] {
let { shared = {}, include, exclude } = options;
const filterFunction = createFilter(include, exclude);
const { name } = getNormalizeModuleFederationOptions()
return [
{
name: "generateLocalSharedImportMap",
Expand Down Expand Up @@ -73,18 +73,20 @@ export function proxySharedModule(
...Object.keys(shared).map((key) => {
return command === "build" ?
{
find: new RegExp(`${virtualPackageName}/${PREBUILD_TAG}(.+)`), replacement: function (_: string, $1: string) {
return packageNameDecode($1)
find: new RegExp(`(.*${PREBUILD_TAG}(.+))`), replacement: function ($1: string) {
const pkgName = packageNameDecode($1.split(PREBUILD_TAG)[1])
return packageNameDecode(pkgName)
}
} :
{
find: new RegExp(`${virtualPackageName}/${PREBUILD_TAG}(.+)`), replacement: "$1", async customResolver(source: string, importer: string) {
find: new RegExp(`(.*${PREBUILD_TAG}(.+))`), replacement: "$1", async customResolver(source: string, importer: string) {
const pkgName = packageNameDecode(source.split(PREBUILD_TAG)[1])
if (importer.includes(LOAD_SHARE_TAG)) {
// save pre-bunding module id
savePrebuild.set(source, (this as any).resolve(packageNameDecode(source)).then((item: any) => item.id))
savePrebuild.set(pkgName, (this as any).resolve(pkgName).then((item: any) => item.id))
}
// Fix localSharedImportMap import id
return await (this as any).resolve(await savePrebuild.get(source))
return await (this as any).resolve(await savePrebuild.get(pkgName))
}
}
})
Expand Down
5 changes: 4 additions & 1 deletion src/utils/VirtualModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync, mkdirSync, writeFile, writeFileSync } from "fs";
import { dirname, join, parse, resolve } from "pathe";
import { packageNameEncode } from "../utils/packageNameUtils";
import { getNormalizeModuleFederationOptions } from "./normalizeModuleFederationOptions";

const nodeModulesDir = function findNodeModulesDir(startDir = process.cwd()) {
let currentDir = startDir;
Expand Down Expand Up @@ -38,7 +39,9 @@ export default class VirtualModule {
return resolve(nodeModulesDir, this.getImportId())
}
getImportId() {
return `${virtualPackageName}/${packageNameEncode(this.originName)}`
const { name } = getNormalizeModuleFederationOptions()

return `${virtualPackageName}/${packageNameEncode(name)}-${packageNameEncode(this.originName)}`
}
writeSync(code: string, force?: boolean) {
if (!force && this.inited) return
Expand Down
24 changes: 24 additions & 0 deletions src/virtualModules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { writeHostAutoInit, writeWrapRemoteEntry } from "./virtualRemoteEntry";
import { writeRemote } from "./virtualRemotes";
import { writeLocalSharedImportMap } from "./virtualShared_preBuild";

export {
generateRemoteEntry, getHostAutoInitImportId,
getHostAutoInitPath, getWrapRemoteEntryImportId,
getWrapRemoteEntryPath, REMOTE_ENTRY_ID
} from "./virtualRemoteEntry";

export {
generateRemotes, remoteVirtualModule
} from "./virtualRemotes";

export {
addShare, generateLocalSharedImportMap, getLoadShareModulePath, getLocalSharedImportMapPath, getPreBuildLibImportId, LOAD_SHARE_TAG, localSharedImportMapModule, PREBUILD_TAG, writeLoadShareModule, writeLocalSharedImportMap, writePreBuildLibPath
} from "./virtualShared_preBuild";

export function initVirtualModules() {
writeLocalSharedImportMap()
writeWrapRemoteEntry()
writeHostAutoInit()
writeRemote()
}
30 changes: 21 additions & 9 deletions src/virtualModules/virtualRemoteEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,33 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
}

const wrapRemoteEntryModule = new VirtualModule("wrapRemoteEntry")
wrapRemoteEntryModule.writeSync(`
import {init, get} from "${REMOTE_ENTRY_ID}"
export {init, get}
`)
export const WRAP_REMOTE_ENTRY_QUERY_STR = wrapRemoteEntryModule.getImportId();
export const WRAP_REMOTE_ENTRY_PATH = wrapRemoteEntryModule.getPath();
export function writeWrapRemoteEntry() {
wrapRemoteEntryModule.writeSync(`
import {init, get} from "${REMOTE_ENTRY_ID}"
export {init, get}
`)
}
export function getWrapRemoteEntryImportId() {
return wrapRemoteEntryModule.getImportId();
}
export function getWrapRemoteEntryPath() {
return wrapRemoteEntryModule.getPath();
}

/**
* Inject entry file, automatically init when used as host,
* and will not inject remoteEntry
*/
const hostAutoInitModule = new VirtualModule("hostAutoInit")
hostAutoInitModule.writeSync(`
export function writeHostAutoInit() {
hostAutoInitModule.writeSync(`
import {init} from "${REMOTE_ENTRY_ID}"
init()
`)
export const HOST_AUTO_INIT_QUERY_STR = hostAutoInitModule.getImportId();
export const HOST_AUTO_INIT_PATH = hostAutoInitModule.getPath();
}
export function getHostAutoInitImportId() {
return hostAutoInitModule.getImportId();
}
export function getHostAutoInitPath() {
return hostAutoInitModule.getPath();
}
4 changes: 3 additions & 1 deletion src/virtualModules/virtualRemotes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import VirtualModule from "../utils/VirtualModule";

export const remoteVirtualModule = new VirtualModule("remoteModule")
remoteVirtualModule.writeSync("")
export function writeRemote() {
remoteVirtualModule.writeSync("")
}
export function generateRemotes(id: string, command: string): { code: string; map: null; syntheticNamedExports: string } {
return {
code: `
Expand Down
1 change: 0 additions & 1 deletion src/virtualModules/virtualShared_preBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function addShare(pkg: string) {

// *** Expose locally provided shared modules here
export const localSharedImportMapModule = new VirtualModule("localSharedImportMap")
localSharedImportMapModule.writeSync("")
export function getLocalSharedImportMapPath() {
if (process.platform === "win32") {
return getLocalSharedImportMapPath_windows()
Expand Down