From 39fe32c8e867455825dccc3595fa4bf4b4404471 Mon Sep 17 00:00:00 2001 From: f4562 Date: Sat, 7 Dec 2024 07:38:24 +0100 Subject: [PATCH 1/5] fix: try to find shared package.json --- src/utils/normalizeModuleFederationOptions.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index 5e961cf..f3d7ab2 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -21,6 +21,7 @@ export type RemoteEntryType = | 'system' | string; +import { existsSync, readFileSync } from 'fs'; import * as path from 'pathe'; import { warn } from './logUtils'; @@ -117,6 +118,21 @@ function removePathFromNpmPackage(packageString: string): string { return match ? match[0] : packageString; } +function findPackageJson(moduleName: string) { + const mainFilePath = require.resolve(moduleName); + + let currentDir = path.dirname(mainFilePath); + while (currentDir !== path.parse(currentDir).root) { + const potentialPackageJsonPath = path.join(currentDir, 'package.json'); + if (existsSync(potentialPackageJsonPath)) { + return JSON.parse(readFileSync(potentialPackageJsonPath, 'utf8')); + } + currentDir = path.dirname(currentDir); + } + + throw new Error(`Unable to find package.json for the module "${moduleName}"`); +} + function normalizeShareItem( key: string, shareItem: @@ -132,7 +148,7 @@ function normalizeShareItem( ): ShareItem { let version: string | undefined; try { - version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version; + version = findPackageJson(removePathFromNpmPackage(key)).version; } catch (e) { console.log(e); } From d975fd3b8c7aa90d469e6ed004ba8041c2b8b560 Mon Sep 17 00:00:00 2001 From: f4562 Date: Thu, 12 Dec 2024 06:19:17 +0100 Subject: [PATCH 2/5] fix: prefer use require --- src/utils/normalizeModuleFederationOptions.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index f3d7ab2..3f79660 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -21,7 +21,7 @@ export type RemoteEntryType = | 'system' | string; -import { existsSync, readFileSync } from 'fs'; +import { existsSync } from 'fs'; import * as path from 'pathe'; import { warn } from './logUtils'; @@ -122,10 +122,13 @@ function findPackageJson(moduleName: string) { const mainFilePath = require.resolve(moduleName); let currentDir = path.dirname(mainFilePath); - while (currentDir !== path.parse(currentDir).root) { + while ( + path.parse(currentDir).base !== 'node_modules' && + currentDir !== path.parse(currentDir).root + ) { const potentialPackageJsonPath = path.join(currentDir, 'package.json'); if (existsSync(potentialPackageJsonPath)) { - return JSON.parse(readFileSync(potentialPackageJsonPath, 'utf8')); + return require(potentialPackageJsonPath); } currentDir = path.dirname(currentDir); } From 273cf70a73ff572b1eb0af9c83435182f30885b0 Mon Sep 17 00:00:00 2001 From: f4562 Date: Fri, 13 Dec 2024 15:53:23 +0100 Subject: [PATCH 3/5] fix: remove function removePathFromNpmPackage --- src/utils/normalizeModuleFederationOptions.ts | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index 3f79660..4fc7bbf 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -107,17 +107,6 @@ export interface ShareItem { shareConfig: SharedConfig; } -function removePathFromNpmPackage(packageString: string): string { - // 匹配npm包名的正则表达式,忽略路径部分 - const regex = /^(?:@[^/]+\/)?[^/]+/; - - // 使用正则表达式匹配并提取包名 - const match = packageString.match(regex); - - // 返回匹配到的包名,如果没有匹配到则返回原字符串 - return match ? match[0] : packageString; -} - function findPackageJson(moduleName: string) { const mainFilePath = require.resolve(moduleName); @@ -151,7 +140,7 @@ function normalizeShareItem( ): ShareItem { let version: string | undefined; try { - version = findPackageJson(removePathFromNpmPackage(key)).version; + version = findPackageJson(key).version; } catch (e) { console.log(e); } @@ -332,9 +321,7 @@ export function getNormalizeModuleFederationOptions() { export function getNormalizeShareItem(key: string) { const options = getNormalizeModuleFederationOptions(); - const shareItem = - options.shared[removePathFromNpmPackage(key)] || - options.shared[removePathFromNpmPackage(key) + '/']; + const shareItem = options.shared[key] || options.shared[key + '/']; return shareItem; } From fed459dbdba758d9ab4a98193e39da752a3e92d7 Mon Sep 17 00:00:00 2001 From: f4562 Date: Mon, 16 Dec 2024 06:17:13 +0100 Subject: [PATCH 4/5] fix: remove leading and trailing slash --- src/utils/normalizeModuleFederationOptions.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index 4fc7bbf..7469811 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -107,6 +107,10 @@ export interface ShareItem { shareConfig: SharedConfig; } +function cleanShareItem(key: string) { + return key.replace(/^\//, '').replace(/\/$/, ''); +} + function findPackageJson(moduleName: string) { const mainFilePath = require.resolve(moduleName); @@ -139,14 +143,15 @@ function normalizeShareItem( } ): ShareItem { let version: string | undefined; + const shareName = cleanShareItem(key); try { - version = findPackageJson(key).version; + version = findPackageJson(shareName).version; } catch (e) { console.log(e); } if (typeof shareItem === 'string') { return { - name: shareItem, + name: shareName, version, scope: 'default', from: '', @@ -157,7 +162,7 @@ function normalizeShareItem( }; } return { - name: key, + name: shareName, from: '', version: shareItem.version || version, scope: shareItem.shareScope || 'default', @@ -321,7 +326,7 @@ export function getNormalizeModuleFederationOptions() { export function getNormalizeShareItem(key: string) { const options = getNormalizeModuleFederationOptions(); - const shareItem = options.shared[key] || options.shared[key + '/']; + const shareItem = options.shared[cleanShareItem(key)]; return shareItem; } From 121dd524e7c25a4b5050cfc988618b1e7024be68 Mon Sep 17 00:00:00 2001 From: f4562 Date: Mon, 23 Dec 2024 07:06:07 +0100 Subject: [PATCH 5/5] fix: normalize shared with trailing slash --- src/plugins/pluginProxySharedModule_preBuild.ts | 4 +--- src/utils/normalizeModuleFederationOptions.ts | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/plugins/pluginProxySharedModule_preBuild.ts b/src/plugins/pluginProxySharedModule_preBuild.ts index bc41775..4039eb2 100644 --- a/src/plugins/pluginProxySharedModule_preBuild.ts +++ b/src/plugins/pluginProxySharedModule_preBuild.ts @@ -45,9 +45,7 @@ export function proxySharedModule(options: { config(config: UserConfig, { command }) { (config.resolve as any).alias.push( ...Object.keys(shared).map((key) => { - const pattern = key.endsWith('/') - ? `(^${key.replace(/\/$/, '')}(\/.+)?$)` - : `(^${key}$)`; + const pattern = key.endsWith('/') ? `(^${key.replace(/\/$/, '')}(\/)?$)` : `(^${key}$)`; return { // Intercept all shared requests and proxy them to loadShare find: new RegExp(pattern), diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index 7469811..1d31648 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -195,13 +195,13 @@ function normalizeShared( const result: NormalizedShared = {}; if (Array.isArray(shared)) { shared.forEach((key) => { - result[key] = normalizeShareItem(key, key); + result[cleanShareItem(key)] = normalizeShareItem(key, key); }); return result; } if (typeof shared === 'object') { Object.keys(shared).forEach((key) => { - result[key] = normalizeShareItem(key, shared[key] as any); + result[cleanShareItem(key)] = normalizeShareItem(key, shared[key] as any); }); }