{"version":3,"file":"consumes.cjs","names":["getUsedExports","getShortErrorMsg","RUNTIME_012"],"sources":["../src/consumes.ts"],"sourcesContent":["import { ConsumesOptions } from './types';\nimport { attachShareScopeMap } from './attachShareScopeMap';\nimport { updateConsumeOptions } from './updateOptions';\nimport { getUsedExports } from './getUsedExports';\nimport type { Shared } from '@module-federation/runtime/types';\nimport { getShortErrorMsg, RUNTIME_012 } from '@module-federation/error-codes';\n\nexport function consumes(options: ConsumesOptions) {\n updateConsumeOptions(options);\n const {\n chunkId,\n promises,\n installedModules,\n webpackRequire,\n chunkMapping,\n moduleToHandlerMapping,\n } = options;\n\n attachShareScopeMap(webpackRequire);\n if (webpackRequire.o(chunkMapping, chunkId)) {\n chunkMapping[chunkId].forEach((id) => {\n if (webpackRequire.o(installedModules, id)) {\n return promises.push(installedModules[id] as Promise);\n }\n const onFactory = (factory: () => any) => {\n installedModules[id] = 0;\n webpackRequire.m[id] = (module) => {\n delete webpackRequire.c[id];\n const result = factory();\n // Add layer property from shareConfig if available\n const { shareInfo } = moduleToHandlerMapping[id];\n if (\n shareInfo?.shareConfig?.layer &&\n result &&\n typeof result === 'object'\n ) {\n try {\n // Only set layer if it's not already defined or if it's undefined\n if (\n !result.hasOwnProperty('layer') ||\n (result as any).layer === undefined\n ) {\n (result as any).layer = shareInfo.shareConfig.layer;\n }\n } catch (e) {\n // Ignore if layer property is read-only\n }\n }\n module.exports = result;\n };\n };\n const onError = (error: unknown) => {\n delete installedModules[id];\n webpackRequire.m[id] = (module) => {\n delete webpackRequire.c[id];\n throw error;\n };\n };\n try {\n const federationInstance = webpackRequire.federation.instance;\n if (!federationInstance) {\n throw new Error('Federation instance not found!');\n }\n const { shareKey, getter, shareInfo, treeShakingGetter } =\n moduleToHandlerMapping[id];\n const usedExports = getUsedExports(webpackRequire, shareKey);\n const customShareInfo: Partial = { ...shareInfo };\n // compatibility for rspack, which will wrap scope with array, but webpack will not\n // https://github.com/web-infra-dev/rspack/blob/main/packages/rspack/src/runtime/moduleFederationDefaultRuntime.js#L95\n if (\n Array.isArray(customShareInfo.scope) &&\n Array.isArray(customShareInfo.scope[0])\n ) {\n customShareInfo.scope = customShareInfo.scope[0];\n }\n if (usedExports) {\n customShareInfo.treeShaking = {\n usedExports,\n useIn: [federationInstance.options.name],\n };\n }\n const promise = federationInstance\n .loadShare(shareKey, {\n customShareInfo,\n })\n .then((factory: any) => {\n if (factory === false) {\n if (typeof getter !== 'function') {\n throw new Error(\n getShortErrorMsg(\n RUNTIME_012,\n {\n [RUNTIME_012]:\n 'The getter for the shared module is not a function. This may be caused by setting \"shared.import: false\" without the host providing the corresponding lib.',\n },\n { shareKey },\n ),\n );\n }\n return treeShakingGetter?.() || getter();\n }\n return factory;\n });\n\n if (promise.then) {\n promises.push(\n (installedModules[id] = promise.then(onFactory).catch(onError)),\n );\n } else {\n // @ts-ignore maintain previous logic\n onFactory(promise);\n }\n } catch (e) {\n onError(e);\n }\n });\n }\n}\n"],"mappings":";;;;;;;AAOA,SAAgB,SAAS,SAA0B;AACjD,4CAAqB,QAAQ;CAC7B,MAAM,EACJ,SACA,UACA,kBACA,gBACA,cACA,2BACE;AAEJ,iDAAoB,eAAe;AACnC,KAAI,eAAe,EAAE,cAAc,QAAQ,CACzC,cAAa,SAAS,SAAS,OAAO;AACpC,MAAI,eAAe,EAAE,kBAAkB,GAAG,CACxC,QAAO,SAAS,KAAK,iBAAiB,IAAoB;EAE5D,MAAM,aAAa,YAAuB;AACxC,oBAAiB,MAAM;AACvB,kBAAe,EAAE,OAAO,WAAW;AACjC,WAAO,eAAe,EAAE;IACxB,MAAM,SAAS,SAAS;IAExB,MAAM,EAAE,cAAc,uBAAuB;AAC7C,QACE,WAAW,aAAa,SACxB,UACA,OAAO,WAAW,SAElB,KAAI;AAEF,SACE,CAAC,OAAO,eAAe,QAAQ,IAC9B,OAAe,UAAU,OAE1B,CAAC,OAAe,QAAQ,UAAU,YAAY;aAEzC,GAAG;AAId,WAAO,UAAU;;;EAGrB,MAAM,W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