UNPKG

@module-federation/enhanced

Version:

This package provides enhanced features for module federation.

78 lines (76 loc) 2.93 kB
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } }); const require_runtime = require('../../../_virtual/_rolldown/runtime.js'); let path = require("path"); path = require_runtime.__toESM(path); let fs = require("fs"); fs = require_runtime.__toESM(fs); //#region src/lib/sharing/tree-shaking/CollectSharedEntryPlugin.ts const PLUGIN_NAME = "CollectSharedEntryPlugin"; function inferPkgVersionFromResource(resource) { try { const nmIndex = resource.lastIndexOf("node_modules"); if (nmIndex === -1) return; const after = resource.substring(nmIndex + 12 + 1); if (after.startsWith(".pnpm/")) { const m = after.match(/\.pnpm\/(?:[^/]+)@([^/]+)\/node_modules\//); if (m && m[1]) return m[1]; } const parts = after.split(/[\\/]+/).filter(Boolean); if (!parts.length) return; let pkgPathParts; if (parts[0].startsWith("@") && parts.length >= 2) pkgPathParts = [parts[0], parts[1]]; else pkgPathParts = [parts[0]]; const nmBase = resource.substring(0, nmIndex + 12 + 1); const pkgJsonPath = path.join(nmBase, ...pkgPathParts, "package.json"); try { const content = fs.readFileSync(pkgJsonPath, "utf-8"); const v = JSON.parse(content)?.version; if (typeof v === "string" && v) return v; } catch {} return; } catch { return; } } var CollectSharedEntryPlugin = class { constructor(options) { this.name = PLUGIN_NAME; this.name = PLUGIN_NAME; const { sharedOptions } = options; this.sharedOptions = sharedOptions; this._collectedEntries = {}; } getData() { return this._collectedEntries; } apply(compiler) { const { sharedOptions } = this; const { _collectedEntries: collectedEntries } = this; compiler.hooks.compilation.tap("CollectSharedEntryPlugin", (_compilation, { normalModuleFactory }) => { normalModuleFactory.hooks.module.tap("CollectSharedEntryPlugin", (module, { resource }, resolveData) => { if (!resource || !("rawRequest" in module)) return module; const matchedSharedOption = sharedOptions.find((item) => item[0] === module.rawRequest); if (!matchedSharedOption) return module; const [sharedName, _] = matchedSharedOption; const sharedVersion = inferPkgVersionFromResource(resource); if (!sharedVersion) return module; collectedEntries[sharedName] ||= { requests: [] }; collectedEntries[sharedName].requests.push([resource, sharedVersion]); return module; }); }); compiler.hooks.thisCompilation.tap("Collect shared entry", (compilation) => { compilation.hooks.processAssets.tapPromise({ name: "CollectSharedEntry", stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE }, async () => { compilation.getAssets().forEach((asset) => { compilation.deleteAsset(asset.name); }); }); }); } }; //#endregion exports.default = CollectSharedEntryPlugin; //# sourceMappingURL=CollectSharedEntryPlugin.js.map