From fed459dbdba758d9ab4a98193e39da752a3e92d7 Mon Sep 17 00:00:00 2001 From: f4562 Date: Mon, 16 Dec 2024 06:17:13 +0100 Subject: [PATCH] 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; }