Skip to content

Commit 326fce5

Browse files
committed
fix: use provided version
1 parent 6360a21 commit 326fce5

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/utils/__tests__/normalizeModuleFederationOption.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('normalizeModuleFederationOption', () => {
159159
scope: 'default',
160160
version: '1.0.0',
161161
shareConfig: {
162-
requiredVersion: '*',
162+
requiredVersion: '^1.0.0',
163163
singleton: false,
164164
strictVersion: false,
165165
},

src/utils/normalizeModuleFederationOptions.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ export interface ShareItem {
106106
shareConfig: SharedConfig;
107107
}
108108

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

113113
// 使用正则表达式匹配并提取包名
114114
const match = packageString.match(regex);
115115

116116
// 返回匹配到的包名,如果没有匹配到则返回原字符串
117-
return match ? match[0] : packageString;
117+
return match?.[0];
118118
}
119119

120120
function normalizeShareItem(
@@ -130,12 +130,18 @@ function normalizeShareItem(
130130
strictVersion?: boolean;
131131
}
132132
): ShareItem {
133-
let version: string | undefined;
134-
try {
135-
version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version;
136-
} catch (e) {
137-
console.log(e);
133+
let { version, requiredVersion } = typeof shareItem === 'object' ? shareItem : {};
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+
}
142+
}
138143
}
144+
requiredVersion ??= version ? `^${version}` : '*';
139145
if (typeof shareItem === 'string') {
140146
return {
141147
name: shareItem,
@@ -144,18 +150,18 @@ function normalizeShareItem(
144150
from: '',
145151
shareConfig: {
146152
singleton: false,
147-
requiredVersion: version ? `^${version}` : '*',
153+
requiredVersion,
148154
},
149155
};
150156
}
151157
return {
152158
name: key,
153159
from: '',
154-
version: shareItem.version || version,
160+
version: shareItem.version ?? version,
155161
scope: shareItem.shareScope || 'default',
156162
shareConfig: {
157163
singleton: shareItem.singleton || false,
158-
requiredVersion: shareItem.requiredVersion || (version ? `^${version}` : '*'),
164+
requiredVersion,
159165
strictVersion: !!shareItem.strictVersion,
160166
},
161167
};
@@ -313,10 +319,8 @@ export function getNormalizeModuleFederationOptions() {
313319

314320
export function getNormalizeShareItem(key: string) {
315321
const options = getNormalizeModuleFederationOptions();
316-
const shareItem =
317-
options.shared[removePathFromNpmPackage(key)] ||
318-
options.shared[removePathFromNpmPackage(key) + '/'];
319-
return shareItem;
322+
const sharedKey = removePathFromNpmPackage(key) ?? key;
323+
return options.shared[sharedKey];
320324
}
321325

322326
export function normalizeModuleFederationOptions(

0 commit comments

Comments
 (0)