Skip to content

Commit

Permalink
fix: eager shared modules use loadShareSync api
Browse files Browse the repository at this point in the history
  • Loading branch information
MadaraUchiha-314 committed Jul 31, 2024
1 parent 43c5611 commit 0661f06
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

19 changes: 12 additions & 7 deletions packages/rollup-plugin-module-federation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const REMOTE_ENTRY_FILE_NAME: string = `${REMOTE_ENTRY_MODULE_ID}.js`;
* All imports to shared/exposed/remotes will get converted to this expression.
*/
const FEDERATED_IMPORT_EXPR: string = 'loadShare';
const FEDERATED_EAGER_IMPORT_EXPR: string = 'loadShareSync';
const FEDERATED_IMPORT_FROM_REMOTE: string = 'loadRemote';
const FEDERATED_EAGER_SHARED: string = '__federated__shared__eager__';

Expand All @@ -75,7 +76,8 @@ export function getFederatedImportStatementForNode(
{
importStmt,
entityToImport,
}: { importStmt: string; entityToImport: string },
isAsync,
}: { importStmt: string; entityToImport: string, isAsync: boolean },
federatedModuleType: FederatedModuleType,
): string {
const federatedImportStms: Array<string> = [];
Expand All @@ -85,7 +87,7 @@ export function getFederatedImportStatementForNode(
* ES2015 Module spec: https://github.com/estree/estree/blob/master/es2015.md#modules
*/
const moduleSpecifier = `${importStmt}('${entityToImport}')`;
const getModuleOrFactoryAsync = `await ${moduleSpecifier}`;
const getModuleOrFactoryAsync = isAsync ? `await ${moduleSpecifier}`: moduleSpecifier;
/**
* loadRemote directly returns the module, while loadShare returns a factory which returns the module.
*/
Expand Down Expand Up @@ -529,8 +531,8 @@ export default function federation(
*/
remoteEntryCode.append(
`
const init = async (sharedScope) => {
const instance = await initModuleFederationRuntime({
const init = (sharedScope) => {
const instance = initModuleFederationRuntime({
name: '${initConfig.name}',
plugins: [
${(
Expand Down Expand Up @@ -571,7 +573,7 @@ export default function federation(
* 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),`
? `lib: () => ${FEDERATED_EAGER_SHARED}${moduleNameOrPath},`
: `get: () => import('${moduleNameOrPath}').then((module) => () => module),`
}
},
Expand Down Expand Up @@ -681,12 +683,14 @@ export default function federation(
resolvedModulePath
] as SharedOrExposedModuleInfo
).moduleNameOrPath;
const isEager = (federatedModuleInfo[resolvedModulePath] as SharedOrExposedModuleInfo).versionInfo.eager;
const federatedImportStmsStr =
getFederatedImportStatementForNode(
node as NodesToRewrite,
{
importStmt: FEDERATED_IMPORT_EXPR,
importStmt: isEager? FEDERATED_EAGER_IMPORT_EXPR: FEDERATED_IMPORT_EXPR,
entityToImport: chunkName,
isAsync: !isEager,
},
'shared',
);
Expand Down Expand Up @@ -726,6 +730,7 @@ export default function federation(
importStmt: FEDERATED_IMPORT_FROM_REMOTE,
// @ts-ignore
entityToImport: node?.source?.value,
isAsync: true,
},
'remote',
);
Expand All @@ -747,7 +752,7 @@ export default function federation(
/**
* Import from @module-federation/runtime
*/
import { ${FEDERATED_IMPORT_EXPR}, ${FEDERATED_IMPORT_FROM_REMOTE} } from '@module-federation/runtime';${EOL}
import { ${FEDERATED_IMPORT_EXPR}, ${FEDERATED_EAGER_IMPORT_EXPR}, ${FEDERATED_IMPORT_FROM_REMOTE} } from '@module-federation/runtime';${EOL}
`);
}
return {
Expand Down
3 changes: 0 additions & 3 deletions packages/rollup-plugin-module-federation/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,6 @@ export function getInitConfig(
eager: sharedConfigForPkg.eager,
},
scope: sharedConfigForPkg.shareScope,
// We just add a fake function that won't be used so that typescript is satisfied.
// We either need lib() or get() and we can't provide get()
lib: () => null,
/**
* 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.
*/
Expand Down

0 comments on commit 0661f06

Please sign in to comment.