Skip to content

Commit

Permalink
fix: updating mf/core packages (#53)
Browse files Browse the repository at this point in the history
* fix: updating mf/core packages

* fix: lint

* fix: code

* fix: lint

* fix: unit test
  • Loading branch information
MadaraUchiha-314 authored May 30, 2024
1 parent 5fdacd4 commit aed9193
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 249 deletions.
21 changes: 11 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions packages/rollup-plugin-module-federation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
]
},
"dependencies": {
"@module-federation/runtime": "0.0.10",
"@module-federation/sdk": "0.0.10",
"@module-federation/runtime": "0.1.15",
"@module-federation/sdk": "0.1.15",
"estree-walker": "3.0.3",
"magic-string": "0.30.1",
"semver": "7.5.4"
Expand All @@ -82,7 +82,8 @@
"tslib": "2.6.2",
"type-fest": "4.7.0",
"typescript": "5.2.2",
"vitest": "1.3.1"
"vitest": "1.3.1",
"@types/estree": "1.0.5"
},
"engines": {
"node": ">= 18.0.0"
Expand Down
1 change: 1 addition & 0 deletions packages/rollup-plugin-module-federation/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const PACKAGE_JSON: string = 'package.json';
export const DEFAULT_CONTAINER_NAME: string = 'default_container_name';
86 changes: 55 additions & 31 deletions packages/rollup-plugin-module-federation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { PACKAGE_JSON } from './constants';

import type { ImportDeclaration, ExportNamedDeclaration, Node } from 'estree';
import type { ModuleFederationPluginOptions } from '../types';
import { moduleFederationPlugin } from '@module-federation/sdk';
import type { PackageJson } from 'type-fest';
import type { Plugin, ManualChunksOption, AcornNode } from 'rollup';

Expand All @@ -31,6 +31,7 @@ import {
FederatedModuleType,
ModuleVersionInfo,
} from './types';
import { ShareArgs } from '@module-federation/runtime/types';

const IMPORTS_TO_FEDERATED_IMPORTS_NODES = {
ImportDeclaration: 'ImportDeclaration',
Expand Down Expand Up @@ -194,7 +195,7 @@ export function getFederatedImportStatementForNode(
}

export default function federation(
federationConfig: ModuleFederationPluginOptions,
federationConfig: moduleFederationPlugin.ModuleFederationPluginOptions,
): Plugin {
const { name, filename, shareScope = 'default' } = federationConfig;

Expand Down Expand Up @@ -456,6 +457,22 @@ export default function federation(
*/
remoteEntryCode.append(
Object.entries(initConfig?.shared ?? {})
.reduce<Array<[string, ShareArgs]>>(
(allSharedConfigs, [moduleNameOrPath, sharedConfigs]) => {
if (Array.isArray(sharedConfigs)) {
return allSharedConfigs.concat(
sharedConfigs.map((sharedConfigForPkg) => [
moduleNameOrPath,
sharedConfigForPkg,
]),
);
}
return allSharedConfigs.concat([
[moduleNameOrPath, sharedConfigs],
]);
},
[],
)
.filter(
([_, sharedConfigForPkg]) =>
sharedConfigForPkg.shareConfig?.eager,
Expand Down Expand Up @@ -496,36 +513,43 @@ export default function federation(
remotes: ${JSON.stringify(initConfig.remotes)},
shared: {
${Object.entries(initConfig.shared ?? {})
.map(([moduleNameOrPath, sharedConfigForPkg]) => {
return `
'${moduleNameOrPath}': {
${
/**
* We inject the entire object as json and then remote the starting and ending curly braces
* This is to add further keys to the object.
*/
JSON.stringify(sharedConfigForPkg).replace(
/^\{|\}$/g,
'',
)
},
version: '${getVersionForModule(moduleNameOrPath)}',
${
/**
* If the dependency is declared as a import: false, then we don't need to provide it to the initConfig.
* QUESTION: How does one even support import: false with this ?
* Bug: https://github.com/module-federation/universe/issues/2020
*/
!shared[moduleNameOrPath]?.import
? `get: () => Promise.resolve().then(() => () => null),`
: /**
* TODO: Convert this to a lib and re-write eager shared imports to loadShareSync()
.map(([moduleNameOrPath, sharedConfigs]) => {
return Array.isArray(sharedConfigs)
? sharedConfigs
: [sharedConfigs]
.map((sharedConfigForPkg) => {
return `
'${moduleNameOrPath}': {
${
/**
* We inject the entire object as json and then remote the starting and ending curly braces
* This is to add further keys to the object.
*/
sharedConfigForPkg.shareConfig?.eager
? `get: () => Promise.resolve(${FEDERATED_EAGER_SHARED}${moduleNameOrPath}).then((module) => () => module),`
: `get: () => import('${moduleNameOrPath}').then((module) => () => module),`
}
},`;
JSON.stringify(sharedConfigForPkg).replace(
/^\{|\}$/g,
'',
)
},
version: '${getVersionForModule(moduleNameOrPath)}',
${
/**
* If the dependency is declared as a import: false, then we don't need to provide it to the initConfig.
* QUESTION: How does one even support import: false with this ?
* Bug: https://github.com/module-federation/universe/issues/2020
*/
!shared[moduleNameOrPath]?.import
? `get: () => Promise.resolve().then(() => () => null),`
: /**
* TODO: Convert this to a lib and re-write eager shared imports to loadShareSync()
*/
sharedConfigForPkg.shareConfig?.eager
? `get: () => Promise.resolve(${FEDERATED_EAGER_SHARED}${moduleNameOrPath}).then((module) => () => module),`
: `get: () => import('${moduleNameOrPath}').then((module) => () => module),`
}
},
`;
})
.join('');
})
.join('')}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { ExposesConfig, SharedConfig, RemotesConfig } from '../../types';
import type { ShareArgs } from '@module-federation/runtime/dist/type.cjs.js';
import { moduleFederationPlugin } from '@module-federation/sdk';
import type { ShareArgs } from '@module-federation/runtime/types';
import type {
ImportDeclaration,
ImportExpression,
ExportNamedDeclaration,
ExportAllDeclaration,
} from 'estree';
/**
* We rewrite the type for SharedObject to be that of the most verbose definition.
*/
export type SharedObject = {
[index: string]: SharedConfig & {
[index: string]: moduleFederationPlugin.SharedConfig & {
import: string | false;
};
};
Expand All @@ -13,14 +19,14 @@ export type SharedObject = {
* We rewrite the type for ExposesObject to be that of the most verbose definition.
*/
export type ExposesObject = {
[index: string]: ExposesConfig;
[index: string]: moduleFederationPlugin.ExposesConfig;
};

/**
* We rewrite the type for RemotesObject to be that of the most verbose definition.
*/
export type RemotesObject = {
[index: string]: RemotesConfig;
[index: string]: moduleFederationPlugin.RemotesConfig;
};

export type ShareInfo = {
Expand Down
40 changes: 21 additions & 19 deletions packages/rollup-plugin-module-federation/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { dirname, sep } from 'node:path';
import { existsSync, readFileSync, lstatSync } from 'node:fs';
import { PACKAGE_JSON } from './constants';
import { PACKAGE_JSON, DEFAULT_CONTAINER_NAME } from './constants';
import {
generateExposeFilename,
generateShareFilename,
moduleFederationPlugin,
} from '@module-federation/sdk';
import type { UserOptions } from '@module-federation/runtime/dist/types.cjs.js';

import type { UserOptions } from '@module-federation/runtime/types';
import type { PackageJson } from 'type-fest';
import type { Exposes, Remotes, Shared } from '../types';
import type {
SharedObject,
ExposesObject,
Expand Down Expand Up @@ -70,7 +70,9 @@ export function getNearestPackageJson(path: string): PackageJson | null {
return getNearestPackageJson(parentDir);
}

export function getSharedConfig(shared: Shared): SharedObject {
export function getSharedConfig(
shared: moduleFederationPlugin.Shared,
): SharedObject {
if (Array.isArray(shared)) {
return shared.reduce<SharedObject>(
(sharedObject, sharedEntity): SharedObject => {
Expand Down Expand Up @@ -128,7 +130,9 @@ export function getSharedConfig(shared: Shared): SharedObject {
}
}

export function getExposesConfig(exposes: Exposes): ExposesObject {
export function getExposesConfig(
exposes: moduleFederationPlugin.Exposes,
): ExposesObject {
if (Array.isArray(exposes)) {
return exposes.reduce<ExposesObject>(
(exposedModules, exposedEntity): ExposesObject => {
Expand Down Expand Up @@ -184,7 +188,9 @@ export function getExposesConfig(exposes: Exposes): ExposesObject {
}
}

export function getRemotesConfig(remotes: Remotes): RemotesObject {
export function getRemotesConfig(
remotes: moduleFederationPlugin.Remotes,
): RemotesObject {
if (Array.isArray(remotes)) {
return remotes.reduce<RemotesObject>(
(remoteModules, remoteEntity): RemotesObject => {
Expand Down Expand Up @@ -259,16 +265,16 @@ export function getRequiredVersionForModule(
}

export function getInitConfig(
name: string,
name: string | undefined,
shared: SharedObject,
remotes: RemotesObject,
federatedModuleInfo: Record<string, FederatedModuleInfo>,
remoteType: string,
): UserOptions {
return {
name,
shared: Object.entries(shared).reduce<ShareInfo>(
(sharedConfig, [pkgName, sharedConfigForPkg]): ShareInfo => {
name: name ?? DEFAULT_CONTAINER_NAME,
shared: Object.entries(shared).reduce(
(sharedConfig, [pkgName, sharedConfigForPkg]) => {
const sharedOptionForPkg = {
version: sharedConfigForPkg.version as string,
shareConfig: {
Expand All @@ -286,11 +292,9 @@ export function getInitConfig(
/**
* If its a package for which the user has specified import: false, then we load whatever version is given to us from the shared scope.
*/
...(sharedConfigForPkg.import === false
? {
strategy: 'loaded-first',
}
: {}),
strategy: sharedConfigForPkg.import
? 'version-first'
: 'loaded-first',
};
return {
...sharedConfig,
Expand All @@ -301,14 +305,12 @@ export function getInitConfig(
},
{},
),
/**
* TODO: Find a type definition of how plugins can be injected during build time.
*/
plugins: [],
remotes: Object.entries(remotes).map(([remoteName, remoteConfig]) => {
return {
name: remoteName,
entry: remoteConfig.external,
// TODO: Remove this type coercion once we get an answer from module federation team.
entry: remoteConfig.external as string,
shareScope: remoteConfig.shareScope,
type:
remoteType === 'module' || remoteType === 'import' ? 'esm' : 'global',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ describe('getInitConfig', () => {
},
scope: undefined,
lib: expect.any(Function),
strategy: 'version-first',
},
sharedPkg2: {
version: undefined,
Expand Down
Loading

0 comments on commit aed9193

Please sign in to comment.