@module-federation/runtime-core 0.0.0-chore-bump-node-22-20260710161714
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/LICENSE +21 -0
- package/dist/_virtual/_rolldown/runtime.cjs +19 -0
- package/dist/_virtual/_rolldown/runtime.js +18 -0
- package/dist/constant.cjs +9 -0
- package/dist/constant.cjs.map +1 -0
- package/dist/constant.js +7 -0
- package/dist/constant.js.map +1 -0
- package/dist/core.cjs +161 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.ts +197 -0
- package/dist/core.js +161 -0
- package/dist/core.js.map +1 -0
- package/dist/global.cjs +160 -0
- package/dist/global.cjs.map +1 -0
- package/dist/global.d.ts +45 -0
- package/dist/global.js +142 -0
- package/dist/global.js.map +1 -0
- package/dist/helpers.cjs +44 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.ts +30 -0
- package/dist/helpers.js +44 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.cjs +60 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/module/index.cjs +266 -0
- package/dist/module/index.cjs.map +1 -0
- package/dist/module/index.d.ts +32 -0
- package/dist/module/index.js +266 -0
- package/dist/module/index.js.map +1 -0
- package/dist/plugins/generate-preload-assets.cjs +182 -0
- package/dist/plugins/generate-preload-assets.cjs.map +1 -0
- package/dist/plugins/generate-preload-assets.js +182 -0
- package/dist/plugins/generate-preload-assets.js.map +1 -0
- package/dist/plugins/snapshot/SnapshotHandler.cjs +215 -0
- package/dist/plugins/snapshot/SnapshotHandler.cjs.map +1 -0
- package/dist/plugins/snapshot/SnapshotHandler.d.ts +69 -0
- package/dist/plugins/snapshot/SnapshotHandler.js +214 -0
- package/dist/plugins/snapshot/SnapshotHandler.js.map +1 -0
- package/dist/plugins/snapshot/index.cjs +67 -0
- package/dist/plugins/snapshot/index.cjs.map +1 -0
- package/dist/plugins/snapshot/index.js +66 -0
- package/dist/plugins/snapshot/index.js.map +1 -0
- package/dist/remote/index.cjs +445 -0
- package/dist/remote/index.cjs.map +1 -0
- package/dist/remote/index.d.ts +145 -0
- package/dist/remote/index.js +445 -0
- package/dist/remote/index.js.map +1 -0
- package/dist/shared/index.cjs +418 -0
- package/dist/shared/index.cjs.map +1 -0
- package/dist/shared/index.d.ts +104 -0
- package/dist/shared/index.js +418 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/type/config.d.ts +130 -0
- package/dist/type/index.cjs +13 -0
- package/dist/type/index.cjs.map +1 -0
- package/dist/type/index.d.ts +11 -0
- package/dist/type/index.js +8 -0
- package/dist/type/index.js.map +1 -0
- package/dist/type/plugin.d.ts +27 -0
- package/dist/type/preload.d.ts +54 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.js +1 -0
- package/dist/utils/context.cjs +45 -0
- package/dist/utils/context.cjs.map +1 -0
- package/dist/utils/context.d.ts +1 -0
- package/dist/utils/context.js +44 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/env.cjs +10 -0
- package/dist/utils/env.cjs.map +1 -0
- package/dist/utils/env.d.ts +1 -0
- package/dist/utils/env.js +10 -0
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/hooks/asyncHook.cjs +26 -0
- package/dist/utils/hooks/asyncHook.cjs.map +1 -0
- package/dist/utils/hooks/asyncHook.d.ts +10 -0
- package/dist/utils/hooks/asyncHook.js +26 -0
- package/dist/utils/hooks/asyncHook.js.map +1 -0
- package/dist/utils/hooks/asyncWaterfallHooks.cjs +44 -0
- package/dist/utils/hooks/asyncWaterfallHooks.cjs.map +1 -0
- package/dist/utils/hooks/asyncWaterfallHooks.d.ts +12 -0
- package/dist/utils/hooks/asyncWaterfallHooks.js +44 -0
- package/dist/utils/hooks/asyncWaterfallHooks.js.map +1 -0
- package/dist/utils/hooks/index.cjs +5 -0
- package/dist/utils/hooks/index.d.ts +5 -0
- package/dist/utils/hooks/index.js +7 -0
- package/dist/utils/hooks/pluginSystem.cjs +37 -0
- package/dist/utils/hooks/pluginSystem.cjs.map +1 -0
- package/dist/utils/hooks/pluginSystem.d.ts +19 -0
- package/dist/utils/hooks/pluginSystem.js +37 -0
- package/dist/utils/hooks/pluginSystem.js.map +1 -0
- package/dist/utils/hooks/syncHook.cjs +37 -0
- package/dist/utils/hooks/syncHook.cjs.map +1 -0
- package/dist/utils/hooks/syncHook.d.ts +16 -0
- package/dist/utils/hooks/syncHook.js +36 -0
- package/dist/utils/hooks/syncHook.js.map +1 -0
- package/dist/utils/hooks/syncWaterfallHook.cjs +40 -0
- package/dist/utils/hooks/syncWaterfallHook.cjs.map +1 -0
- package/dist/utils/hooks/syncWaterfallHook.d.ts +11 -0
- package/dist/utils/hooks/syncWaterfallHook.js +39 -0
- package/dist/utils/hooks/syncWaterfallHook.js.map +1 -0
- package/dist/utils/index.cjs +8 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +9 -0
- package/dist/utils/load.cjs +214 -0
- package/dist/utils/load.cjs.map +1 -0
- package/dist/utils/load.d.ts +16 -0
- package/dist/utils/load.js +212 -0
- package/dist/utils/load.js.map +1 -0
- package/dist/utils/logger.cjs +34 -0
- package/dist/utils/logger.cjs.map +1 -0
- package/dist/utils/logger.d.ts +10 -0
- package/dist/utils/logger.js +31 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/manifest.cjs +56 -0
- package/dist/utils/manifest.cjs.map +1 -0
- package/dist/utils/manifest.d.ts +10 -0
- package/dist/utils/manifest.js +53 -0
- package/dist/utils/manifest.js.map +1 -0
- package/dist/utils/plugin.cjs +27 -0
- package/dist/utils/plugin.cjs.map +1 -0
- package/dist/utils/plugin.d.ts +1 -0
- package/dist/utils/plugin.js +27 -0
- package/dist/utils/plugin.js.map +1 -0
- package/dist/utils/preload.cjs +212 -0
- package/dist/utils/preload.cjs.map +1 -0
- package/dist/utils/preload.d.ts +8 -0
- package/dist/utils/preload.js +209 -0
- package/dist/utils/preload.js.map +1 -0
- package/dist/utils/semver/compare.cjs +48 -0
- package/dist/utils/semver/compare.cjs.map +1 -0
- package/dist/utils/semver/compare.js +47 -0
- package/dist/utils/semver/compare.js.map +1 -0
- package/dist/utils/semver/constants.cjs +41 -0
- package/dist/utils/semver/constants.cjs.map +1 -0
- package/dist/utils/semver/constants.js +31 -0
- package/dist/utils/semver/constants.js.map +1 -0
- package/dist/utils/semver/index.cjs +68 -0
- package/dist/utils/semver/index.cjs.map +1 -0
- package/dist/utils/semver/index.d.ts +5 -0
- package/dist/utils/semver/index.js +68 -0
- package/dist/utils/semver/index.js.map +1 -0
- package/dist/utils/semver/parser.cjs +104 -0
- package/dist/utils/semver/parser.cjs.map +1 -0
- package/dist/utils/semver/parser.js +96 -0
- package/dist/utils/semver/parser.js.map +1 -0
- package/dist/utils/semver/utils.cjs +28 -0
- package/dist/utils/semver/utils.cjs.map +1 -0
- package/dist/utils/semver/utils.js +24 -0
- package/dist/utils/semver/utils.js.map +1 -0
- package/dist/utils/share.cjs +284 -0
- package/dist/utils/share.cjs.map +1 -0
- package/dist/utils/share.d.ts +23 -0
- package/dist/utils/share.js +278 -0
- package/dist/utils/share.js.map +1 -0
- package/dist/utils/tool.cjs +82 -0
- package/dist/utils/tool.cjs.map +1 -0
- package/dist/utils/tool.d.ts +8 -0
- package/dist/utils/tool.js +71 -0
- package/dist/utils/tool.js.map +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load.js","names":[],"sources":["../../src/utils/load.ts"],"sourcesContent":["import {\n loadScript,\n loadScriptNode,\n composeKeyWithSeparator,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { ModuleFederation } from '../core';\nimport { globalLoading, getRemoteEntryExports } from '../global';\nimport {\n Remote,\n RemoteEntryExports,\n RemoteInfo,\n ResourceLoadContext,\n} from '../type';\nimport { assert, error } from './logger';\nimport {\n RUNTIME_001,\n RUNTIME_008,\n runtimeDescMap,\n} from '@module-federation/error-codes';\n\n// Declare the ENV_TARGET constant that will be defined by DefinePlugin\ndeclare const ENV_TARGET: 'web' | 'node';\nconst importCallback = '.then(callbacks[0]).catch(callbacks[1])';\n\nasync function loadEsmEntry({\n entry,\n remoteEntryExports,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n try {\n if (!remoteEntryExports) {\n if (typeof FEDERATION_ALLOW_NEW_FUNCTION !== 'undefined') {\n new Function('callbacks', `import(\"${entry}\")${importCallback}`)([\n resolve,\n reject,\n ]);\n } else {\n import(/* webpackIgnore: true */ /* @vite-ignore */ entry)\n .then(resolve)\n .catch(reject);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load ESM entry from \"${entry}\". ${msg}`);\n }\n });\n}\n\nasync function loadSystemJsEntry({\n entry,\n remoteEntryExports,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n try {\n if (!remoteEntryExports) {\n //@ts-ignore\n if (typeof __system_context__ === 'undefined') {\n //@ts-ignore\n System.import(entry).then(resolve).catch(reject);\n } else {\n new Function(\n 'callbacks',\n `System.import(\"${entry}\")${importCallback}`,\n )([resolve, reject]);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load SystemJS entry from \"${entry}\". ${msg}`);\n }\n });\n}\n\nfunction handleRemoteEntryLoaded(\n name: string,\n globalName: string,\n entry: string,\n): RemoteEntryExports {\n const { remoteEntryKey, entryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (!entryExports) {\n error(RUNTIME_001, runtimeDescMap, {\n remoteName: name,\n remoteEntryUrl: entry,\n remoteEntryKey,\n });\n }\n\n return entryExports;\n}\n\nasync function loadEntryScript({\n name,\n globalName,\n entry,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n name: string;\n globalName: string;\n entry: string;\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports> {\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n // if getEntryUrl is passed, use the getEntryUrl to get the entry url\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n return loadScript(url, {\n attrs: {},\n createScriptHook: (url, attrs) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if (res instanceof HTMLScriptElement) {\n return res;\n }\n\n if ('script' in res || 'timeout' in res) {\n return res;\n }\n\n return;\n },\n }).then(\n () => {\n // loadScript resolved: script was fetched, executed without throwing, and\n // did not trigger a ScriptExecutionError listener. Now verify the global was registered.\n return handleRemoteEntryLoaded(name, globalName, entry);\n },\n (loadError: unknown) => {\n // loadScript rejected — one of three causes, all with descriptive messages:\n // ScriptNetworkError — URL unreachable, 404, CORS, etc.\n // ScriptExecutionError — script fetched OK but IIFE threw during execution\n // timeout — script took too long to load\n // Errors thrown inside handleRemoteEntryLoaded above are NOT caught here.\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n },\n );\n}\nasync function loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n switch (type) {\n case 'esm':\n case 'module':\n return loadEsmEntry({ entry, remoteEntryExports });\n case 'system':\n return loadSystemJsEntry({ entry, remoteEntryExports });\n default:\n return loadEntryScript({\n entry,\n globalName,\n name,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n });\n }\n}\n\nasync function loadEntryNode({\n remoteInfo,\n loaderHook,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n return loadScriptNode(entry, {\n attrs: { name, globalName, type },\n loaderHook: {\n createScriptHook: (url: string, attrs: Record<string, any> = {}) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if ('url' in res) {\n return res;\n }\n\n return;\n },\n },\n })\n .then(() => {\n return handleRemoteEntryLoaded(name, globalName, entry);\n })\n .catch((e) => {\n const msg = e instanceof Error ? e.message : String(e);\n error(\n `Failed to load Node.js entry for remote \"${name}\" from \"${entry}\". ${msg}`,\n );\n });\n}\n\nexport function getRemoteEntryUniqueKey(remoteInfo: RemoteInfo): string {\n const { entry, name } = remoteInfo;\n return composeKeyWithSeparator(name, entry);\n}\n\nexport async function getRemoteEntry(params: {\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports | undefined;\n getEntryUrl?: (url: string) => string;\n _inErrorHandling?: boolean; // Add flag to prevent recursion\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports | false | void> {\n const {\n origin,\n remoteEntryExports,\n remoteInfo,\n getEntryUrl,\n resourceContext,\n _inErrorHandling = false,\n } = params;\n const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n if (!globalLoading[uniqueKey]) {\n const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;\n const loaderHook = origin.loaderHook;\n\n globalLoading[uniqueKey] = loadEntryHook\n .emit({\n origin,\n loaderHook,\n remoteInfo,\n remoteEntryExports,\n })\n .then((res) => {\n if (res) {\n return res;\n }\n // Use ENV_TARGET if defined, otherwise fallback to isBrowserEnvValue\n const isWebEnvironment =\n typeof ENV_TARGET !== 'undefined'\n ? ENV_TARGET === 'web'\n : isBrowserEnvValue;\n\n return isWebEnvironment\n ? loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n })\n : loadEntryNode({ remoteInfo, loaderHook, resourceContext });\n })\n .then(async (res) => {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: res,\n });\n return res;\n })\n .catch(async (err) => {\n const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);\n // ScriptExecutionError means the script downloaded fine but its IIFE\n // threw at runtime — retrying would reproduce the same error, so exclude it.\n const isScriptExecutionError =\n err instanceof Error && err.message.includes('ScriptExecutionError');\n const isScriptLoadError =\n err instanceof Error &&\n err.message.includes(RUNTIME_008) &&\n !isScriptExecutionError;\n\n if (isScriptLoadError && !_inErrorHandling) {\n const wrappedGetRemoteEntry = (\n params: Parameters<typeof getRemoteEntry>[0],\n ) => {\n return getRemoteEntry({ ...params, _inErrorHandling: true });\n };\n\n const RemoteEntryExports =\n await origin.loaderHook.lifecycle.loadEntryError.emit({\n getRemoteEntry: wrappedGetRemoteEntry,\n origin,\n remoteInfo: remoteInfo,\n remoteEntryExports,\n globalLoading,\n uniqueKey,\n });\n\n if (RemoteEntryExports) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: RemoteEntryExports,\n recovered: true,\n });\n return RemoteEntryExports;\n }\n }\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n error: err,\n });\n throw err;\n });\n }\n\n return globalLoading[uniqueKey];\n}\n\nexport function getRemoteInfo(remote: Remote): RemoteInfo {\n return {\n ...remote,\n entry: 'entry' in remote ? remote.entry : '',\n type: remote.type || DEFAULT_REMOTE_TYPE,\n entryGlobalName: remote.entryGlobalName || remote.name,\n shareScope: remote.shareScope || DEFAULT_SCOPE,\n };\n}\n"],"mappings":";;;;;;;AAwBA,MAAM,iBAAiB;AAEvB,eAAe,aAAa,EAC1B,OACA,sBAI8B;AAC9B,QAAO,IAAI,SAA6B,SAAS,WAAW;AAC1D,MAAI;AACF,OAAI,CAAC,mBACH,KAAI,OAAO,kCAAkC,YAC3C,KAAI,SAAS,aAAa,WAAW,MAAM,IAAI,iBAAiB,CAAC,CAC/D,SACA,OACD,CAAC;OAEF;;;IAAoD;EACjD,KAAK,QAAQ,CACb,MAAM,OAAO;OAGlB,SAAQ,mBAAmB;WAEtB,GAAG;AAEV,SAAM,kCAAkC,MAAM,KADlC,aAAa,QAAQ,EAAE,UAAU,OAAO,EAAE,GACG;;GAE3D;;AAGJ,eAAe,kBAAkB,EAC/B,OACA,sBAI8B;AAC9B,QAAO,IAAI,SAA6B,SAAS,WAAW;AAC1D,MAAI;AACF,OAAI,CAAC,mBAEH,KAAI,OAAO,uBAAuB,YAEhC,QAAO,OAAO,MAAM,CAAC,KAAK,QAAQ,CAAC,MAAM,OAAO;OAEhD,KAAI,SACF,aACA,kBAAkB,MAAM,IAAI,iBAC7B,CAAC,CAAC,SAAS,OAAO,CAAC;OAGtB,SAAQ,mBAAmB;WAEtB,GAAG;AAEV,SAAM,uCAAuC,MA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ACnB,SAAM,OAAO,WAAW,UAAU,eAAe,KAAK;IACpD;IACA;IACA,oBAAoB;IACrB,CAAC;AACF,UAAO;IACP,CACD,MAAM,OAAO,QAAQ;GACpB,MAAM,YAAY,wBAAwB,WAAW;GAGrD,MAAM,yBACJ,eAAe,SAAS,IAAI,QAAQ,SAAS,uBAAuB;AAMtE,OAJE,eAAe,SACf,IAAI,QAAQ,SAAS,YAAY,IACjC,CAAC,0BAEsB,CAAC,kBAAkB;IAC1C,MAAM,yBACJ,WACG;AACH,YAAO,eAAe;MAAE,GAAG;MAAQ,kBAAkB;MAAM,CAAC;;IAG9D,MAAM,qBACJ,MAAM,OAAO,WAAW,UAAU,eAAe,KAAK;KACpD,gBAAgB;KAChB;KACY;KACZ;KACA;KACA;KACD,CAAC;AAEJ,QAAI,oBAAoB;AACtB,WAAM,OAAO,WAAW,UAAU,eAAe,KAAK;MACpD;MACA;MACA,oBAAoB;MACpB,WAAW;MACZ,CAAC;AACF,YAAO;;;AAGX,SAAM,OAAO,WAAW,UAAU,eAAe,KAAK;IACpD;IACA;IACA,OAAO;IACR,CAAC;AACF,SAAM;IACN;;AAGN,QAAO,cAAc;;AAGvB,SAAgB,cAAc,QAA4B;AACxD,QAAO;EACL,GAAG;EACH,OAAO,WAAW,SAAS,OAAO,QAAQ;EAC1C,MAAM,OAAO,QAAQ;EACrB,iBAAiB,OAAO,mBAAmB,OAAO;EAClD,YAAY,OAAO,cAAc;EAClC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
let _module_federation_sdk = require("@module-federation/sdk");
|
|
2
|
+
let _module_federation_error_codes_browser = require("@module-federation/error-codes/browser");
|
|
3
|
+
|
|
4
|
+
//#region src/utils/logger.ts
|
|
5
|
+
const LOG_CATEGORY = "[ Federation Runtime ]";
|
|
6
|
+
const logger = (0, _module_federation_sdk.createLogger)(LOG_CATEGORY);
|
|
7
|
+
function assert(condition, msgOrCode, descMap, args, context) {
|
|
8
|
+
if (!condition) if (descMap !== void 0) error(msgOrCode, descMap, args, void 0, context);
|
|
9
|
+
else error(msgOrCode);
|
|
10
|
+
}
|
|
11
|
+
function error(msgOrCode, descMap, args, originalErrorMsg, context) {
|
|
12
|
+
if (descMap !== void 0) return (0, _module_federation_error_codes_browser.logAndReport)(msgOrCode, descMap, args ?? {}, (msg) => {
|
|
13
|
+
throw new Error(`${LOG_CATEGORY}: ${msg}`);
|
|
14
|
+
}, originalErrorMsg, context);
|
|
15
|
+
const msg = msgOrCode;
|
|
16
|
+
if (msg instanceof Error) {
|
|
17
|
+
if (!msg.message.startsWith(LOG_CATEGORY)) msg.message = `${LOG_CATEGORY}: ${msg.message}`;
|
|
18
|
+
throw msg;
|
|
19
|
+
}
|
|
20
|
+
throw new Error(`${LOG_CATEGORY}: ${msg}`);
|
|
21
|
+
}
|
|
22
|
+
function warn(msg) {
|
|
23
|
+
if (msg instanceof Error) {
|
|
24
|
+
if (!msg.message.startsWith(LOG_CATEGORY)) msg.message = `${LOG_CATEGORY}: ${msg.message}`;
|
|
25
|
+
logger.warn(msg);
|
|
26
|
+
} else logger.warn(msg);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
exports.assert = assert;
|
|
31
|
+
exports.error = error;
|
|
32
|
+
exports.logger = logger;
|
|
33
|
+
exports.warn = warn;
|
|
34
|
+
//# sourceMappingURL=logger.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.cjs","names":[],"sources":["../../src/utils/logger.ts"],"sourcesContent":["import { createLogger } from '@module-federation/sdk';\nimport type { MFContext } from '@module-federation/error-codes';\nimport { logAndReport } from '@module-federation/error-codes/browser';\n\nconst LOG_CATEGORY = '[ Federation Runtime ]';\n// FIXME: pre-bundle ?\nconst logger = createLogger(LOG_CATEGORY);\n\n// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\nexport function assert(condition: any, msg: string): asserts condition;\nexport function assert(\n condition: any,\n code: string,\n descMap: Record<string, string>,\n args?: Record<string, unknown>,\n context?: Partial<MFContext>,\n): asserts condition;\nexport function assert(\n condition: any,\n msgOrCode: string,\n descMap?: Record<string, string>,\n args?: Record<string, unknown>,\n context?: Partial<MFContext>,\n): asserts condition {\n if (!condition) {\n if (descMap !== undefined) {\n error(msgOrCode, descMap, args, undefined, context);\n } else {\n error(msgOrCode);\n }\n }\n}\n\nexport function error(msg: string | Error | unknown): never;\nexport function error(\n code: string,\n descMap: Record<string, string>,\n args?: Record<string, unknown>,\n originalErrorMsg?: string,\n context?: Partial<MFContext>,\n): never;\nexport function error(\n msgOrCode: string | Error | unknown,\n descMap?: Record<string, string>,\n args?: Record<string, unknown>,\n originalErrorMsg?: string,\n context?: Partial<MFContext>,\n): never {\n if (descMap !== undefined) {\n return logAndReport(\n msgOrCode as string,\n descMap,\n args ?? {},\n (msg) => {\n throw new Error(`${LOG_CATEGORY}: ${msg}`);\n },\n originalErrorMsg,\n context,\n );\n }\n const msg = msgOrCode;\n if (msg instanceof Error) {\n if (!msg.message.startsWith(LOG_CATEGORY)) {\n msg.message = `${LOG_CATEGORY}: ${msg.message}`;\n }\n throw msg;\n }\n throw new Error(`${LOG_CATEGORY}: ${msg}`);\n}\n\nexport function warn(msg: Parameters<typeof console.warn>[0]): void {\n if (msg instanceof Error) {\n // Check if the message already starts with the log category to avoid duplication\n if (!msg.message.startsWith(LOG_CATEGORY)) {\n msg.message = `${LOG_CATEGORY}: ${msg.message}`;\n }\n logger.warn(msg);\n } else {\n logger.warn(msg);\n }\n}\n\nexport { logger };\n"],"mappings":";;;;AAIA,MAAM,eAAe;AAErB,MAAM,kDAAsB,aAAa;AAWzC,SAAgB,OACd,WACA,WACA,SACA,MACA,SACmB;AACnB,KAAI,CAAC,UACH,KAAI,YAAY,OACd,OAAM,WAAW,SAAS,MAAM,QAAW,QAAQ;KAEnD,OAAM,UAAU;;AAatB,SAAgB,MACd,WACA,SACA,MACA,kBACA,SACO;AACP,KAAI,YAAY,OACd,iEACE,WACA,SACA,QAAQ,EAAE,GACT,QAAQ;AACP,QAAM,IAAI,MAAM,GAAG,aAAa,IAAI,MAAM;IAE5C,kBACA,QACD;CAEH,MAAM,MAAM;AACZ,KAAI,eAAe,OAAO;AACxB,MAAI,CAAC,IAAI,QAAQ,WAAW,aAAa,CACvC,KAAI,UAAU,GAAG,aAAa,IAAI,IAAI;AAExC,QAAM;;AAER,OAAM,IAAI,MAAM,GAAG,aAAa,IAAI,MAAM;;AAG5C,SAAgB,KAAK,KAA+C;AAClE,KAAI,eAAe,OAAO;AAExB,MAAI,CAAC,IAAI,QAAQ,WAAW,aAAa,CACvC,KAAI,UAAU,GAAG,aAAa,IAAI,IAAI;AAExC,SAAO,KAAK,IAAI;OAEhB,QAAO,KAAK,IAAI"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MFContext } from "@module-federation/error-codes";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/logger.d.ts
|
|
4
|
+
declare function assert(condition: any, msg: string): asserts condition;
|
|
5
|
+
declare function assert(condition: any, code: string, descMap: Record<string, string>, args?: Record<string, unknown>, context?: Partial<MFContext>): asserts condition;
|
|
6
|
+
declare function error(msg: string | Error | unknown): never;
|
|
7
|
+
declare function error(code: string, descMap: Record<string, string>, args?: Record<string, unknown>, originalErrorMsg?: string, context?: Partial<MFContext>): never;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { assert, error };
|
|
10
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createLogger } from "@module-federation/sdk";
|
|
2
|
+
import { logAndReport } from "@module-federation/error-codes/browser";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/logger.ts
|
|
5
|
+
const LOG_CATEGORY = "[ Federation Runtime ]";
|
|
6
|
+
const logger = createLogger(LOG_CATEGORY);
|
|
7
|
+
function assert(condition, msgOrCode, descMap, args, context) {
|
|
8
|
+
if (!condition) if (descMap !== void 0) error(msgOrCode, descMap, args, void 0, context);
|
|
9
|
+
else error(msgOrCode);
|
|
10
|
+
}
|
|
11
|
+
function error(msgOrCode, descMap, args, originalErrorMsg, context) {
|
|
12
|
+
if (descMap !== void 0) return logAndReport(msgOrCode, descMap, args ?? {}, (msg) => {
|
|
13
|
+
throw new Error(`${LOG_CATEGORY}: ${msg}`);
|
|
14
|
+
}, originalErrorMsg, context);
|
|
15
|
+
const msg = msgOrCode;
|
|
16
|
+
if (msg instanceof Error) {
|
|
17
|
+
if (!msg.message.startsWith(LOG_CATEGORY)) msg.message = `${LOG_CATEGORY}: ${msg.message}`;
|
|
18
|
+
throw msg;
|
|
19
|
+
}
|
|
20
|
+
throw new Error(`${LOG_CATEGORY}: ${msg}`);
|
|
21
|
+
}
|
|
22
|
+
function warn$1(msg) {
|
|
23
|
+
if (msg instanceof Error) {
|
|
24
|
+
if (!msg.message.startsWith(LOG_CATEGORY)) msg.message = `${LOG_CATEGORY}: ${msg.message}`;
|
|
25
|
+
logger.warn(msg);
|
|
26
|
+
} else logger.warn(msg);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { assert, error, logger, warn$1 as warn };
|
|
31
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","names":["warn"],"sources":["../../src/utils/logger.ts"],"sourcesContent":["import { createLogger } from '@module-federation/sdk';\nimport type { MFContext } from '@module-federation/error-codes';\nimport { logAndReport } from '@module-federation/error-codes/browser';\n\nconst LOG_CATEGORY = '[ Federation Runtime ]';\n// FIXME: pre-bundle ?\nconst logger = createLogger(LOG_CATEGORY);\n\n// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\nexport function assert(condition: any, msg: string): asserts condition;\nexport function assert(\n condition: any,\n code: string,\n descMap: Record<string, string>,\n args?: Record<string, unknown>,\n context?: Partial<MFContext>,\n): asserts condition;\nexport function assert(\n condition: any,\n msgOrCode: string,\n descMap?: Record<string, string>,\n args?: Record<string, unknown>,\n context?: Partial<MFContext>,\n): asserts condition {\n if (!condition) {\n if (descMap !== undefined) {\n error(msgOrCode, descMap, args, undefined, context);\n } else {\n error(msgOrCode);\n }\n }\n}\n\nexport function error(msg: string | Error | unknown): never;\nexport function error(\n code: string,\n descMap: Record<string, string>,\n args?: Record<string, unknown>,\n originalErrorMsg?: string,\n context?: Partial<MFContext>,\n): never;\nexport function error(\n msgOrCode: string | Error | unknown,\n descMap?: Record<string, string>,\n args?: Record<string, unknown>,\n originalErrorMsg?: string,\n context?: Partial<MFContext>,\n): never {\n if (descMap !== undefined) {\n return logAndReport(\n msgOrCode as string,\n descMap,\n args ?? {},\n (msg) => {\n throw new Error(`${LOG_CATEGORY}: ${msg}`);\n },\n originalErrorMsg,\n context,\n );\n }\n const msg = msgOrCode;\n if (msg instanceof Error) {\n if (!msg.message.startsWith(LOG_CATEGORY)) {\n msg.message = `${LOG_CATEGORY}: ${msg.message}`;\n }\n throw msg;\n }\n throw new Error(`${LOG_CATEGORY}: ${msg}`);\n}\n\nexport function warn(msg: Parameters<typeof console.warn>[0]): void {\n if (msg instanceof Error) {\n // Check if the message already starts with the log category to avoid duplication\n if (!msg.message.startsWith(LOG_CATEGORY)) {\n msg.message = `${LOG_CATEGORY}: ${msg.message}`;\n }\n logger.warn(msg);\n } else {\n logger.warn(msg);\n }\n}\n\nexport { logger };\n"],"mappings":";;;;AAIA,MAAM,eAAe;AAErB,MAAM,SAAS,aAAa,aAAa;AAWzC,SAAgB,OACd,WACA,WACA,SACA,MACA,SACmB;AACnB,KAAI,CAAC,UACH,KAAI,YAAY,OACd,OAAM,WAAW,SAAS,MAAM,QAAW,QAAQ;KAEnD,OAAM,UAAU;;AAatB,SAAgB,MACd,WACA,SACA,MACA,kBACA,SACO;AACP,KAAI,YAAY,OACd,QAAO,aACL,WACA,SACA,QAAQ,EAAE,GACT,QAAQ;AACP,QAAM,IAAI,MAAM,GAAG,aAAa,IAAI,MAAM;IAE5C,kBACA,QACD;CAEH,MAAM,MAAM;AACZ,KAAI,eAAe,OAAO;AACxB,MAAI,CAAC,IAAI,QAAQ,WAAW,aAAa,CACvC,KAAI,UAAU,GAAG,aAAa,IAAI,IAAI;AAExC,QAAM;;AAER,OAAM,IAAI,MAAM,GAAG,aAAa,IAAI,MAAM;;AAG5C,SAAgBA,OAAK,KAA+C;AAClE,KAAI,eAAe,OAAO;AAExB,MAAI,CAAC,IAAI,QAAQ,WAAW,aAAa,CACvC,KAAI,UAAU,GAAG,aAAa,IAAI,IAAI;AAExC,SAAO,KAAK,IAAI;OAEhB,QAAO,KAAK,IAAI"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/utils/manifest.ts
|
|
3
|
+
function composeRemoteRequestId(remoteName, expose) {
|
|
4
|
+
if (!expose || expose === ".") return remoteName;
|
|
5
|
+
return `${remoteName}/${expose.replace(/^\.\//, "")}`;
|
|
6
|
+
}
|
|
7
|
+
function matchRemoteWithNameAndExpose(remotes, id) {
|
|
8
|
+
for (const remote of remotes) {
|
|
9
|
+
const isNameMatched = id.startsWith(remote.name);
|
|
10
|
+
let expose = id.replace(remote.name, "");
|
|
11
|
+
if (isNameMatched) {
|
|
12
|
+
if (expose.startsWith("/")) {
|
|
13
|
+
const pkgNameOrAlias = remote.name;
|
|
14
|
+
expose = `.${expose}`;
|
|
15
|
+
return {
|
|
16
|
+
pkgNameOrAlias,
|
|
17
|
+
expose,
|
|
18
|
+
remote
|
|
19
|
+
};
|
|
20
|
+
} else if (expose === "") return {
|
|
21
|
+
pkgNameOrAlias: remote.name,
|
|
22
|
+
expose: ".",
|
|
23
|
+
remote
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const isAliasMatched = remote.alias && id.startsWith(remote.alias);
|
|
27
|
+
let exposeWithAlias = remote.alias && id.replace(remote.alias, "");
|
|
28
|
+
if (remote.alias && isAliasMatched) {
|
|
29
|
+
if (exposeWithAlias && exposeWithAlias.startsWith("/")) {
|
|
30
|
+
const pkgNameOrAlias = remote.alias;
|
|
31
|
+
exposeWithAlias = `.${exposeWithAlias}`;
|
|
32
|
+
return {
|
|
33
|
+
pkgNameOrAlias,
|
|
34
|
+
expose: exposeWithAlias,
|
|
35
|
+
remote
|
|
36
|
+
};
|
|
37
|
+
} else if (exposeWithAlias === "") return {
|
|
38
|
+
pkgNameOrAlias: remote.alias,
|
|
39
|
+
expose: ".",
|
|
40
|
+
remote
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function matchRemote(remotes, nameOrAlias) {
|
|
46
|
+
for (const remote of remotes) {
|
|
47
|
+
if (nameOrAlias === remote.name) return remote;
|
|
48
|
+
if (remote.alias && nameOrAlias === remote.alias) return remote;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
exports.composeRemoteRequestId = composeRemoteRequestId;
|
|
54
|
+
exports.matchRemote = matchRemote;
|
|
55
|
+
exports.matchRemoteWithNameAndExpose = matchRemoteWithNameAndExpose;
|
|
56
|
+
//# sourceMappingURL=manifest.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manifest.cjs","names":[],"sources":["../../src/utils/manifest.ts"],"sourcesContent":["import { Remote } from '../type';\n\nexport function composeRemoteRequestId(\n remoteName: string,\n expose?: string,\n): string {\n if (!expose || expose === '.') {\n return remoteName;\n }\n\n return `${remoteName}/${expose.replace(/^\\.\\//, '')}`;\n}\n\n// Function to match a remote with its name and expose\n// id: pkgName(@federation/app1) + expose(button) = @federation/app1/button\n// id: alias(app1) + expose(button) = app1/button\n// id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort\nexport function matchRemoteWithNameAndExpose(\n remotes: Array<Remote>,\n id: string,\n):\n | {\n pkgNameOrAlias: string;\n expose: string;\n remote: Remote;\n }\n | undefined {\n for (const remote of remotes) {\n // match pkgName\n const isNameMatched = id.startsWith(remote.name);\n let expose = id.replace(remote.name, '');\n if (isNameMatched) {\n if (expose.startsWith('/')) {\n const pkgNameOrAlias = remote.name;\n expose = `.${expose}`;\n return {\n pkgNameOrAlias,\n expose,\n remote,\n };\n } else if (expose === '') {\n return {\n pkgNameOrAlias: remote.name,\n expose: '.',\n remote,\n };\n }\n }\n\n // match alias\n const isAliasMatched = remote.alias && id.startsWith(remote.alias);\n let exposeWithAlias = remote.alias && id.replace(remote.alias, '');\n if (remote.alias && isAliasMatched) {\n if (exposeWithAlias && exposeWithAlias.startsWith('/')) {\n const pkgNameOrAlias = remote.alias;\n exposeWithAlias = `.${exposeWithAlias}`;\n return {\n pkgNameOrAlias,\n expose: exposeWithAlias,\n remote,\n };\n } else if (exposeWithAlias === '') {\n return {\n pkgNameOrAlias: remote.alias,\n expose: '.',\n remote,\n };\n }\n }\n }\n\n return;\n}\n\n// Function to match a remote with its name or alias\nexport function matchRemote(\n remotes: Array<Remote>,\n nameOrAlias: string,\n): Remote | undefined {\n for (const remote of remotes) {\n const isNameMatched = nameOrAlias === remote.name;\n if (isNameMatched) {\n return remote;\n }\n\n const isAliasMatched = remote.alias && nameOrAlias === remote.alias;\n if (isAliasMatched) {\n return remote;\n }\n }\n return;\n}\n"],"mappings":";;AAEA,SAAgB,uBACd,YACA,QACQ;AACR,KAAI,CAAC,UAAU,WAAW,IACxB,QAAO;AAGT,QAAO,GAAG,WAAW,GAAG,OAAO,QAAQ,SAAS,GAAG;;AAOrD,SAAgB,6BACd,SACA,IAOY;AACZ,MAAK,MAAM,UAAU,SAAS;EAE5B,MAAM,gBAAgB,GAAG,WAAW,OAAO,KAAK;EAChD,IAAI,SAAS,GAAG,QAAQ,OAAO,MAAM,GAAG;AACxC,MAAI,eACF;OAAI,OAAO,WAAW,IAAI,EAAE;IAC1B,MAAM,iBAAiB,OAAO;AAC9B,aAAS,IAAI;AACb,WAAO;KACL;KACA;KACA;KACD;cACQ,WAAW,GACpB,QAAO;IACL,gBAAgB,OAAO;IACvB,QAAQ;IACR;IACD;;EAKL,MAAM,iBAAiB,OAAO,SAAS,GAAG,WAAW,OAAO,MAAM;EAClE,IAAI,kBAAkB,OAAO,SAAS,GAAG,QAAQ,OAAO,OAAO,GAAG;AAClE,MAAI,OAAO,SAAS,gBAClB;OAAI,mBAAmB,gBAAgB,WAAW,IAAI,EAAE;IACtD,MAAM,iBAAiB,OAAO;AAC9B,sBAAkB,IAAI;AACtB,WAAO;KACL;KACA,QAAQ;KACR;KACD;cACQ,oBAAoB,GAC7B,QAAO;IACL,gBAAgB,OAAO;IACvB,QAAQ;IACR;IACD;;;;AAST,SAAgB,YACd,SACA,aACoB;AACpB,MAAK,MAAM,UAAU,SAAS;AAE5B,MADsB,gBAAgB,OAAO,KAE3C,QAAO;AAIT,MADuB,OAAO,SAAS,gBAAgB,OAAO,MAE5D,QAAO"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Remote } from "../type/config.js";
|
|
2
|
+
//#region src/utils/manifest.d.ts
|
|
3
|
+
declare function matchRemoteWithNameAndExpose(remotes: Array<Remote>, id: string): {
|
|
4
|
+
pkgNameOrAlias: string;
|
|
5
|
+
expose: string;
|
|
6
|
+
remote: Remote;
|
|
7
|
+
} | undefined;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { matchRemoteWithNameAndExpose };
|
|
10
|
+
//# sourceMappingURL=manifest.d.ts.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//#region src/utils/manifest.ts
|
|
2
|
+
function composeRemoteRequestId(remoteName, expose) {
|
|
3
|
+
if (!expose || expose === ".") return remoteName;
|
|
4
|
+
return `${remoteName}/${expose.replace(/^\.\//, "")}`;
|
|
5
|
+
}
|
|
6
|
+
function matchRemoteWithNameAndExpose(remotes, id) {
|
|
7
|
+
for (const remote of remotes) {
|
|
8
|
+
const isNameMatched = id.startsWith(remote.name);
|
|
9
|
+
let expose = id.replace(remote.name, "");
|
|
10
|
+
if (isNameMatched) {
|
|
11
|
+
if (expose.startsWith("/")) {
|
|
12
|
+
const pkgNameOrAlias = remote.name;
|
|
13
|
+
expose = `.${expose}`;
|
|
14
|
+
return {
|
|
15
|
+
pkgNameOrAlias,
|
|
16
|
+
expose,
|
|
17
|
+
remote
|
|
18
|
+
};
|
|
19
|
+
} else if (expose === "") return {
|
|
20
|
+
pkgNameOrAlias: remote.name,
|
|
21
|
+
expose: ".",
|
|
22
|
+
remote
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const isAliasMatched = remote.alias && id.startsWith(remote.alias);
|
|
26
|
+
let exposeWithAlias = remote.alias && id.replace(remote.alias, "");
|
|
27
|
+
if (remote.alias && isAliasMatched) {
|
|
28
|
+
if (exposeWithAlias && exposeWithAlias.startsWith("/")) {
|
|
29
|
+
const pkgNameOrAlias = remote.alias;
|
|
30
|
+
exposeWithAlias = `.${exposeWithAlias}`;
|
|
31
|
+
return {
|
|
32
|
+
pkgNameOrAlias,
|
|
33
|
+
expose: exposeWithAlias,
|
|
34
|
+
remote
|
|
35
|
+
};
|
|
36
|
+
} else if (exposeWithAlias === "") return {
|
|
37
|
+
pkgNameOrAlias: remote.alias,
|
|
38
|
+
expose: ".",
|
|
39
|
+
remote
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function matchRemote(remotes, nameOrAlias) {
|
|
45
|
+
for (const remote of remotes) {
|
|
46
|
+
if (nameOrAlias === remote.name) return remote;
|
|
47
|
+
if (remote.alias && nameOrAlias === remote.alias) return remote;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { composeRemoteRequestId, matchRemote, matchRemoteWithNameAndExpose };
|
|
53
|
+
//# sourceMappingURL=manifest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manifest.js","names":[],"sources":["../../src/utils/manifest.ts"],"sourcesContent":["import { Remote } from '../type';\n\nexport function composeRemoteRequestId(\n remoteName: string,\n expose?: string,\n): string {\n if (!expose || expose === '.') {\n return remoteName;\n }\n\n return `${remoteName}/${expose.replace(/^\\.\\//, '')}`;\n}\n\n// Function to match a remote with its name and expose\n// id: pkgName(@federation/app1) + expose(button) = @federation/app1/button\n// id: alias(app1) + expose(button) = app1/button\n// id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort\nexport function matchRemoteWithNameAndExpose(\n remotes: Array<Remote>,\n id: string,\n):\n | {\n pkgNameOrAlias: string;\n expose: string;\n remote: Remote;\n }\n | undefined {\n for (const remote of remotes) {\n // match pkgName\n const isNameMatched = id.startsWith(remote.name);\n let expose = id.replace(remote.name, '');\n if (isNameMatched) {\n if (expose.startsWith('/')) {\n const pkgNameOrAlias = remote.name;\n expose = `.${expose}`;\n return {\n pkgNameOrAlias,\n expose,\n remote,\n };\n } else if (expose === '') {\n return {\n pkgNameOrAlias: remote.name,\n expose: '.',\n remote,\n };\n }\n }\n\n // match alias\n const isAliasMatched = remote.alias && id.startsWith(remote.alias);\n let exposeWithAlias = remote.alias && id.replace(remote.alias, '');\n if (remote.alias && isAliasMatched) {\n if (exposeWithAlias && exposeWithAlias.startsWith('/')) {\n const pkgNameOrAlias = remote.alias;\n exposeWithAlias = `.${exposeWithAlias}`;\n return {\n pkgNameOrAlias,\n expose: exposeWithAlias,\n remote,\n };\n } else if (exposeWithAlias === '') {\n return {\n pkgNameOrAlias: remote.alias,\n expose: '.',\n remote,\n };\n }\n }\n }\n\n return;\n}\n\n// Function to match a remote with its name or alias\nexport function matchRemote(\n remotes: Array<Remote>,\n nameOrAlias: string,\n): Remote | undefined {\n for (const remote of remotes) {\n const isNameMatched = nameOrAlias === remote.name;\n if (isNameMatched) {\n return remote;\n }\n\n const isAliasMatched = remote.alias && nameOrAlias === remote.alias;\n if (isAliasMatched) {\n return remote;\n }\n }\n return;\n}\n"],"mappings":";AAEA,SAAgB,uBACd,YACA,QACQ;AACR,KAAI,CAAC,UAAU,WAAW,IACxB,QAAO;AAGT,QAAO,GAAG,WAAW,GAAG,OAAO,QAAQ,SAAS,GAAG;;AAOrD,SAAgB,6BACd,SACA,IAOY;AACZ,MAAK,MAAM,UAAU,SAAS;EAE5B,MAAM,gBAAgB,GAAG,WAAW,OAAO,KAAK;EAChD,IAAI,SAAS,GAAG,QAAQ,OAAO,MAAM,GAAG;AACxC,MAAI,eACF;OAAI,OAAO,WAAW,IAAI,EAAE;IAC1B,MAAM,iBAAiB,OAAO;AAC9B,aAAS,IAAI;AACb,WAAO;KACL;KACA;KACA;KACD;cACQ,WAAW,GACpB,QAAO;IACL,gBAAgB,OAAO;IACvB,QAAQ;IACR;IACD;;EAKL,MAAM,iBAAiB,OAAO,SAAS,GAAG,WAAW,OAAO,MAAM;EAClE,IAAI,kBAAkB,OAAO,SAAS,GAAG,QAAQ,OAAO,OAAO,GAAG;AAClE,MAAI,OAAO,SAAS,gBAClB;OAAI,mBAAmB,gBAAgB,WAAW,IAAI,EAAE;IACtD,MAAM,iBAAiB,OAAO;AAC9B,sBAAkB,IAAI;AACtB,WAAO;KACL;KACA,QAAQ;KACR;KACD;cACQ,oBAAoB,GAC7B,QAAO;IACL,gBAAgB,OAAO;IACvB,QAAQ;IACR;IACD;;;;AAST,SAAgB,YACd,SACA,aACoB;AACpB,MAAK,MAAM,UAAU,SAAS;AAE5B,MADsB,gBAAgB,OAAO,KAE3C,QAAO;AAIT,MADuB,OAAO,SAAS,gBAAgB,OAAO,MAE5D,QAAO"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const require_global = require('../global.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/utils/plugin.ts
|
|
4
|
+
function registerPlugins(plugins, instance) {
|
|
5
|
+
const globalPlugins = require_global.getGlobalHostPlugins();
|
|
6
|
+
const hookInstances = [
|
|
7
|
+
instance.hooks,
|
|
8
|
+
instance.remoteHandler.hooks,
|
|
9
|
+
instance.sharedHandler.hooks,
|
|
10
|
+
instance.snapshotHandler.hooks,
|
|
11
|
+
instance.loaderHook,
|
|
12
|
+
instance.bridgeHook
|
|
13
|
+
];
|
|
14
|
+
if (globalPlugins.length > 0) globalPlugins.forEach((plugin) => {
|
|
15
|
+
if (plugins?.find((item) => item.name !== plugin.name)) plugins.push(plugin);
|
|
16
|
+
});
|
|
17
|
+
if (plugins && plugins.length > 0) plugins.forEach((plugin) => {
|
|
18
|
+
hookInstances.forEach((hookInstance) => {
|
|
19
|
+
hookInstance.applyPlugin(plugin, instance);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
return plugins;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.registerPlugins = registerPlugins;
|
|
27
|
+
//# sourceMappingURL=plugin.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.cjs","names":["getGlobalHostPlugins"],"sources":["../../src/utils/plugin.ts"],"sourcesContent":["import { ModuleFederation } from '../core';\nimport { UserOptions } from '../type';\nimport { getGlobalHostPlugins } from '../global';\n\nexport function registerPlugins(\n plugins: UserOptions['plugins'],\n instance: ModuleFederation,\n) {\n const globalPlugins = getGlobalHostPlugins();\n const hookInstances = [\n instance.hooks,\n instance.remoteHandler.hooks,\n instance.sharedHandler.hooks,\n instance.snapshotHandler.hooks,\n instance.loaderHook,\n instance.bridgeHook,\n ];\n // Incorporate global plugins\n if (globalPlugins.length > 0) {\n globalPlugins.forEach((plugin) => {\n if (plugins?.find((item) => item.name !== plugin.name)) {\n plugins.push(plugin);\n }\n });\n }\n\n if (plugins && plugins.length > 0) {\n plugins.forEach((plugin) => {\n hookInstances.forEach((hookInstance) => {\n hookInstance.applyPlugin(plugin, instance);\n });\n });\n }\n return plugins;\n}\n"],"mappings":";;;AAIA,SAAgB,gBACd,SACA,UACA;CACA,MAAM,gBAAgBA,qCAAsB;CAC5C,MAAM,gBAAgB;EACpB,SAAS;EACT,SAAS,cAAc;EACvB,SAAS,cAAc;EACvB,SAAS,gBAAgB;EACzB,SAAS;EACT,SAAS;EACV;AAED,KAAI,cAAc,SAAS,EACzB,eAAc,SAAS,WAAW;AAChC,MAAI,SAAS,MAAM,SAAS,KAAK,SAAS,OAAO,KAAK,CACpD,SAAQ,KAAK,OAAO;GAEtB;AAGJ,KAAI,WAAW,QAAQ,SAAS,EAC9B,SAAQ,SAAS,WAAW;AAC1B,gBAAc,SAAS,iBAAiB;AACtC,gBAAa,YAAY,QAAQ,SAAS;IAC1C;GACF;AAEJ,QAAO"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getGlobalHostPlugins } from "../global.js";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/plugin.ts
|
|
4
|
+
function registerPlugins(plugins, instance) {
|
|
5
|
+
const globalPlugins = getGlobalHostPlugins();
|
|
6
|
+
const hookInstances = [
|
|
7
|
+
instance.hooks,
|
|
8
|
+
instance.remoteHandler.hooks,
|
|
9
|
+
instance.sharedHandler.hooks,
|
|
10
|
+
instance.snapshotHandler.hooks,
|
|
11
|
+
instance.loaderHook,
|
|
12
|
+
instance.bridgeHook
|
|
13
|
+
];
|
|
14
|
+
if (globalPlugins.length > 0) globalPlugins.forEach((plugin) => {
|
|
15
|
+
if (plugins?.find((item) => item.name !== plugin.name)) plugins.push(plugin);
|
|
16
|
+
});
|
|
17
|
+
if (plugins && plugins.length > 0) plugins.forEach((plugin) => {
|
|
18
|
+
hookInstances.forEach((hookInstance) => {
|
|
19
|
+
hookInstance.applyPlugin(plugin, instance);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
return plugins;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { registerPlugins };
|
|
27
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","names":[],"sources":["../../src/utils/plugin.ts"],"sourcesContent":["import { ModuleFederation } from '../core';\nimport { UserOptions } from '../type';\nimport { getGlobalHostPlugins } from '../global';\n\nexport function registerPlugins(\n plugins: UserOptions['plugins'],\n instance: ModuleFederation,\n) {\n const globalPlugins = getGlobalHostPlugins();\n const hookInstances = [\n instance.hooks,\n instance.remoteHandler.hooks,\n instance.sharedHandler.hooks,\n instance.snapshotHandler.hooks,\n instance.loaderHook,\n instance.bridgeHook,\n ];\n // Incorporate global plugins\n if (globalPlugins.length > 0) {\n globalPlugins.forEach((plugin) => {\n if (plugins?.find((item) => item.name !== plugin.name)) {\n plugins.push(plugin);\n }\n });\n }\n\n if (plugins && plugins.length > 0) {\n plugins.forEach((plugin) => {\n hookInstances.forEach((hookInstance) => {\n hookInstance.applyPlugin(plugin, instance);\n });\n });\n }\n return plugins;\n}\n"],"mappings":";;;AAIA,SAAgB,gBACd,SACA,UACA;CACA,MAAM,gBAAgB,sBAAsB;CAC5C,MAAM,gBAAgB;EACpB,SAAS;EACT,SAAS,cAAc;EACvB,SAAS,cAAc;EACvB,SAAS,gBAAgB;EACzB,SAAS;EACT,SAAS;EACV;AAED,KAAI,cAAc,SAAS,EACzB,eAAc,SAAS,WAAW;AAChC,MAAI,SAAS,MAAM,SAAS,KAAK,SAAS,OAAO,KAAK,CACpD,SAAQ,KAAK,OAAO;GAEtB;AAGJ,KAAI,WAAW,QAAQ,SAAS,EAC9B,SAAQ,SAAS,WAAW;AAC1B,gBAAc,SAAS,iBAAiB;AACtC,gBAAa,YAAY,QAAQ,SAAS;IAC1C;GACF;AAEJ,QAAO"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
const require_logger = require('./logger.cjs');
|
|
2
|
+
const require_manifest = require('./manifest.cjs');
|
|
3
|
+
const require_load = require('./load.cjs');
|
|
4
|
+
let _module_federation_sdk = require("@module-federation/sdk");
|
|
5
|
+
|
|
6
|
+
//#region src/utils/preload.ts
|
|
7
|
+
function defaultPreloadArgs(preloadConfig) {
|
|
8
|
+
return {
|
|
9
|
+
resourceCategory: "sync",
|
|
10
|
+
share: true,
|
|
11
|
+
depsRemote: true,
|
|
12
|
+
...preloadConfig
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function formatPreloadArgs(remotes, preloadArgs) {
|
|
16
|
+
return preloadArgs.map((args) => {
|
|
17
|
+
const remoteInfo = require_manifest.matchRemote(remotes, args.nameOrAlias);
|
|
18
|
+
require_logger.assert(remoteInfo, `Unable to preload ${args.nameOrAlias} as it is not included in ${!remoteInfo && (0, _module_federation_sdk.safeToString)({
|
|
19
|
+
remoteInfo,
|
|
20
|
+
remotes
|
|
21
|
+
})}`);
|
|
22
|
+
return {
|
|
23
|
+
remote: remoteInfo,
|
|
24
|
+
preloadConfig: defaultPreloadArgs(args)
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function normalizePreloadExposes(exposes) {
|
|
29
|
+
if (!exposes) return [];
|
|
30
|
+
return exposes.map((expose) => {
|
|
31
|
+
if (expose === ".") return expose;
|
|
32
|
+
if (expose.startsWith("./")) return expose.replace("./", "");
|
|
33
|
+
return expose;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function isTimeoutError(error) {
|
|
37
|
+
if (!(error instanceof Error)) return false;
|
|
38
|
+
return error.message.includes("timed out") || error.name.includes("Timeout");
|
|
39
|
+
}
|
|
40
|
+
function createAssetResult(context, url, status, error) {
|
|
41
|
+
return {
|
|
42
|
+
url,
|
|
43
|
+
status,
|
|
44
|
+
resourceType: context.resourceType,
|
|
45
|
+
initiator: context.initiator,
|
|
46
|
+
id: context.id,
|
|
47
|
+
error
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async function waitForRemoteEntryPreload(host, remoteInfo, entryRemoteInfo, context) {
|
|
51
|
+
const cachedRemote = host.moduleCache.get(entryRemoteInfo.name);
|
|
52
|
+
const url = entryRemoteInfo.entry;
|
|
53
|
+
if (cachedRemote?.remoteEntryExports) return createAssetResult(context, url, "cached");
|
|
54
|
+
try {
|
|
55
|
+
if (!await require_load.getRemoteEntry({
|
|
56
|
+
origin: host,
|
|
57
|
+
remoteInfo: entryRemoteInfo,
|
|
58
|
+
remoteEntryExports: cachedRemote?.remoteEntryExports,
|
|
59
|
+
resourceContext: {
|
|
60
|
+
...context,
|
|
61
|
+
url
|
|
62
|
+
}
|
|
63
|
+
})) throw new Error(`Failed to load remoteEntry "${url}".`);
|
|
64
|
+
return createAssetResult(context, url, "success");
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return createAssetResult(context, url, isTimeoutError(error) ? "timeout" : "error", error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function waitForLinkPreload({ host, remoteInfo, url, attrs, context, needDeleteLink }) {
|
|
70
|
+
return new Promise((resolve) => {
|
|
71
|
+
const { link, needAttach } = (0, _module_federation_sdk.createLink)({
|
|
72
|
+
url,
|
|
73
|
+
cb: () => {
|
|
74
|
+
resolve(createAssetResult(context, url, needAttach ? "success" : "cached"));
|
|
75
|
+
},
|
|
76
|
+
onErrorCallback: (error) => {
|
|
77
|
+
resolve(createAssetResult(context, url, isTimeoutError(error) ? "timeout" : "error", error));
|
|
78
|
+
},
|
|
79
|
+
attrs,
|
|
80
|
+
createLinkHook: (hookUrl, hookAttrs) => {
|
|
81
|
+
const res = host.loaderHook.lifecycle.createLink.emit({
|
|
82
|
+
url: hookUrl,
|
|
83
|
+
attrs: hookAttrs,
|
|
84
|
+
remoteInfo,
|
|
85
|
+
resourceContext: {
|
|
86
|
+
...context,
|
|
87
|
+
url: hookUrl
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
if (res instanceof HTMLLinkElement) return res;
|
|
91
|
+
return res;
|
|
92
|
+
},
|
|
93
|
+
needDeleteLink
|
|
94
|
+
});
|
|
95
|
+
needAttach && document.head.appendChild(link);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function waitForScriptPreload({ host, remoteInfo, url, attrs, context }) {
|
|
99
|
+
return new Promise((resolve) => {
|
|
100
|
+
const { script, needAttach } = (0, _module_federation_sdk.createScript)({
|
|
101
|
+
url,
|
|
102
|
+
cb: () => {
|
|
103
|
+
resolve(createAssetResult(context, url, needAttach ? "success" : "cached"));
|
|
104
|
+
},
|
|
105
|
+
onErrorCallback: (error) => {
|
|
106
|
+
resolve(createAssetResult(context, url, isTimeoutError(error) ? "timeout" : "error", error));
|
|
107
|
+
},
|
|
108
|
+
attrs,
|
|
109
|
+
createScriptHook: (hookUrl, hookAttrs) => {
|
|
110
|
+
const res = host.loaderHook.lifecycle.createScript.emit({
|
|
111
|
+
url: hookUrl,
|
|
112
|
+
attrs: hookAttrs,
|
|
113
|
+
remoteInfo,
|
|
114
|
+
resourceContext: {
|
|
115
|
+
...context,
|
|
116
|
+
url: hookUrl
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
if (res instanceof HTMLScriptElement) return res;
|
|
120
|
+
return res;
|
|
121
|
+
},
|
|
122
|
+
needDeleteScript: true
|
|
123
|
+
});
|
|
124
|
+
needAttach && document.head.appendChild(script);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function createResourceContext(baseContext, resourceType) {
|
|
128
|
+
return {
|
|
129
|
+
...baseContext,
|
|
130
|
+
resourceType
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function preloadAssets(remoteInfo, host, assets, useLinkPreload = true, baseContext = {
|
|
134
|
+
initiator: "preloadRemote",
|
|
135
|
+
id: remoteInfo.name
|
|
136
|
+
}) {
|
|
137
|
+
const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
|
|
138
|
+
const results = [];
|
|
139
|
+
if (host.options.inBrowser) {
|
|
140
|
+
entryAssets.forEach((asset) => {
|
|
141
|
+
const { moduleInfo: entryRemoteInfo } = asset;
|
|
142
|
+
results.push(waitForRemoteEntryPreload(host, remoteInfo, entryRemoteInfo, createResourceContext(baseContext, "remoteEntry")));
|
|
143
|
+
});
|
|
144
|
+
if (useLinkPreload) {
|
|
145
|
+
const defaultAttrs = {
|
|
146
|
+
rel: "preload",
|
|
147
|
+
as: "style"
|
|
148
|
+
};
|
|
149
|
+
cssAssets.forEach((cssUrl) => {
|
|
150
|
+
results.push(waitForLinkPreload({
|
|
151
|
+
host,
|
|
152
|
+
remoteInfo,
|
|
153
|
+
url: cssUrl,
|
|
154
|
+
attrs: defaultAttrs,
|
|
155
|
+
context: createResourceContext(baseContext, "css")
|
|
156
|
+
}));
|
|
157
|
+
});
|
|
158
|
+
} else {
|
|
159
|
+
const defaultAttrs = {
|
|
160
|
+
rel: "stylesheet",
|
|
161
|
+
type: "text/css"
|
|
162
|
+
};
|
|
163
|
+
cssAssets.forEach((cssUrl) => {
|
|
164
|
+
results.push(waitForLinkPreload({
|
|
165
|
+
host,
|
|
166
|
+
remoteInfo,
|
|
167
|
+
url: cssUrl,
|
|
168
|
+
attrs: defaultAttrs,
|
|
169
|
+
needDeleteLink: false,
|
|
170
|
+
context: createResourceContext(baseContext, "css")
|
|
171
|
+
}));
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (useLinkPreload) {
|
|
175
|
+
const defaultAttrs = {
|
|
176
|
+
rel: "preload",
|
|
177
|
+
as: "script"
|
|
178
|
+
};
|
|
179
|
+
jsAssetsWithoutEntry.forEach((jsUrl) => {
|
|
180
|
+
results.push(waitForLinkPreload({
|
|
181
|
+
host,
|
|
182
|
+
remoteInfo,
|
|
183
|
+
url: jsUrl,
|
|
184
|
+
attrs: defaultAttrs,
|
|
185
|
+
context: createResourceContext(baseContext, "js")
|
|
186
|
+
}));
|
|
187
|
+
});
|
|
188
|
+
} else {
|
|
189
|
+
const defaultAttrs = {
|
|
190
|
+
fetchpriority: "high",
|
|
191
|
+
type: remoteInfo?.type === "module" ? "module" : "text/javascript"
|
|
192
|
+
};
|
|
193
|
+
jsAssetsWithoutEntry.forEach((jsUrl) => {
|
|
194
|
+
results.push(waitForScriptPreload({
|
|
195
|
+
host,
|
|
196
|
+
remoteInfo,
|
|
197
|
+
url: jsUrl,
|
|
198
|
+
attrs: defaultAttrs,
|
|
199
|
+
context: createResourceContext(baseContext, "js")
|
|
200
|
+
}));
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return Promise.all(results);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
exports.defaultPreloadArgs = defaultPreloadArgs;
|
|
209
|
+
exports.formatPreloadArgs = formatPreloadArgs;
|
|
210
|
+
exports.normalizePreloadExposes = normalizePreloadExposes;
|
|
211
|
+
exports.preloadAssets = preloadAssets;
|
|
212
|
+
//# sourceMappingURL=preload.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preload.cjs","names":["matchRemote","getRemoteEntry"],"sources":["../../src/utils/preload.ts"],"sourcesContent":["import { createLink, createScript, safeToString } from '@module-federation/sdk';\nimport {\n PreloadAssets,\n PreloadAssetResult,\n PreloadConfig,\n PreloadOptions,\n PreloadRemoteArgs,\n Remote,\n RemoteInfo,\n ResourceLoadContext,\n ResourceLoadType,\n depsPreloadArg,\n} from '../type';\nimport { matchRemote } from './manifest';\nimport { assert } from './logger';\nimport { ModuleFederation } from '../core';\nimport { getRemoteEntry } from './load';\n\nexport function defaultPreloadArgs(\n preloadConfig: PreloadRemoteArgs | depsPreloadArg,\n): PreloadConfig {\n return {\n resourceCategory: 'sync',\n share: true,\n depsRemote: true,\n ...preloadConfig,\n } as PreloadConfig;\n}\n\nexport function formatPreloadArgs(\n remotes: Array<Remote>,\n preloadArgs: Array<PreloadRemoteArgs>,\n): PreloadOptions {\n return preloadArgs.map((args) => {\n const remoteInfo = matchRemote(remotes, args.nameOrAlias);\n assert(\n remoteInfo,\n `Unable to preload ${args.nameOrAlias} as it is not included in ${\n !remoteInfo &&\n safeToString({\n remoteInfo,\n remotes,\n })\n }`,\n );\n return {\n remote: remoteInfo,\n preloadConfig: defaultPreloadArgs(args),\n };\n });\n}\n\nexport function normalizePreloadExposes(exposes?: string[]): string[] {\n if (!exposes) {\n return [];\n }\n\n return exposes.map((expose) => {\n if (expose === '.') {\n return expose;\n }\n if (expose.startsWith('./')) {\n return expose.replace('./', '');\n }\n return expose;\n });\n}\n\nfunction isTimeoutError(error: unknown): boolean {\n if (!(error instanceof Error)) {\n return false;\n }\n return error.message.includes('timed out') || error.name.includes('Timeout');\n}\n\nfunction createAssetResult(\n context: ResourceLoadContext,\n url: string,\n status: PreloadAssetResult['status'],\n error?: unknown,\n): PreloadAssetResult {\n return {\n url,\n status,\n resourceType: context.resourceType,\n initiator: context.initiator,\n id: context.id,\n error,\n };\n}\n\nasync function waitForRemoteEntryPreload(\n host: ModuleFederation,\n remoteInfo: RemoteInfo,\n entryRemoteInfo: RemoteInfo,\n context: ResourceLoadContext,\n): Promise<PreloadAssetResult> {\n const cachedRemote = host.moduleCache.get(entryRemoteInfo.name);\n const url = entryRemoteInfo.entry;\n if (cachedRemote?.remoteEntryExports) {\n return createAssetResult(context, url, 'cached');\n }\n\n try {\n const remoteEntryExports = await getRemoteEntry({\n origin: host,\n remoteInfo: entryRemoteInfo,\n remoteEntryExports: cachedRemote?.remoteEntryExports,\n resourceContext: {\n ...context,\n url,\n },\n });\n if (!remoteEntryExports) {\n throw new Error(`Failed to load remoteEntry \"${url}\".`);\n }\n return createAssetResult(context, url, 'success');\n } catch (error) {\n return createAssetResult(\n context,\n url,\n isTimeoutError(error) ? 'timeout' : 'error',\n error,\n );\n }\n}\n\nfunction waitForLinkPreload({\n host,\n remoteInfo,\n url,\n attrs,\n context,\n needDeleteLink,\n}: {\n host: ModuleFederation;\n remoteInfo: RemoteInfo;\n url: string;\n attrs: Record<string, string>;\n context: ResourceLoadContext;\n needDeleteLink?: boolean;\n}): Promise<PreloadAssetResult> {\n return new Promise((resolve) => {\n const { link, needAttach } = createLink({\n url,\n cb: () => {\n resolve(\n createAssetResult(context, url, needAttach ? 'success' : 'cached'),\n );\n },\n onErrorCallback: (error) => {\n resolve(\n createAssetResult(\n context,\n url,\n isTimeoutError(error) ? 'timeout' : 'error',\n error,\n ),\n );\n },\n attrs,\n createLinkHook: (hookUrl, hookAttrs) => {\n const res = host.loaderHook.lifecycle.createLink.emit({\n url: hookUrl,\n attrs: hookAttrs,\n remoteInfo,\n resourceContext: {\n ...context,\n url: hookUrl,\n },\n });\n if (res instanceof HTMLLinkElement) {\n return res;\n }\n return res;\n },\n needDeleteLink,\n });\n\n needAttach && document.head.appendChild(link);\n });\n}\n\nfunction waitForScriptPreload({\n host,\n remoteInfo,\n url,\n attrs,\n context,\n}: {\n host: ModuleFederation;\n remoteInfo: RemoteInfo;\n url: string;\n attrs: Record<string, string>;\n context: ResourceLoadContext;\n}): Promise<PreloadAssetResult> {\n return new Promise((resolve) => {\n const { script, needAttach } = createScript({\n url,\n cb: () => {\n resolve(\n createAssetResult(context, url, needAttach ? 'success' : 'cached'),\n );\n },\n onErrorCallback: (error) => {\n resolve(\n createAssetResult(\n context,\n url,\n isTimeoutError(error) ? 'timeout' : 'error',\n error,\n ),\n );\n },\n attrs,\n createScriptHook: (hookUrl: string, hookAttrs: any) => {\n const res = host.loaderHook.lifecycle.createScript.emit({\n url: hookUrl,\n attrs: hookAttrs,\n remoteInfo,\n resourceContext: {\n ...context,\n url: hookUrl,\n },\n });\n if (res instanceof HTMLScriptElement) {\n return res;\n }\n return res;\n },\n needDeleteScript: true,\n });\n\n needAttach && document.head.appendChild(script);\n });\n}\n\nfunction createResourceContext(\n baseContext: Omit<ResourceLoadContext, 'resourceType'>,\n resourceType: ResourceLoadType,\n): ResourceLoadContext {\n return {\n ...baseContext,\n resourceType,\n };\n}\n\nexport function preloadAssets(\n remoteInfo: RemoteInfo,\n host: ModuleFederation,\n assets: PreloadAssets,\n // It is used to distinguish preload from load remote parallel loading\n useLinkPreload = true,\n baseContext: Omit<ResourceLoadContext, 'resourceType'> = {\n initiator: 'preloadRemote',\n id: remoteInfo.name,\n },\n): Promise<PreloadAssetResult[]> {\n const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;\n const results: Array<Promise<PreloadAssetResult>> = [];\n\n if (host.options.inBrowser) {\n entryAssets.forEach((asset) => {\n const { moduleInfo: entryRemoteInfo } = asset;\n results.push(\n waitForRemoteEntryPreload(\n host,\n remoteInfo,\n entryRemoteInfo,\n createResourceContext(baseContext, 'remoteEntry'),\n ),\n );\n });\n\n if (useLinkPreload) {\n const defaultAttrs = {\n rel: 'preload',\n as: 'style',\n };\n cssAssets.forEach((cssUrl) => {\n results.push(\n waitForLinkPreload({\n host,\n remoteInfo,\n url: cssUrl,\n attrs: defaultAttrs,\n context: createResourceContext(baseContext, 'css'),\n }),\n );\n });\n } else {\n const defaultAttrs = {\n rel: 'stylesheet',\n type: 'text/css',\n };\n cssAssets.forEach((cssUrl) => {\n results.push(\n waitForLinkPreload({\n host,\n remoteInfo,\n url: cssUrl,\n attrs: defaultAttrs,\n needDeleteLink: false,\n context: createResourceContext(baseContext, 'css'),\n }),\n );\n });\n }\n\n if (useLinkPreload) {\n const defaultAttrs = {\n rel: 'preload',\n as: 'script',\n };\n jsAssetsWithoutEntry.forEach((jsUrl) => {\n results.push(\n waitForLinkPreload({\n host,\n remoteInfo,\n url: jsUrl,\n attrs: defaultAttrs,\n context: createResourceContext(baseContext, 'js'),\n }),\n );\n });\n } else {\n const defaultAttrs = {\n fetchpriority: 'high',\n type: remoteInfo?.type === 'module' ? 'module' : 'text/javascript',\n };\n jsAssetsWithoutEntry.forEach((jsUrl) => {\n results.push(\n waitForScriptPreload({\n host,\n remoteInfo,\n url: jsUrl,\n attrs: defaultAttrs,\n context: createResourceContext(baseContext, 'js'),\n }),\n );\n });\n }\n }\n\n return Promise.all(results);\n}\n"],"mappings":";;;;;;AAkBA,SAAgB,mBACd,eACe;AACf,QAAO;EACL,kBAAkB;EAClB,OAAO;EACP,YAAY;EACZ,GAAG;EACJ;;AAGH,SAAgB,kBACd,SACA,aACgB;AAChB,QAAO,YAAY,KAAK,SAAS;EAC/B,MAAM,aAAaA,6BAAY,SAAS,KAAK,YAAY;AACzD,wBACE,YACA,qBAAqB,KAAK,YAAY,4BACpC,CAAC,uDACY;GAC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ACxC,WAAQ,KACN,0BACE,MACA,YACA,iBACA,sBAAsB,aAAa,cAAc,CAClD,CACF;IACD;AAEF,MAAI,gBAAgB;GAClB,MAAM,eAAe;IACnB,KAAK;IACL,IAAI;IACL;AACD,aAAU,SAAS,WAAW;AAC5B,YAAQ,KACN,mBAAmB;KACjB;KACA;KACA,KAAK;KACL,OAAO;KACP,SAAS,sBAAsB,aAAa,MAAM;KACnD,CAAC,CACH;KACD;SACG;GACL,MAAM,eAAe;IACnB,KAAK;IACL,MAAM;IACP;AACD,aAAU,SAAS,WAAW;AAC5B,YAAQ,KACN,mBAAmB;KACjB;KACA;KACA,KAAK;KACL,OAAO;KACP,gBAAgB;KAChB,SAAS,sBAAsB,aAAa,MAAM;KACnD,CAAC,CACH;KACD;;AAGJ,MAAI,gBAAgB;GAClB,MAAM,eAAe;IACnB,KAAK;IACL,IAAI;IACL;AACD,wBAAqB,SAAS,UAAU;AACtC,YAAQ,KACN,mBAAmB;KACjB;KACA;KACA,KAAK;KACL,OAAO;KACP,SAAS,sBAAsB,aAAa,KAAK;KAClD,CAAC,CACH;KACD;SACG;GACL,MAAM,eAAe;IACnB,eAAe;IACf,MAAM,YAAY,SAAS,WAAW,WAAW;IAClD;AACD,wBAAqB,SAAS,UAAU;AACtC,YAAQ,KACN,qBAAqB;KACnB;KACA;KACA,KAAK;KACL,OAAO;KACP,SAAS,sBAAsB,aAAa,KAAK;KAClD,CAAC,CACH;KACD;;;AAIN,QAAO,QAAQ,IAAI,QAAQ"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ModuleFederation } from "../core.js";
|
|
2
|
+
import { RemoteInfo } from "../type/config.js";
|
|
3
|
+
import { PreloadAssetResult, PreloadAssets, ResourceLoadContext } from "../type/preload.js";
|
|
4
|
+
//#region src/utils/preload.d.ts
|
|
5
|
+
declare function preloadAssets(remoteInfo: RemoteInfo, host: ModuleFederation, assets: PreloadAssets, useLinkPreload?: boolean, baseContext?: Omit<ResourceLoadContext, 'resourceType'>): Promise<PreloadAssetResult[]>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { preloadAssets };
|
|
8
|
+
//# sourceMappingURL=preload.d.ts.map
|