source: trip-planner-front/node_modules/webpack/lib/sharing/ConsumeSharedModule.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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