Skip to content

Commit f6a1836

Browse files
authored
Update normalizeModuleFederationOptions.ts
1 parent c531eee commit f6a1836

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/utils/normalizeModuleFederationOptions.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export interface ShareItem {
108108

109109
function removePathFromNpmPackage(packageString: string): string | undefined {
110110
// 匹配npm包名的正则表达式,忽略路径部分
111-
const regex = /^(?:@[^/]+\/)?(^@[^/]+)/;
111+
const regex = /^(?:@[^/]+\/)?\w[^/]+/;
112112

113113
// 使用正则表达式匹配并提取包名
114114
const match = packageString.match(regex);
@@ -130,13 +130,15 @@ function normalizeShareItem(
130130
strictVersion?: boolean;
131131
}
132132
): ShareItem {
133-
let version: string | undefined;
134-
const npmPackage = removePathFromNpmPackage(key);
135-
if (npmPackage) {
136-
try {
137-
version = require(path.join(npmPackage, 'package.json')).version;
138-
} catch (e) {
139-
console.log(e);
133+
let version = typeof shareItem === 'object' ? shareItem.requiredVersion : undefined;
134+
if (!version) {
135+
const npmPackage = removePathFromNpmPackage(key);
136+
if (npmPackage) {
137+
try {
138+
version = `^${require(path.join(npmPackage, 'package.json')).version}`;
139+
} catch (e) {
140+
console.log(e);
141+
}
140142
}
141143
}
142144
if (typeof shareItem === 'string') {
@@ -147,7 +149,7 @@ function normalizeShareItem(
147149
from: '',
148150
shareConfig: {
149151
singleton: false,
150-
requiredVersion: version ? `^${version}` : '*',
152+
requiredVersion: version ?? '*',
151153
},
152154
};
153155
}
@@ -158,7 +160,7 @@ function normalizeShareItem(
158160
scope: shareItem.shareScope || 'default',
159161
shareConfig: {
160162
singleton: shareItem.singleton || false,
161-
requiredVersion: shareItem.requiredVersion || (version ? `^${version}` : '*'),
163+
requiredVersion: version ?? '*',
162164
strictVersion: !!shareItem.strictVersion,
163165
},
164166
};

0 commit comments

Comments
 (0)