diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index 72aaf6b..8a15439 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -108,7 +108,7 @@ export interface ShareItem { function removePathFromNpmPackage(packageString: string): string | undefined { // 匹配npm包名的正则表达式,忽略路径部分 - const regex = /^(?:@[^/]+\/)?(^@[^/]+)/; + const regex = /^(?:@[^/]+\/)?\w[^/]+/; // 使用正则表达式匹配并提取包名 const match = packageString.match(regex); @@ -130,13 +130,15 @@ function normalizeShareItem( strictVersion?: boolean; } ): ShareItem { - let version: string | undefined; - const npmPackage = removePathFromNpmPackage(key); - if (npmPackage) { - try { - version = require(path.join(npmPackage, 'package.json')).version; - } catch (e) { - console.log(e); + let version = typeof shareItem === 'object' ? shareItem.requiredVersion : undefined; + if (!version) { + const npmPackage = removePathFromNpmPackage(key); + if (npmPackage) { + try { + version = `^${require(path.join(npmPackage, 'package.json')).version}`; + } catch (e) { + console.log(e); + } } } if (typeof shareItem === 'string') { @@ -147,7 +149,7 @@ function normalizeShareItem( from: '', shareConfig: { singleton: false, - requiredVersion: version ? `^${version}` : '*', + requiredVersion: version ?? '*', }, }; } @@ -158,7 +160,7 @@ function normalizeShareItem( scope: shareItem.shareScope || 'default', shareConfig: { singleton: shareItem.singleton || false, - requiredVersion: shareItem.requiredVersion || (version ? `^${version}` : '*'), + requiredVersion: version ?? '*', strictVersion: !!shareItem.strictVersion, }, };