Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yummy-hounds-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/runtime-core': patch
---

fix(runtime-core): improve error messages clarity and fix preload resourceCategory bug
6 changes: 4 additions & 2 deletions packages/runtime-core/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ModuleInfo,
isDebugMode,
} from '@module-federation/sdk';
import { warn } from './utils/logger';
import { warn, error } from './utils/logger';
import { ModuleFederationRuntimePlugin } from './type/plugin';

export interface Federation {
Expand Down Expand Up @@ -171,7 +171,9 @@ export function getInfoWithoutType<T extends object>(
};
}
} else {
throw new Error('key must be string');
error(
`getInfoWithoutType: "key" must be a string, got ${typeof key} (${JSON.stringify(key)}).`,
);
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/runtime-core/src/plugins/generate-preload-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ export function generatePreloadAssets(
cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
jsAssets.push(...handleAssets(assetsInfo.assets.js.async));
jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
// eslint-disable-next-line no-constant-condition
} else if ((preloadConfig.resourceCategory = 'sync')) {
} else if (preloadConfig.resourceCategory === 'sync') {
cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
}
Expand Down
7 changes: 4 additions & 3 deletions packages/runtime-core/src/plugins/snapshot/SnapshotHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ export class SnapshotHandler {
} else {
error(
getShortErrorMsg(RUNTIME_007, runtimeDescMap, {
hostName: moduleInfo.name,
hostVersion: moduleInfo.version,
remoteName: moduleInfo.name,
remoteVersion: moduleInfo.version,
hostName: this.HostInstance.options.name,
globalSnapshot: JSON.stringify(globalSnapshotRes),
}),
);
Expand Down Expand Up @@ -324,7 +325,7 @@ export class SnapshotHandler {

assert(
manifestJson.metaData && manifestJson.exposes && manifestJson.shared,
`${manifestUrl} is not a federation manifest`,
`"${manifestUrl}" is not a valid federation manifest for remote "${moduleInfo.name}". Missing required fields: ${[!manifestJson.metaData && 'metaData', !manifestJson.exposes && 'exposes', !manifestJson.shared && 'shared'].filter(Boolean).join(', ')}.`,
);
this.manifestCache.set(manifestUrl, manifestJson);
return manifestJson;
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-core/src/remote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ export class RemoteHandler {
host.moduleCache.delete(remote.name);
}
} catch (err) {
logger.log('removeRemote fail: ', err);
logger.error(
`removeRemote failed: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
}
5 changes: 4 additions & 1 deletion packages/runtime-core/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class SharedHandler {
// Assert that shareInfoRes exists, if not, throw an error
assert(
shareOptionsRes,
`Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`,
`Cannot find shared "${pkgName}" in host "${host.options.name}". Ensure the shared config for "${pkgName}" is declared in the federation plugin options and the host has been initialized before loading shares.`,
);

const { shared: registeredShared, useTreesShaking } =
Expand Down Expand Up @@ -330,6 +330,9 @@ export class SharedHandler {
lifecycle: 'beforeLoadShare',
origin: host,
})) as RemoteEntryExports;
if (!remoteEntryExports) {
return;
}
} finally {
// prevent self load loop: when host load self , the initTokens is not the same
if (remoteEntryExports?.init && !module.initing) {
Expand Down
13 changes: 9 additions & 4 deletions packages/runtime-core/src/utils/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';
import { ModuleFederation } from '../core';
import { globalLoading, getRemoteEntryExports } from '../global';
import { Remote, RemoteEntryExports, RemoteInfo } from '../type';
import { assert } from './logger';
import { assert, error } from './logger';
import {
RUNTIME_001,
RUNTIME_008,
Expand Down Expand Up @@ -44,7 +44,8 @@ async function loadEsmEntry({
resolve(remoteEntryExports);
}
} catch (e) {
reject(e);
const msg = e instanceof Error ? e.message : String(e);
error(`Failed to load ESM entry from "${entry}". ${msg}`);
}
});
}
Expand Down Expand Up @@ -73,7 +74,8 @@ async function loadSystemJsEntry({
resolve(remoteEntryExports);
}
} catch (e) {
reject(e);
const msg = e instanceof Error ? e.message : String(e);
error(`Failed to load SystemJS entry from "${entry}". ${msg}`);
}
});
}
Expand Down Expand Up @@ -222,7 +224,10 @@ async function loadEntryNode({
return handleRemoteEntryLoaded(name, globalName, entry);
})
.catch((e) => {
throw e;
const msg = e instanceof Error ? e.message : String(e);
error(
`Failed to load Node.js entry for remote "${name}" from "${entry}". ${msg}`,
);
});
}

Expand Down
8 changes: 5 additions & 3 deletions packages/runtime-core/src/utils/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ function formatShare(
} else {
get = () =>
Promise.resolve(() => {
throw new Error(`Can not get shared '${name}'!`);
error(
`Cannot get shared "${name}" from "${from}": neither "get" nor "lib" is provided in the share config.`,
);
});
}

if (shareArgs.shareConfig?.eager && shareArgs.treeShaking?.mode) {
throw new Error(
'Can not set "eager:true" and "treeShaking" at the same time!',
error(
`Invalid shared config for "${name}" from "${from}": cannot use both "eager: true" and "treeShaking.mode" simultaneously. Choose one strategy.`,
);
}

Expand Down
Loading