UNPKG

zephyr-xpack-internal

Version:

Xpack internals for Zephyr

73 lines (72 loc) • 3.22 kB
import "node:module"; import { ZeErrors, ZephyrError } from "zephyr-agent"; function stableJson(value) { if (null === value) return 'null'; switch(typeof value){ case 'undefined': return 'undefined'; case 'string': case 'number': case 'boolean': return JSON.stringify(value); case 'bigint': return `bigint:${value.toString()}`; case 'symbol': return `symbol:${String(value)}`; case 'function': return `function:${String(value)}`; default: break; } if (Array.isArray(value)) return `[${value.map((item)=>stableJson(item)).join(',')}]`; const record = value; return `{${Object.keys(record).sort().map((key)=>`${JSON.stringify(key)}:${stableJson(record[key])}`).join(',')}}`; } function nonEmptyString(value) { return 'string' == typeof value && value.trim() ? value : void 0; } function mergeUnique(candidates, kind) { const byFingerprint = new Set(); const byIdentity = new Map(); const merged = []; for (const candidate of candidates)if (!byFingerprint.has(candidate.fingerprint)) { for (const identity of candidate.identities){ const existing = byIdentity.get(identity); if (existing && existing !== candidate.fingerprint) throw new ZephyrError(ZeErrors.ERR_DEPLOY_LOCAL_BUILD, { message: `Conflicting Module Federation ${kind} for ${identity}.` }); } byFingerprint.add(candidate.fingerprint); for (const identity of candidate.identities)byIdentity.set(identity, candidate.fingerprint); merged.push(candidate.value); } return merged; } function mergeModuleFederationConfigs(configs) { return mergeUnique(configs.flatMap((configList)=>(configList ?? []).map((config)=>({ value: config, fingerprint: stableJson(config), identities: [ nonEmptyString(config.name) ? `name:${config.name}` : void 0, nonEmptyString(config.filename) ? `entry:${config.filename}` : void 0 ].filter((identity)=>!!identity) }))), 'configuration'); } function getLegacyModuleFederationConfig(configs) { const config = configs?.length === 1 ? configs[0] : void 0; if (!config || !nonEmptyString(config.name) || !nonEmptyString(config.filename)) return; return config; } function mergeModuleFederationBuildMetadata(metadata) { return mergeUnique(metadata.flatMap((metadataList)=>(metadataList ?? []).map((entry)=>({ value: entry, fingerprint: stableJson(entry), identities: [ nonEmptyString(entry.name) ? `name:${entry.name}` : void 0, nonEmptyString(entry.remote) ? `entry:${entry.remote}` : void 0, nonEmptyString(entry.mf_manifest) ? `manifest:${entry.mf_manifest}` : void 0 ].filter((identity)=>!!identity) }))), 'metadata'); } export { getLegacyModuleFederationConfig, mergeModuleFederationBuildMetadata, mergeModuleFederationConfigs }; //# sourceMappingURL=federation-config-metadata.mjs.map