[79a0317] | 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 { CONSUME_SHARED_TYPES } = require("../ModuleSourceTypesConstants");
|
---|
| 12 | const {
|
---|
| 13 | WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE
|
---|
| 14 | } = require("../ModuleTypeConstants");
|
---|
| 15 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 16 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 17 | const { rangeToString, stringifyHoley } = require("../util/semver");
|
---|
| 18 | const ConsumeSharedFallbackDependency = require("./ConsumeSharedFallbackDependency");
|
---|
| 19 |
|
---|
| 20 | /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
---|
| 21 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 22 | /** @typedef {import("../ChunkGroup")} ChunkGroup */
|
---|
| 23 | /** @typedef {import("../Compilation")} Compilation */
|
---|
| 24 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 25 | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
---|
| 26 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
---|
| 27 | /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
|
---|
| 28 | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
---|
| 29 | /** @typedef {import("../Module").SourceTypes} SourceTypes */
|
---|
| 30 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
---|
| 31 | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
---|
| 32 | /** @typedef {import("../WebpackError")} WebpackError */
|
---|
| 33 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 34 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 35 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 36 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
---|
| 37 | /** @typedef {import("../util/semver").SemVerRange} SemVerRange */
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * @typedef {object} ConsumeOptions
|
---|
| 41 | * @property {string=} import fallback request
|
---|
| 42 | * @property {string=} importResolved resolved fallback request
|
---|
| 43 | * @property {string} shareKey global share key
|
---|
| 44 | * @property {string} shareScope share scope
|
---|
| 45 | * @property {SemVerRange | false | undefined} requiredVersion version requirement
|
---|
| 46 | * @property {string=} packageName package name to determine required version automatically
|
---|
| 47 | * @property {boolean} strictVersion don't use shared version even if version isn't valid
|
---|
| 48 | * @property {boolean} singleton use single global version
|
---|
| 49 | * @property {boolean} eager include the fallback module in a sync way
|
---|
| 50 | */
|
---|
| 51 |
|
---|
| 52 | class ConsumeSharedModule extends Module {
|
---|
| 53 | /**
|
---|
| 54 | * @param {string} context context
|
---|
| 55 | * @param {ConsumeOptions} options consume options
|
---|
| 56 | */
|
---|
| 57 | constructor(context, options) {
|
---|
| 58 | super(WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE, context);
|
---|
| 59 | this.options = options;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * @returns {string} a unique identifier of the module
|
---|
| 64 | */
|
---|
| 65 | identifier() {
|
---|
| 66 | const {
|
---|
| 67 | shareKey,
|
---|
| 68 | shareScope,
|
---|
| 69 | importResolved,
|
---|
| 70 | requiredVersion,
|
---|
| 71 | strictVersion,
|
---|
| 72 | singleton,
|
---|
| 73 | eager
|
---|
| 74 | } = this.options;
|
---|
| 75 | return `${WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE}|${shareScope}|${shareKey}|${
|
---|
| 76 | requiredVersion && rangeToString(requiredVersion)
|
---|
| 77 | }|${strictVersion}|${importResolved}|${singleton}|${eager}`;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * @param {RequestShortener} requestShortener the request shortener
|
---|
| 82 | * @returns {string} a user readable identifier of the module
|
---|
| 83 | */
|
---|
| 84 | readableIdentifier(requestShortener) {
|
---|
| 85 | const {
|
---|
| 86 | shareKey,
|
---|
| 87 | shareScope,
|
---|
| 88 | importResolved,
|
---|
| 89 | requiredVersion,
|
---|
| 90 | strictVersion,
|
---|
| 91 | singleton,
|
---|
| 92 | eager
|
---|
| 93 | } = this.options;
|
---|
| 94 | return `consume shared module (${shareScope}) ${shareKey}@${
|
---|
| 95 | requiredVersion ? rangeToString(requiredVersion) : "*"
|
---|
| 96 | }${strictVersion ? " (strict)" : ""}${singleton ? " (singleton)" : ""}${
|
---|
| 97 | importResolved
|
---|
| 98 | ? ` (fallback: ${requestShortener.shorten(importResolved)})`
|
---|
| 99 | : ""
|
---|
| 100 | }${eager ? " (eager)" : ""}`;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * @param {LibIdentOptions} options options
|
---|
| 105 | * @returns {string | null} an identifier for library inclusion
|
---|
| 106 | */
|
---|
| 107 | libIdent(options) {
|
---|
| 108 | const { shareKey, shareScope, import: request } = this.options;
|
---|
| 109 | return `${
|
---|
| 110 | this.layer ? `(${this.layer})/` : ""
|
---|
| 111 | }webpack/sharing/consume/${shareScope}/${shareKey}${
|
---|
| 112 | request ? `/${request}` : ""
|
---|
| 113 | }`;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * @param {NeedBuildContext} context context info
|
---|
| 118 | * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
|
---|
| 119 | * @returns {void}
|
---|
| 120 | */
|
---|
| 121 | needBuild(context, callback) {
|
---|
| 122 | callback(null, !this.buildInfo);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * @param {WebpackOptions} options webpack options
|
---|
| 127 | * @param {Compilation} compilation the compilation
|
---|
| 128 | * @param {ResolverWithOptions} resolver the resolver
|
---|
| 129 | * @param {InputFileSystem} fs the file system
|
---|
| 130 | * @param {function(WebpackError=): void} callback callback function
|
---|
| 131 | * @returns {void}
|
---|
| 132 | */
|
---|
| 133 | build(options, compilation, resolver, fs, callback) {
|
---|
| 134 | this.buildMeta = {};
|
---|
| 135 | this.buildInfo = {};
|
---|
| 136 | if (this.options.import) {
|
---|
| 137 | const dep = new ConsumeSharedFallbackDependency(this.options.import);
|
---|
| 138 | if (this.options.eager) {
|
---|
| 139 | this.addDependency(dep);
|
---|
| 140 | } else {
|
---|
| 141 | const block = new AsyncDependenciesBlock({});
|
---|
| 142 | block.addDependency(dep);
|
---|
| 143 | this.addBlock(block);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | callback();
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * @returns {SourceTypes} types available (do not mutate)
|
---|
| 151 | */
|
---|
| 152 | getSourceTypes() {
|
---|
| 153 | return CONSUME_SHARED_TYPES;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * @param {string=} type the source type for which the size should be estimated
|
---|
| 158 | * @returns {number} the estimated size of the module (must be non-zero)
|
---|
| 159 | */
|
---|
| 160 | size(type) {
|
---|
| 161 | return 42;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
| 165 | * @param {Hash} hash the hash used to track dependencies
|
---|
| 166 | * @param {UpdateHashContext} context context
|
---|
| 167 | * @returns {void}
|
---|
| 168 | */
|
---|
| 169 | updateHash(hash, context) {
|
---|
| 170 | hash.update(JSON.stringify(this.options));
|
---|
| 171 | super.updateHash(hash, context);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * @param {CodeGenerationContext} context context for code generation
|
---|
| 176 | * @returns {CodeGenerationResult} result
|
---|
| 177 | */
|
---|
| 178 | codeGeneration({ chunkGraph, moduleGraph, runtimeTemplate }) {
|
---|
| 179 | const runtimeRequirements = new Set([RuntimeGlobals.shareScopeMap]);
|
---|
| 180 | const {
|
---|
| 181 | shareScope,
|
---|
| 182 | shareKey,
|
---|
| 183 | strictVersion,
|
---|
| 184 | requiredVersion,
|
---|
| 185 | import: request,
|
---|
| 186 | singleton,
|
---|
| 187 | eager
|
---|
| 188 | } = this.options;
|
---|
| 189 | let fallbackCode;
|
---|
| 190 | if (request) {
|
---|
| 191 | if (eager) {
|
---|
| 192 | const dep = this.dependencies[0];
|
---|
| 193 | fallbackCode = runtimeTemplate.syncModuleFactory({
|
---|
| 194 | dependency: dep,
|
---|
| 195 | chunkGraph,
|
---|
| 196 | runtimeRequirements,
|
---|
| 197 | request: this.options.import
|
---|
| 198 | });
|
---|
| 199 | } else {
|
---|
| 200 | const block = this.blocks[0];
|
---|
| 201 | fallbackCode = runtimeTemplate.asyncModuleFactory({
|
---|
| 202 | block,
|
---|
| 203 | chunkGraph,
|
---|
| 204 | runtimeRequirements,
|
---|
| 205 | request: this.options.import
|
---|
| 206 | });
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | const args = [
|
---|
| 211 | JSON.stringify(shareScope),
|
---|
| 212 | JSON.stringify(shareKey),
|
---|
| 213 | JSON.stringify(eager)
|
---|
| 214 | ];
|
---|
| 215 | if (requiredVersion) {
|
---|
| 216 | args.push(stringifyHoley(requiredVersion));
|
---|
| 217 | }
|
---|
| 218 | if (fallbackCode) {
|
---|
| 219 | args.push(fallbackCode);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | let fn;
|
---|
| 223 |
|
---|
| 224 | if (requiredVersion) {
|
---|
| 225 | if (strictVersion) {
|
---|
| 226 | fn = singleton ? "loadStrictSingletonVersion" : "loadStrictVersion";
|
---|
| 227 | } else {
|
---|
| 228 | fn = singleton ? "loadSingletonVersion" : "loadVersion";
|
---|
| 229 | }
|
---|
| 230 | } else {
|
---|
| 231 | fn = singleton ? "loadSingleton" : "load";
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | const code = runtimeTemplate.returningFunction(`${fn}(${args.join(", ")})`);
|
---|
| 235 | const sources = new Map();
|
---|
| 236 | sources.set("consume-shared", new RawSource(code));
|
---|
| 237 | return {
|
---|
| 238 | runtimeRequirements,
|
---|
| 239 | sources
|
---|
| 240 | };
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | /**
|
---|
| 244 | * @param {ObjectSerializerContext} context context
|
---|
| 245 | */
|
---|
| 246 | serialize(context) {
|
---|
| 247 | const { write } = context;
|
---|
| 248 | write(this.options);
|
---|
| 249 | super.serialize(context);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
| 253 | * @param {ObjectDeserializerContext} context context
|
---|
| 254 | */
|
---|
| 255 | deserialize(context) {
|
---|
| 256 | const { read } = context;
|
---|
| 257 | this.options = read();
|
---|
| 258 | super.deserialize(context);
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | makeSerializable(
|
---|
| 263 | ConsumeSharedModule,
|
---|
| 264 | "webpack/lib/sharing/ConsumeSharedModule"
|
---|
| 265 | );
|
---|
| 266 |
|
---|
| 267 | module.exports = ConsumeSharedModule;
|
---|