| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { RawSource } = require("webpack-sources");
|
|---|
| 9 | const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
|---|
| 10 | const Module = require("../Module");
|
|---|
| 11 | const {
|
|---|
| 12 | CONSUME_SHARED_TYPES,
|
|---|
| 13 | JAVASCRIPT_TYPES
|
|---|
| 14 | } = require("../ModuleSourceTypeConstants");
|
|---|
| 15 | const {
|
|---|
| 16 | WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE
|
|---|
| 17 | } = require("../ModuleTypeConstants");
|
|---|
| 18 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 19 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 20 | const { rangeToString, stringifyHoley } = require("../util/semver");
|
|---|
| 21 | const ConsumeSharedFallbackDependency = require("./ConsumeSharedFallbackDependency");
|
|---|
| 22 |
|
|---|
| 23 | /** @type {WeakMap<ModuleGraph, WeakMap<ConsumeSharedModule, Module | null>>} */
|
|---|
| 24 | const fallbackModuleCache = new WeakMap();
|
|---|
| 25 |
|
|---|
| 26 | /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
|---|
| 27 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 28 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 29 | /** @typedef {import("../Module").BuildCallback} BuildCallback */
|
|---|
| 30 | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
|---|
| 31 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
|---|
| 32 | /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
|
|---|
| 33 | /** @typedef {import("../Module").LibIdent} LibIdent */
|
|---|
| 34 | /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
|
|---|
| 35 | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
|---|
| 36 | /** @typedef {import("../Module").Sources} Sources */
|
|---|
| 37 | /** @typedef {import("../Module").SourceTypes} SourceTypes */
|
|---|
| 38 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 39 | /** @typedef {import("../Module").ExportsType} ExportsType */
|
|---|
| 40 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
|---|
| 41 | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
|---|
| 42 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 43 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 44 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 45 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 46 | /** @typedef {import("../util/semver").SemVerRange} SemVerRange */
|
|---|
| 47 | /** @typedef {import("../Module").BasicSourceTypes} BasicSourceTypes */
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Represents the consume shared module runtime component.
|
|---|
| 51 | * @typedef {object} ConsumeOptions
|
|---|
| 52 | * @property {string=} import fallback request
|
|---|
| 53 | * @property {string=} importResolved resolved fallback request
|
|---|
| 54 | * @property {string} shareKey global share key
|
|---|
| 55 | * @property {string} shareScope share scope
|
|---|
| 56 | * @property {SemVerRange | false | undefined} requiredVersion version requirement
|
|---|
| 57 | * @property {string=} packageName package name to determine required version automatically
|
|---|
| 58 | * @property {boolean} strictVersion don't use shared version even if version isn't valid
|
|---|
| 59 | * @property {boolean} singleton use single global version
|
|---|
| 60 | * @property {boolean} eager include the fallback module in a sync way
|
|---|
| 61 | */
|
|---|
| 62 |
|
|---|
| 63 | class ConsumeSharedModule extends Module {
|
|---|
| 64 | /**
|
|---|
| 65 | * Creates an instance of ConsumeSharedModule.
|
|---|
| 66 | * @param {string} context context
|
|---|
| 67 | * @param {ConsumeOptions} options consume options
|
|---|
| 68 | */
|
|---|
| 69 | constructor(context, options) {
|
|---|
| 70 | super(WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE, context);
|
|---|
| 71 | this.options = options;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Returns the unique identifier used to reference this module.
|
|---|
| 76 | * @returns {string} a unique identifier of the module
|
|---|
| 77 | */
|
|---|
| 78 | identifier() {
|
|---|
| 79 | const {
|
|---|
| 80 | shareKey,
|
|---|
| 81 | shareScope,
|
|---|
| 82 | importResolved,
|
|---|
| 83 | requiredVersion,
|
|---|
| 84 | strictVersion,
|
|---|
| 85 | singleton,
|
|---|
| 86 | eager
|
|---|
| 87 | } = this.options;
|
|---|
| 88 | return `${WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE}|${shareScope}|${shareKey}|${
|
|---|
| 89 | requiredVersion && rangeToString(requiredVersion)
|
|---|
| 90 | }|${strictVersion}|${importResolved}|${singleton}|${eager}`;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Returns a human-readable identifier for this module.
|
|---|
| 95 | * @param {RequestShortener} requestShortener the request shortener
|
|---|
| 96 | * @returns {string} a user readable identifier of the module
|
|---|
| 97 | */
|
|---|
| 98 | readableIdentifier(requestShortener) {
|
|---|
| 99 | const {
|
|---|
| 100 | shareKey,
|
|---|
| 101 | shareScope,
|
|---|
| 102 | importResolved,
|
|---|
| 103 | requiredVersion,
|
|---|
| 104 | strictVersion,
|
|---|
| 105 | singleton,
|
|---|
| 106 | eager
|
|---|
| 107 | } = this.options;
|
|---|
| 108 | return `consume shared module (${shareScope}) ${shareKey}@${
|
|---|
| 109 | requiredVersion ? rangeToString(requiredVersion) : "*"
|
|---|
| 110 | }${strictVersion ? " (strict)" : ""}${singleton ? " (singleton)" : ""}${
|
|---|
| 111 | importResolved
|
|---|
| 112 | ? ` (fallback: ${requestShortener.shorten(importResolved)})`
|
|---|
| 113 | : ""
|
|---|
| 114 | }${eager ? " (eager)" : ""}`;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * Gets the library identifier.
|
|---|
| 119 | * @param {LibIdentOptions} options options
|
|---|
| 120 | * @returns {LibIdent | null} an identifier for library inclusion
|
|---|
| 121 | */
|
|---|
| 122 | libIdent(options) {
|
|---|
| 123 | const { shareKey, shareScope, import: request } = this.options;
|
|---|
| 124 | return `${
|
|---|
| 125 | this.layer ? `(${this.layer})/` : ""
|
|---|
| 126 | }webpack/sharing/consume/${shareScope}/${shareKey}${
|
|---|
| 127 | request ? `/${request}` : ""
|
|---|
| 128 | }`;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Checks whether the module needs to be rebuilt for the current build state.
|
|---|
| 133 | * @param {NeedBuildContext} context context info
|
|---|
| 134 | * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
|
|---|
| 135 | * @returns {void}
|
|---|
| 136 | */
|
|---|
| 137 | needBuild(context, callback) {
|
|---|
| 138 | callback(null, !this.buildInfo);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /**
|
|---|
| 142 | * Builds the module using the provided compilation context.
|
|---|
| 143 | * @param {WebpackOptions} options webpack options
|
|---|
| 144 | * @param {Compilation} compilation the compilation
|
|---|
| 145 | * @param {ResolverWithOptions} resolver the resolver
|
|---|
| 146 | * @param {InputFileSystem} fs the file system
|
|---|
| 147 | * @param {BuildCallback} callback callback function
|
|---|
| 148 | * @returns {void}
|
|---|
| 149 | */
|
|---|
| 150 | build(options, compilation, resolver, fs, callback) {
|
|---|
| 151 | this.buildMeta = {};
|
|---|
| 152 | this.buildInfo = {};
|
|---|
| 153 | if (this.options.import) {
|
|---|
| 154 | const dep = new ConsumeSharedFallbackDependency(this.options.import);
|
|---|
| 155 | if (this.options.eager) {
|
|---|
| 156 | this.addDependency(dep);
|
|---|
| 157 | } else {
|
|---|
| 158 | const block = new AsyncDependenciesBlock({});
|
|---|
| 159 | block.addDependency(dep);
|
|---|
| 160 | this.addBlock(block);
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | callback();
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Returns the source types this module can generate.
|
|---|
| 168 | * @returns {SourceTypes} types available (do not mutate)
|
|---|
| 169 | */
|
|---|
| 170 | getSourceTypes() {
|
|---|
| 171 | return CONSUME_SHARED_TYPES;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /**
|
|---|
| 175 | * Basic source types are high-level categories like javascript, css, webassembly, etc.
|
|---|
| 176 | * We only have built-in knowledge about the javascript basic type here; other basic types may be
|
|---|
| 177 | * added or changed over time by generators and do not need to be handled or detected here.
|
|---|
| 178 | *
|
|---|
| 179 | * Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
|
|---|
| 180 | * from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
|
|---|
| 181 | * @returns {BasicSourceTypes} types available (do not mutate)
|
|---|
| 182 | */
|
|---|
| 183 | getSourceBasicTypes() {
|
|---|
| 184 | return JAVASCRIPT_TYPES;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| 188 | * Get fallback module.
|
|---|
| 189 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 190 | * @returns {Module | null} fallback module
|
|---|
| 191 | */
|
|---|
| 192 | _getFallbackModule(moduleGraph) {
|
|---|
| 193 | let moduleCache = fallbackModuleCache.get(moduleGraph);
|
|---|
| 194 | if (!moduleCache) {
|
|---|
| 195 | moduleCache = new WeakMap();
|
|---|
| 196 | fallbackModuleCache.set(moduleGraph, moduleCache);
|
|---|
| 197 | }
|
|---|
| 198 | const cached = moduleCache.get(this);
|
|---|
| 199 | if (cached !== undefined) {
|
|---|
| 200 | return cached;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /** @type {undefined | null | Module} */
|
|---|
| 204 | let fallbackModule = null;
|
|---|
| 205 |
|
|---|
| 206 | if (this.options.import) {
|
|---|
| 207 | if (this.options.eager) {
|
|---|
| 208 | const dep = this.dependencies[0];
|
|---|
| 209 | if (dep) {
|
|---|
| 210 | fallbackModule = moduleGraph.getModule(dep);
|
|---|
| 211 | }
|
|---|
| 212 | } else {
|
|---|
| 213 | const block = this.blocks[0];
|
|---|
| 214 | if (block && block.dependencies.length > 0) {
|
|---|
| 215 | fallbackModule = moduleGraph.getModule(block.dependencies[0]);
|
|---|
| 216 | }
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | moduleCache.set(this, fallbackModule);
|
|---|
| 221 | return fallbackModule;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /**
|
|---|
| 225 | * Returns export type.
|
|---|
| 226 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 227 | * @param {boolean | undefined} strict the importing module is strict
|
|---|
| 228 | * @returns {ExportsType} export type
|
|---|
| 229 | * "namespace": Exports is already a namespace object. namespace = exports.
|
|---|
| 230 | * "dynamic": Check at runtime if __esModule is set. When set: namespace = { ...exports, default: exports }. When not set: namespace = { default: exports }.
|
|---|
| 231 | * "default-only": Provide a namespace object with only default export. namespace = { default: exports }
|
|---|
| 232 | * "default-with-named": Provide a namespace object with named and default export. namespace = { ...exports, default: exports }
|
|---|
| 233 | */
|
|---|
| 234 | getExportsType(moduleGraph, strict) {
|
|---|
| 235 | const fallbackModule = this._getFallbackModule(moduleGraph);
|
|---|
| 236 | if (!fallbackModule) return "dynamic";
|
|---|
| 237 | return fallbackModule.getExportsType(moduleGraph, strict);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /**
|
|---|
| 241 | * Returns the estimated size for the requested source type.
|
|---|
| 242 | * @param {string=} type the source type for which the size should be estimated
|
|---|
| 243 | * @returns {number} the estimated size of the module (must be non-zero)
|
|---|
| 244 | */
|
|---|
| 245 | size(type) {
|
|---|
| 246 | return 42;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /**
|
|---|
| 250 | * Updates the hash with the data contributed by this instance.
|
|---|
| 251 | * @param {Hash} hash the hash used to track dependencies
|
|---|
| 252 | * @param {UpdateHashContext} context context
|
|---|
| 253 | * @returns {void}
|
|---|
| 254 | */
|
|---|
| 255 | updateHash(hash, context) {
|
|---|
| 256 | hash.update(JSON.stringify(this.options));
|
|---|
| 257 | super.updateHash(hash, context);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | /**
|
|---|
| 261 | * Generates code and runtime requirements for this module.
|
|---|
| 262 | * @param {CodeGenerationContext} context context for code generation
|
|---|
| 263 | * @returns {CodeGenerationResult} result
|
|---|
| 264 | */
|
|---|
| 265 | codeGeneration({ chunkGraph, runtimeTemplate }) {
|
|---|
| 266 | const runtimeRequirements = new Set([RuntimeGlobals.shareScopeMap]);
|
|---|
| 267 | const {
|
|---|
| 268 | shareScope,
|
|---|
| 269 | shareKey,
|
|---|
| 270 | strictVersion,
|
|---|
| 271 | requiredVersion,
|
|---|
| 272 | import: request,
|
|---|
| 273 | singleton,
|
|---|
| 274 | eager
|
|---|
| 275 | } = this.options;
|
|---|
| 276 | /** @type {undefined | string} */
|
|---|
| 277 | let fallbackCode;
|
|---|
| 278 | if (request) {
|
|---|
| 279 | if (eager) {
|
|---|
| 280 | const dep = this.dependencies[0];
|
|---|
| 281 | fallbackCode = runtimeTemplate.syncModuleFactory({
|
|---|
| 282 | dependency: dep,
|
|---|
| 283 | chunkGraph,
|
|---|
| 284 | runtimeRequirements,
|
|---|
| 285 | request: this.options.import
|
|---|
| 286 | });
|
|---|
| 287 | } else {
|
|---|
| 288 | const block = this.blocks[0];
|
|---|
| 289 | fallbackCode = runtimeTemplate.asyncModuleFactory({
|
|---|
| 290 | block,
|
|---|
| 291 | chunkGraph,
|
|---|
| 292 | runtimeRequirements,
|
|---|
| 293 | request: this.options.import
|
|---|
| 294 | });
|
|---|
| 295 | }
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | const args = [
|
|---|
| 299 | JSON.stringify(shareScope),
|
|---|
| 300 | JSON.stringify(shareKey),
|
|---|
| 301 | JSON.stringify(eager)
|
|---|
| 302 | ];
|
|---|
| 303 | if (requiredVersion) {
|
|---|
| 304 | args.push(stringifyHoley(requiredVersion));
|
|---|
| 305 | }
|
|---|
| 306 | if (fallbackCode) {
|
|---|
| 307 | args.push(fallbackCode);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /** @type {string} */
|
|---|
| 311 | let fn;
|
|---|
| 312 |
|
|---|
| 313 | if (requiredVersion) {
|
|---|
| 314 | if (strictVersion) {
|
|---|
| 315 | fn = singleton ? "loadStrictSingletonVersion" : "loadStrictVersion";
|
|---|
| 316 | } else {
|
|---|
| 317 | fn = singleton ? "loadSingletonVersion" : "loadVersion";
|
|---|
| 318 | }
|
|---|
| 319 | } else {
|
|---|
| 320 | fn = singleton ? "loadSingleton" : "load";
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | const code = runtimeTemplate.returningFunction(`${fn}(${args.join(", ")})`);
|
|---|
| 324 | /** @type {Sources} */
|
|---|
| 325 | const sources = new Map();
|
|---|
| 326 | sources.set("consume-shared", new RawSource(code));
|
|---|
| 327 | return {
|
|---|
| 328 | runtimeRequirements,
|
|---|
| 329 | sources
|
|---|
| 330 | };
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | /**
|
|---|
| 334 | * Serializes this instance into the provided serializer context.
|
|---|
| 335 | * @param {ObjectSerializerContext} context context
|
|---|
| 336 | */
|
|---|
| 337 | serialize(context) {
|
|---|
| 338 | const { write } = context;
|
|---|
| 339 | write(this.options);
|
|---|
| 340 | super.serialize(context);
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /**
|
|---|
| 344 | * Restores this instance from the provided deserializer context.
|
|---|
| 345 | * @param {ObjectDeserializerContext} context context
|
|---|
| 346 | */
|
|---|
| 347 | deserialize(context) {
|
|---|
| 348 | const { read } = context;
|
|---|
| 349 | this.options = read();
|
|---|
| 350 | super.deserialize(context);
|
|---|
| 351 | }
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | makeSerializable(
|
|---|
| 355 | ConsumeSharedModule,
|
|---|
| 356 | "webpack/lib/sharing/ConsumeSharedModule"
|
|---|
| 357 | );
|
|---|
| 358 |
|
|---|
| 359 | module.exports = ConsumeSharedModule;
|
|---|