every-plugin
Version:
1 lines • 10.1 kB
Source Map (JSON)
{"version":3,"file":"module-federation.service.cjs","names":["MF_CORE_SHARED_DEPS","Effect","getNormalizedRemoteName","ModuleFederationError"],"sources":["../../../src/runtime/services/module-federation.service.ts"],"sourcesContent":["import { createInstance, getInstance } from \"@module-federation/enhanced/runtime\";\nimport { setGlobalFederationInstance } from \"@module-federation/runtime-core\";\nimport { Effect } from \"effect\";\nimport type { AnyPlugin } from \"../../types\";\nimport { ModuleFederationError } from \"../errors\";\nimport { type CoreSharedDepName, MF_CORE_SHARED_DEPS } from \"../mf-config\";\nimport { getNormalizedRemoteName } from \"./normalize\";\n\ntype RemoteModule = (new () => AnyPlugin) | { default: new () => AnyPlugin };\n\nconst coreModuleLoaders: Record<CoreSharedDepName, () => Promise<unknown>> = {\n \"every-plugin\": () => import(\"every-plugin\"),\n effect: () => import(\"effect\"),\n zod: () => import(\"zod\"),\n \"@orpc/contract\": () => import(\"@orpc/contract\"),\n \"@orpc/client\": () => import(\"@orpc/client\"),\n \"@orpc/server\": () => import(\"@orpc/server\"),\n};\n\nfunction buildSharedConfig(): Record<\n string,\n {\n version: string;\n shareScope: string;\n get: () => Promise<() => unknown>;\n shareConfig: (typeof MF_CORE_SHARED_DEPS)[CoreSharedDepName][\"shareConfig\"];\n }\n> {\n return Object.fromEntries(\n (\n Object.entries(MF_CORE_SHARED_DEPS) as [\n CoreSharedDepName,\n (typeof MF_CORE_SHARED_DEPS)[CoreSharedDepName],\n ][]\n ).map(([name, config]) => {\n const load = coreModuleLoaders[name];\n\n if (!load) {\n throw new Error(`Missing core shared module loader for ${name}`);\n }\n\n return [\n name,\n {\n version: config.version,\n shareScope: config.shareScope,\n get: () => load().then((mod) => () => mod),\n shareConfig: config.shareConfig,\n },\n ];\n }),\n );\n}\n\nconst createModuleFederationInstance = Effect.cached(\n Effect.sync(() => {\n try {\n const shared = buildSharedConfig();\n let instance = getInstance();\n\n if (!instance) {\n instance = createInstance({\n name: \"host\",\n remotes: [],\n shared,\n });\n\n setGlobalFederationInstance(instance);\n } else {\n instance.registerShared(shared);\n }\n\n return instance;\n } catch (error) {\n throw new Error(`Failed to initialize Module Federation: ${error}`);\n }\n }),\n);\n\nexport class ModuleFederationService extends Effect.Service<ModuleFederationService>()(\n \"ModuleFederationService\",\n {\n effect: Effect.gen(function* () {\n const mf = yield* Effect.flatten(createModuleFederationInstance);\n\n return {\n registerRemote: (pluginId: string, url: string) =>\n Effect.gen(function* () {\n yield* Effect.logDebug(`[MF] Registering ${pluginId}`);\n\n const remoteName = getNormalizedRemoteName(pluginId);\n const type = url.endsWith(\"/mf-manifest.json\")\n ? (\"manifest\" as const)\n : url.endsWith(\"/remoteEntry.js\")\n ? (\"script\" as const)\n : undefined;\n\n yield* Effect.try({\n try: () =>\n mf.registerRemotes([\n {\n name: remoteName,\n entry: url,\n ...(type ? { type } : {}),\n },\n ]),\n catch: (error): ModuleFederationError =>\n new ModuleFederationError({\n pluginId,\n remoteUrl: url,\n cause: error instanceof Error ? error : new Error(String(error)),\n }),\n });\n\n yield* Effect.logInfo(`[MF] ✅ Registered ${pluginId}`);\n }),\n\n loadRemoteConstructor: (pluginId: string, url: string) =>\n Effect.gen(function* () {\n const remoteName = getNormalizedRemoteName(pluginId);\n yield* Effect.logDebug(`[MF] Loading remote ${remoteName}`);\n const modulePath = `${remoteName}/plugin`;\n\n const pluginConstructor = yield* Effect.tryPromise({\n try: async () => {\n const container = await mf.loadRemote<RemoteModule>(modulePath);\n if (!container) {\n throw new Error(`No container returned for ${modulePath}`);\n }\n\n // Support multiple export patterns: direct function, default export, named exports\n let Constructor: any;\n\n if (typeof container === \"function\") {\n // Direct function export\n Constructor = container;\n } else if (container.default) {\n // Default export\n Constructor = container.default;\n } else {\n // Named export fallback - prioritize exports with 'binding' property (plugin classes)\n Constructor = Object.values(container).find(\n (exp) => typeof exp === \"function\" && (exp as any).binding !== undefined,\n );\n\n // Fallback to any function export if no binding found\n if (!Constructor) {\n Constructor = Object.values(container).find(\n (exp) => typeof exp === \"function\" && exp.prototype?.constructor === exp,\n );\n }\n }\n\n if (!Constructor || typeof Constructor !== \"function\") {\n const containerInfo =\n typeof container === \"object\"\n ? `Available exports: ${Object.keys(container).join(\", \")}`\n : `Container type: ${typeof container}`;\n\n throw new Error(\n `No valid plugin constructor found for '${pluginId}'.\\n` +\n `Supported patterns:\\n` +\n ` - export const YourPlugin = createPlugin({...})\\n` +\n ` - export default createPlugin({...})\\n` +\n `${containerInfo}`,\n );\n }\n\n // Validate it looks like a plugin constructor (has binding property)\n if (!(Constructor as any).binding) {\n const containerInfo =\n typeof container === \"object\"\n ? `Found exports: ${Object.keys(container).join(\", \")}`\n : `Container type: ${typeof container}`;\n\n throw new Error(\n `Invalid plugin constructor for '${pluginId}'. ` +\n `The exported value must be created with createPlugin(). ` +\n `Found a function but it's missing the required 'binding' property.\\n` +\n `${containerInfo}`,\n );\n }\n\n return Constructor;\n },\n catch: (error): ModuleFederationError =>\n new ModuleFederationError({\n pluginId,\n remoteUrl: url,\n cause: error instanceof Error ? error : new Error(String(error)),\n }),\n });\n\n yield* Effect.logInfo(`[MF] ✅ Loaded constructor for ${pluginId}`);\n return pluginConstructor;\n }),\n };\n }),\n },\n) {}\n"],"mappings":";;;;;;;;AAUA,MAAM,oBAAuE;CAC3E,2DAAsB;CACtB,cAAc,OAAO;CACrB,WAAW,OAAO;CAClB,wBAAwB,OAAO;CAC/B,sBAAsB,OAAO;CAC7B,sBAAsB,OAAO;AAC/B;AAEA,SAAS,oBAQP;CACA,OAAO,OAAO,YAEV,OAAO,QAAQA,6CAAmB,CAAC,CAInC,KAAK,CAAC,MAAM,YAAY;EACxB,MAAM,OAAO,kBAAkB;EAE/B,IAAI,CAAC,MACH,MAAM,IAAI,MAAM,yCAAyC,MAAM;EAGjE,OAAO,CACL,MACA;GACE,SAAS,OAAO;GAChB,YAAY,OAAO;GACnB,WAAW,KAAK,CAAC,CAAC,MAAM,cAAc,GAAG;GACzC,aAAa,OAAO;EACtB,CACF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