UNPKG

@module-federation/enhanced

Version:

This package provides enhanced features for module federation.

194 lines (191 loc) • 8.07 kB
'use strict'; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } }); const require_runtime = require('../../_virtual/_rolldown/runtime.js'); const require_lib_Constants = require('../Constants.js'); const require_lib_sharing_utils = require('./utils.js'); const require_lib_sharing_ConsumeSharedFallbackDependency = require('./ConsumeSharedFallbackDependency.js'); let _module_federation_sdk_normalize_webpack_path = require("@module-federation/sdk/normalize-webpack-path"); //#region src/lib/sharing/ConsumeSharedModule.ts const { rangeToString, stringifyHoley } = require((0, _module_federation_sdk_normalize_webpack_path.normalizeWebpackPath)("webpack/lib/util/semver")); const { AsyncDependenciesBlock, Module, RuntimeGlobals } = require((0, _module_federation_sdk_normalize_webpack_path.normalizeWebpackPath)("webpack")); const { sources: webpackSources } = require((0, _module_federation_sdk_normalize_webpack_path.normalizeWebpackPath)("webpack")); const makeSerializable = require((0, _module_federation_sdk_normalize_webpack_path.normalizeWebpackPath)("webpack/lib/util/makeSerializable")); /** * @typedef {Object} ConsumeOptions * @property {string=} import fallback request * @property {string=} importResolved resolved fallback request * @property {string} shareKey global share key * @property {string} shareScope share scope * @property {SemVerRange | false | undefined} requiredVersion version requirement * @property {string} packageName package name to determine required version automatically * @property {boolean} strictVersion don't use shared version even if version isn't valid * @property {boolean} singleton use single global version * @property {boolean} eager include the fallback module in a sync way * @property {string | null=} layer Share a specific layer of the module, if the module supports layers * @property {string | null=} issuerLayer Issuer layer in which the module should be resolved * @property {{ version?: string; fallbackVersion?: string }} exclude Options for excluding certain versions * @property {{ version?: string; fallbackVersion?: string }} include Options for including only certain versions */ const TYPES = new Set(["consume-shared"]); const JAVASCRIPT_TYPES = new Set(["javascript"]); var ConsumeSharedModule = class extends Module { /** * @param {string} context context * @param {ConsumeOptions} options consume options */ constructor(context, options) { super(require_lib_Constants.WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE, context, options?.layer ?? null); this.layer = options?.layer ?? null; this.options = options; } /** * @returns {string} a unique identifier of the module */ identifier() { const { shareKey, shareScope, importResolved, requiredVersion, strictVersion, singleton, eager, layer } = this.options; return `${require_lib_Constants.WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE}|${Array.isArray(shareScope) ? shareScope.join("|") : shareScope}|${shareKey}|${requiredVersion && rangeToString(requiredVersion)}|${strictVersion}|${importResolved}|${singleton}|${eager}|${layer}`; } /** * @param {RequestShortener} requestShortener the request shortener * @returns {string} a user readable identifier of the module */ readableIdentifier(requestShortener) { const { shareKey, shareScope, importResolved, requiredVersion, strictVersion, singleton, eager, layer } = this.options; return `consume shared module (${Array.isArray(shareScope) ? shareScope.join("|") : shareScope}) ${shareKey}@${requiredVersion ? rangeToString(requiredVersion) : "*"}${strictVersion ? " (strict)" : ""}${singleton ? " (singleton)" : ""}${importResolved ? ` (fallback: ${requestShortener.shorten(importResolved)})` : ""}${eager ? " (eager)" : ""}${layer ? ` (${layer})` : ""}`; } /** * @param {LibIdentOptions} options options * @returns {string | null} an identifier for library inclusion */ libIdent(options) { const { shareKey, shareScope, import: request } = this.options; const normalizedShareScope = Array.isArray(shareScope) ? shareScope.join("|") : shareScope; return `${this.layer ? `(${this.layer})/` : ""}webpack/sharing/consume/${normalizedShareScope}/${shareKey}${request ? `/${request}` : ""}`; } /** * @param {NeedBuildContext} context context info * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild * @returns {void} */ needBuild(context, callback) { callback(null, !this.buildInfo); } /** * @param {WebpackOptions} options webpack options * @param {Compilation} compilation the compilation * @param {ResolverWithOptions} resolver the resolver * @param {InputFileSystem} fs the file system * @param {function(WebpackError=): void} callback callback function * @returns {void} */ build(options, compilation, resolver, fs, callback) { this.buildMeta = {}; this.buildInfo = {}; if (this.options.import) { const dep = new require_lib_sharing_ConsumeSharedFallbackDependency.default(this.options.import, this.options.layer); if (this.options.eager) this.addDependency(dep); else { const block = new AsyncDependenciesBlock({}); block.addDependency(dep); this.addBlock(block); } } callback(); } /** * @returns {Set<string>} types available (do not mutate) */ getSourceTypes() { return TYPES; } getSourceBasicTypes() { return JAVASCRIPT_TYPES; } /** * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module (must be non-zero) */ size(type) { return 42; } /** * @param {Hash} hash the hash used to track dependencies * @param {UpdateHashContext} context context * @returns {void} */ updateHash(hash, context) { hash.update(JSON.stringify(this.options)); super.updateHash(hash, context); } /** * @param {CodeGenerationContext} context context for code generation * @returns {CodeGenerationResult} result */ codeGeneration({ chunkGraph, moduleGraph, runtimeTemplate }) { const runtimeRequirements = new Set([RuntimeGlobals.shareScopeMap]); const { shareScope, shareKey, strictVersion, requiredVersion, import: request, singleton, eager } = this.options; let fallbackCode; if (request) if (eager) { const dep = this.dependencies[0]; fallbackCode = runtimeTemplate.syncModuleFactory({ dependency: dep, chunkGraph, runtimeRequirements, request: this.options.import }); } else { const block = this.blocks[0]; fallbackCode = runtimeTemplate.asyncModuleFactory({ block, chunkGraph, runtimeRequirements, request: this.options.import }); } let fn = "load"; const args = [JSON.stringify(shareScope), JSON.stringify(shareKey)]; if (requiredVersion) { if (strictVersion) fn += "Strict"; if (singleton) fn += "Singleton"; args.push(stringifyHoley(requiredVersion)); fn += "VersionCheck"; } else if (singleton) fn += "Singleton"; if (fallbackCode) { fn += "Fallback"; args.push(fallbackCode); } const sources = /* @__PURE__ */ new Map(); sources.set("consume-shared", new webpackSources.RawSource(fallbackCode || `()=>()=>{throw new Error("Can not get '${shareKey}'")}`)); const data = /* @__PURE__ */ new Map(); data.set("consume-shared", require_lib_sharing_utils.normalizeConsumeShareOptions(this.options)); return { runtimeRequirements, sources, data }; } /** * @param {ObjectSerializerContext} context context */ serialize(context) { const { write } = context; write(this.options); write(this.layer); super.serialize(context); } /** * @param {ObjectDeserializerContext} context context */ deserialize(context) { const { read } = context; const options = read(); const layer = read(); this.options = options; this.layer = layer; super.deserialize(context); } }; makeSerializable(ConsumeSharedModule, "enhanced/lib/sharing/ConsumeSharedModule"); //#endregion exports.default = ConsumeSharedModule; //# sourceMappingURL=ConsumeSharedModule.js.map