Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update normalizeModuleFederationOptions.ts
Browse files Browse the repository at this point in the history
mshima authored Oct 4, 2024
1 parent c531eee commit f6a1836
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/utils/normalizeModuleFederationOptions.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};

0 comments on commit f6a1836

Please sign in to comment.