Skip to content

Commit

Permalink
fix(managers): correct manifest shared configuration (#3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 authored Nov 13, 2024
1 parent c2b1efa commit 7facc10
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-yaks-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/managers': patch
---

fix(managers): correct manifest shared configuration
2 changes: 2 additions & 0 deletions packages/managers/__tests__/SharedManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('SharedManager', () => {
name: '@module-federation/shared-managers-test',
shared: {
react: {},
'react-dom': { singleton: false, requiredVersion: '^18.0.0' },
},
};
const sharedManager = new SharedManager();
Expand All @@ -33,6 +34,7 @@ describe('SharedManager', () => {
].includes(key),
),
).toEqual(true);
expect(sharedManager.normalizedOptions['react-dom']).toMatchSnapshot();
});

it('sharedPluginOptions', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SharedManager normalizedOptions 1`] = `
{
"eager": false,
"name": "react-dom",
"requiredVersion": "^18.0.0",
"shareScope": "default",
"singleton": false,
"version": "18.3.1",
}
`;
48 changes: 27 additions & 21 deletions packages/managers/src/ContainerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ManifestModuleInfos,
moduleFederationPlugin,
} from '@module-federation/sdk';
import { getBuildVersion, parseOptions } from './utils';
import { parseOptions } from './utils';
import type { EntryObject } from 'webpack';
import { BasicPluginOptionsManager } from './BasicPluginOptionsManager';

Expand Down Expand Up @@ -65,7 +65,7 @@ class ContainerManager extends BasicPluginOptionsManager<moduleFederationPlugin.
const [exposeKey, exposeObj] = item;
sum[exposeKey] = exposeObj;
return sum;
}, {});
}, {} as moduleFederationPlugin.ExposesObject);
}
// { '.' : './src/Button.jsx' } => { '__federation_expose_Component' : ['src/Buttton'] }
get exposeFileNameImportMap(): Record<string, string[]> {
Expand All @@ -81,30 +81,36 @@ class ContainerManager extends BasicPluginOptionsManager<moduleFederationPlugin.
name: item.name || generateExposeFilename(key, false),
}),
);
return parsedOptions.reduce((sum, item) => {
const [_exposeKey, exposeObj] = item;
const { name, import: importPath } = exposeObj;
sum[name] = importPath;
return sum;
}, {});
return parsedOptions.reduce(
(sum, item) => {
const [_exposeKey, exposeObj] = item;
const { name, import: importPath } = exposeObj;
sum[name] = importPath;
return sum;
},
{} as Record<string, string[]>,
);
}

// { '.' : './src/Button.jsx' } => { '.' : ['src/Button'] }
get exposeObject(): Record<string, string> {
get exposeObject(): Record<string, string[]> {
const parsedOptions = this._parseOptions();

return parsedOptions.reduce((sum, item) => {
const [exposeKey, exposeObject] = item;
sum[exposeKey] = [];
exposeObject.import.forEach((item) => {
const relativePath = path.relative(
'.',
item.replace(path.extname(item), ''),
);
sum[exposeKey].push(relativePath);
});
return sum;
}, {});
return parsedOptions.reduce(
(sum, item) => {
const [exposeKey, exposeObject] = item;
sum[exposeKey] = [];
exposeObject.import.forEach((item) => {
const relativePath = path.relative(
'.',
item.replace(path.extname(item), ''),
);
sum[exposeKey].push(relativePath);
});
return sum;
},
{} as Record<string, string[]>,
);
}

// { '.' : './src/Button.jsx' } => ['./src/Button.jsx']
Expand Down
1 change: 1 addition & 0 deletions packages/managers/src/PKGJsonManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
// @ts-ignore this pkg miss types
import finder from 'find-pkg';
import fs from 'fs';
import { MFModuleType, logger } from '@module-federation/sdk';
Expand Down
19 changes: 11 additions & 8 deletions packages/managers/src/RemoteManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ class RemoteManager extends BasicPluginOptionsManager<moduleFederationPlugin.Mod
// 'micro-app-sub3': @garfish/micro-app-sub3:0.0.4
// }
get dtsRemotes(): Record<string, string> {
return Object.keys(this.normalizedOptions).reduce((sum, remoteAlias) => {
const remoteInfo = this.normalizedOptions[remoteAlias];
sum[remoteAlias] = composeKeyWithSeparator(
remoteInfo.name,
'entry' in remoteInfo ? remoteInfo.entry : remoteInfo.version,
);
return sum;
}, {});
return Object.keys(this.normalizedOptions).reduce(
(sum, remoteAlias) => {
const remoteInfo = this.normalizedOptions[remoteAlias];
sum[remoteAlias] = composeKeyWithSeparator(
remoteInfo.name,
'entry' in remoteInfo ? remoteInfo.entry : remoteInfo.version,
);
return sum;
},
{} as Record<string, string>,
);
}

get remotes(): moduleFederationPlugin.ModuleFederationPluginOptions['remotes'] {
Expand Down
7 changes: 3 additions & 4 deletions packages/managers/src/SharedManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-ignore this pkg miss types
import findPkg from 'find-pkg';
import path from 'path';
import fs from 'fs-extra';
Expand Down Expand Up @@ -40,7 +41,7 @@ class SharedManager extends BasicPluginOptionsManager<moduleFederationPlugin.Mod
import: sharedImport,
};
return sum;
}, {});
}, {} as moduleFederationPlugin.SharedObject);
return {
shared,
shareScope: this.options.shareScope || 'default',
Expand Down Expand Up @@ -127,9 +128,7 @@ class SharedManager extends BasicPluginOptionsManager<moduleFederationPlugin.Mod
sharedOptions.forEach((item) => {
const [sharedName, sharedOptions] = item;
const pkgInfo = this.findPkg(sharedName, sharedOptions);
const sharedConfig = this.transformSharedConfig(
sharedOptions[sharedName],
);
const sharedConfig = this.transformSharedConfig(sharedOptions);
normalizedShared[sharedName] = {
...sharedConfig,
requiredVersion:
Expand Down
2 changes: 1 addition & 1 deletion packages/managers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"declaration": true,
"noImplicitAny": false
"noImplicitAny": true
},
"files": [],
"include": [],
Expand Down

0 comments on commit 7facc10

Please sign in to comment.