Skip to content

Commit

Permalink
fix: vm
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeikim committed Feb 13, 2025
1 parent 7ccebc8 commit f278ee5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
normalizeModuleFederationOptions,
} from './utils/normalizeModuleFederationOptions';
import normalizeOptimizeDepsPlugin from './utils/normalizeOptimizeDeps';
import VirtualModule from './utils/VirtualModule';
import {
getHostAutoInitImportId,
getHostAutoInitPath,
Expand All @@ -24,11 +25,21 @@ import { VIRTUAL_EXPOSES } from './virtualModules/virtualExposes';

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

return [
{
name: 'vite:module-federation-config',
enforce: 'pre',
configResolved(config) {
// Set root path
VirtualModule.setRoot(config.root);
// Ensure virtual package directory exists
VirtualModule.ensureVirtualPackageExists();
initVirtualModules();
},
},
aliasToArrayPlugin,
normalizeOptimizeDepsPlugin,
...addEntry({
Expand Down
71 changes: 55 additions & 16 deletions src/utils/VirtualModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { dirname, join, parse, resolve } from 'pathe';
import { packageNameDecode, packageNameEncode } from '../utils/packageNameUtils';
import { getNormalizeModuleFederationOptions } from './normalizeModuleFederationOptions';

const nodeModulesDir = (function findNodeModulesDir(startDir = process.cwd()) {
let currentDir = startDir;
// Cache root path
let rootDir: string | undefined;

function findNodeModulesDir(root: string = process.cwd()) {
let currentDir = root;

while (currentDir !== parse(currentDir).root) {
const nodeModulesPath = join(currentDir, 'node_modules');
Expand All @@ -15,19 +18,19 @@ const nodeModulesDir = (function findNodeModulesDir(startDir = process.cwd()) {
}

return '';
})();
export const virtualPackageName = '__mf__virtual';
if (!existsSync(resolve(nodeModulesDir, virtualPackageName))) {
mkdirSync(resolve(nodeModulesDir, virtualPackageName));
}
writeFileSync(resolve(nodeModulesDir, virtualPackageName, 'empty.js'), '');
writeFileSync(
resolve(nodeModulesDir, virtualPackageName, 'package.json'),
JSON.stringify({
name: virtualPackageName,
main: 'empty.js',
})
);

// Cache nodeModulesDir result to avoid repeated calculations
let cachedNodeModulesDir: string | undefined;

function getNodeModulesDir() {
if (!cachedNodeModulesDir) {
cachedNodeModulesDir = findNodeModulesDir(rootDir);
}
return cachedNodeModulesDir;
}

export const virtualPackageName = '__mf__virtual';

const patternMap: {
[tag: string]: RegExp;
Expand All @@ -38,6 +41,7 @@ const cacheMap: {
[name: string]: VirtualModule;
};
} = {};

/**
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
*/
Expand All @@ -46,6 +50,37 @@ export default class VirtualModule {
tag: string;
suffix: string;
inited: boolean = false;

/**
* Set the root path for finding node_modules
* @param root - Root path
*/
static setRoot(root: string) {
rootDir = root;
// Reset cache to ensure using the new root path
cachedNodeModulesDir = undefined;
}

/**
* Ensure virtual package directory exists
*/
static ensureVirtualPackageExists() {
const nodeModulesDir = getNodeModulesDir();
const virtualPackagePath = resolve(nodeModulesDir, virtualPackageName);

if (!existsSync(virtualPackagePath)) {
mkdirSync(virtualPackagePath);
writeFileSync(resolve(virtualPackagePath, 'empty.js'), '');
writeFileSync(
resolve(virtualPackagePath, 'package.json'),
JSON.stringify({
name: virtualPackageName,
main: 'empty.js',
})
);
}
}

static findModule(tag: string, str: string = ''): VirtualModule | undefined {
if (!patternMap[tag])
patternMap[tag] = new RegExp(`(.*${packageNameEncode(tag)}(.+?)${packageNameEncode(tag)}.*)`);
Expand All @@ -54,28 +89,32 @@ export default class VirtualModule {
return cacheMap[tag][packageNameDecode(moduleName)] as VirtualModule | undefined;
return undefined;
}

constructor(name: string, tag: string = '__mf_v__', suffix = '') {
this.name = name;
this.tag = tag;
this.suffix = suffix || name.split('.').slice(1).pop()?.replace(/(.)/, '.$1') || '.js';
if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
cacheMap[this.tag][this.name] = this;
}

getPath() {
return resolve(nodeModulesDir, this.getImportId());
return resolve(getNodeModulesDir(), this.getImportId());
}

getImportId() {
const { name: mfName } = getNormalizeModuleFederationOptions();

return `${virtualPackageName}/${packageNameEncode(`${mfName}${this.tag}${this.name}${this.tag}`)}${this.suffix}`;
}

writeSync(code: string, force?: boolean) {
if (!force && this.inited) return;
if (!this.inited) {
this.inited = true;
}
writeFileSync(this.getPath(), code);
}

write(code: string) {
writeFile(this.getPath(), code, function () {});
}
Expand Down

0 comments on commit f278ee5

Please sign in to comment.