ContextModule.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { OriginalSource, RawSource } = require("webpack-sources");
  7. const AsyncDependenciesBlock = require("./AsyncDependenciesBlock");
  8. const { makeWebpackError } = require("./HookWebpackError");
  9. const Module = require("./Module");
  10. const { JS_TYPES } = require("./ModuleSourceTypesConstants");
  11. const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
  12. const RuntimeGlobals = require("./RuntimeGlobals");
  13. const Template = require("./Template");
  14. const WebpackError = require("./WebpackError");
  15. const {
  16. compareLocations,
  17. concatComparators,
  18. compareSelect,
  19. keepOriginalOrder,
  20. compareModulesById
  21. } = require("./util/comparators");
  22. const {
  23. contextify,
  24. parseResource,
  25. makePathsRelative
  26. } = require("./util/identifier");
  27. const makeSerializable = require("./util/makeSerializable");
  28. /** @typedef {import("webpack-sources").Source} Source */
  29. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  30. /** @typedef {import("./Chunk")} Chunk */
  31. /** @typedef {import("./Chunk").ChunkId} ChunkId */
  32. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  33. /** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
  34. /** @typedef {import("./ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
  35. /** @typedef {import("./Compilation")} Compilation */
  36. /** @typedef {import("./Dependency")} Dependency */
  37. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  38. /** @typedef {import("./Generator").SourceTypes} SourceTypes */
  39. /** @typedef {import("./Module").BuildInfo} BuildInfo */
  40. /** @typedef {import("./Module").BuildMeta} BuildMeta */
  41. /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
  42. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  43. /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
  44. /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
  45. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  46. /** @typedef {import("./RequestShortener")} RequestShortener */
  47. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  48. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  49. /** @typedef {import("./dependencies/ContextElementDependency")} ContextElementDependency */
  50. /** @typedef {import("./javascript/JavascriptParser").ImportAttributes} ImportAttributes */
  51. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  52. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  53. /** @template T @typedef {import("./util/LazySet")<T>} LazySet<T> */
  54. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  55. /** @typedef {"sync" | "eager" | "weak" | "async-weak" | "lazy" | "lazy-once"} ContextMode Context mode */
  56. /**
  57. * @typedef {object} ContextOptions
  58. * @property {ContextMode} mode
  59. * @property {boolean} recursive
  60. * @property {RegExp} regExp
  61. * @property {("strict" | boolean)=} namespaceObject
  62. * @property {string=} addon
  63. * @property {(string | null)=} chunkName
  64. * @property {(RegExp | null)=} include
  65. * @property {(RegExp | null)=} exclude
  66. * @property {RawChunkGroupOptions=} groupOptions
  67. * @property {string=} typePrefix
  68. * @property {string=} category
  69. * @property {(string[][] | null)=} referencedExports exports referenced from modules (won't be mangled)
  70. * @property {string=} layer
  71. * @property {ImportAttributes=} attributes
  72. */
  73. /**
  74. * @typedef {object} ContextModuleOptionsExtras
  75. * @property {false|string|string[]} resource
  76. * @property {string=} resourceQuery
  77. * @property {string=} resourceFragment
  78. * @property {TODO} resolveOptions
  79. */
  80. /** @typedef {ContextOptions & ContextModuleOptionsExtras} ContextModuleOptions */
  81. /**
  82. * @callback ResolveDependenciesCallback
  83. * @param {Error | null} err
  84. * @param {ContextElementDependency[]=} dependencies
  85. */
  86. /**
  87. * @callback ResolveDependencies
  88. * @param {InputFileSystem} fs
  89. * @param {ContextModuleOptions} options
  90. * @param {ResolveDependenciesCallback} callback
  91. */
  92. /** @typedef {1 | 3 | 7 | 9} FakeMapType */
  93. /** @typedef {Record<ModuleId, FakeMapType>} FakeMap */
  94. const SNAPSHOT_OPTIONS = { timestamp: true };
  95. class ContextModule extends Module {
  96. /**
  97. * @param {ResolveDependencies} resolveDependencies function to get dependencies in this context
  98. * @param {ContextModuleOptions} options options object
  99. */
  100. constructor(resolveDependencies, options) {
  101. if (!options || typeof options.resource === "string") {
  102. const parsed = parseResource(
  103. options ? /** @type {string} */ (options.resource) : ""
  104. );
  105. const resource = parsed.path;
  106. const resourceQuery = (options && options.resourceQuery) || parsed.query;
  107. const resourceFragment =
  108. (options && options.resourceFragment) || parsed.fragment;
  109. const layer = options && options.layer;
  110. super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, resource, layer);
  111. /** @type {ContextModuleOptions} */
  112. this.options = {
  113. ...options,
  114. resource,
  115. resourceQuery,
  116. resourceFragment
  117. };
  118. } else {
  119. super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, undefined, options.layer);
  120. /** @type {ContextModuleOptions} */
  121. this.options = {
  122. ...options,
  123. resource: options.resource,
  124. resourceQuery: options.resourceQuery || "",
  125. resourceFragment: options.resourceFragment || ""
  126. };
  127. }
  128. // Info from Factory
  129. /** @type {ResolveDependencies | undefined} */
  130. this.resolveDependencies = resolveDependencies;
  131. if (options && options.resolveOptions !== undefined) {
  132. this.resolveOptions = options.resolveOptions;
  133. }
  134. if (options && typeof options.mode !== "string") {
  135. throw new Error("options.mode is a required option");
  136. }
  137. this._identifier = this._createIdentifier();
  138. this._forceBuild = true;
  139. }
  140. /**
  141. * @returns {SourceTypes} types available (do not mutate)
  142. */
  143. getSourceTypes() {
  144. return JS_TYPES;
  145. }
  146. /**
  147. * Assuming this module is in the cache. Update the (cached) module with
  148. * the fresh module from the factory. Usually updates internal references
  149. * and properties.
  150. * @param {Module} module fresh module
  151. * @returns {void}
  152. */
  153. updateCacheModule(module) {
  154. const m = /** @type {ContextModule} */ (module);
  155. this.resolveDependencies = m.resolveDependencies;
  156. this.options = m.options;
  157. }
  158. /**
  159. * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
  160. */
  161. cleanupForCache() {
  162. super.cleanupForCache();
  163. this.resolveDependencies = undefined;
  164. }
  165. /**
  166. * @private
  167. * @param {RegExp} regexString RegExp as a string
  168. * @param {boolean=} stripSlash do we need to strip a slsh
  169. * @returns {string} pretty RegExp
  170. */
  171. _prettyRegExp(regexString, stripSlash = true) {
  172. const str = stripSlash
  173. ? regexString.source + regexString.flags
  174. : `${regexString}`;
  175. return str.replace(/!/g, "%21").replace(/\|/g, "%7C");
  176. }
  177. _createIdentifier() {
  178. let identifier =
  179. this.context ||
  180. (typeof this.options.resource === "string" ||
  181. this.options.resource === false
  182. ? `${this.options.resource}`
  183. : this.options.resource.join("|"));
  184. if (this.options.resourceQuery) {
  185. identifier += `|${this.options.resourceQuery}`;
  186. }
  187. if (this.options.resourceFragment) {
  188. identifier += `|${this.options.resourceFragment}`;
  189. }
  190. if (this.options.mode) {
  191. identifier += `|${this.options.mode}`;
  192. }
  193. if (!this.options.recursive) {
  194. identifier += "|nonrecursive";
  195. }
  196. if (this.options.addon) {
  197. identifier += `|${this.options.addon}`;
  198. }
  199. if (this.options.regExp) {
  200. identifier += `|${this._prettyRegExp(this.options.regExp, false)}`;
  201. }
  202. if (this.options.include) {
  203. identifier += `|include: ${this._prettyRegExp(
  204. this.options.include,
  205. false
  206. )}`;
  207. }
  208. if (this.options.exclude) {
  209. identifier += `|exclude: ${this._prettyRegExp(
  210. this.options.exclude,
  211. false
  212. )}`;
  213. }
  214. if (this.options.referencedExports) {
  215. identifier += `|referencedExports: ${JSON.stringify(
  216. this.options.referencedExports
  217. )}`;
  218. }
  219. if (this.options.chunkName) {
  220. identifier += `|chunkName: ${this.options.chunkName}`;
  221. }
  222. if (this.options.groupOptions) {
  223. identifier += `|groupOptions: ${JSON.stringify(
  224. this.options.groupOptions
  225. )}`;
  226. }
  227. if (this.options.namespaceObject === "strict") {
  228. identifier += "|strict namespace object";
  229. } else if (this.options.namespaceObject) {
  230. identifier += "|namespace object";
  231. }
  232. if (this.layer) {
  233. identifier += `|layer: ${this.layer}`;
  234. }
  235. return identifier;
  236. }
  237. /**
  238. * @returns {string} a unique identifier of the module
  239. */
  240. identifier() {
  241. return this._identifier;
  242. }
  243. /**
  244. * @param {RequestShortener} requestShortener the request shortener
  245. * @returns {string} a user readable identifier of the module
  246. */
  247. readableIdentifier(requestShortener) {
  248. let identifier;
  249. if (this.context) {
  250. identifier = `${requestShortener.shorten(this.context)}/`;
  251. } else if (
  252. typeof this.options.resource === "string" ||
  253. this.options.resource === false
  254. ) {
  255. identifier = `${requestShortener.shorten(`${this.options.resource}`)}/`;
  256. } else {
  257. identifier = this.options.resource
  258. .map(r => `${requestShortener.shorten(r)}/`)
  259. .join(" ");
  260. }
  261. if (this.options.resourceQuery) {
  262. identifier += ` ${this.options.resourceQuery}`;
  263. }
  264. if (this.options.mode) {
  265. identifier += ` ${this.options.mode}`;
  266. }
  267. if (!this.options.recursive) {
  268. identifier += " nonrecursive";
  269. }
  270. if (this.options.addon) {
  271. identifier += ` ${requestShortener.shorten(this.options.addon)}`;
  272. }
  273. if (this.options.regExp) {
  274. identifier += ` ${this._prettyRegExp(this.options.regExp)}`;
  275. }
  276. if (this.options.include) {
  277. identifier += ` include: ${this._prettyRegExp(this.options.include)}`;
  278. }
  279. if (this.options.exclude) {
  280. identifier += ` exclude: ${this._prettyRegExp(this.options.exclude)}`;
  281. }
  282. if (this.options.referencedExports) {
  283. identifier += ` referencedExports: ${this.options.referencedExports
  284. .map(e => e.join("."))
  285. .join(", ")}`;
  286. }
  287. if (this.options.chunkName) {
  288. identifier += ` chunkName: ${this.options.chunkName}`;
  289. }
  290. if (this.options.groupOptions) {
  291. const groupOptions = this.options.groupOptions;
  292. for (const key of Object.keys(groupOptions)) {
  293. identifier += ` ${key}: ${
  294. groupOptions[/** @type {keyof RawChunkGroupOptions} */ (key)]
  295. }`;
  296. }
  297. }
  298. if (this.options.namespaceObject === "strict") {
  299. identifier += " strict namespace object";
  300. } else if (this.options.namespaceObject) {
  301. identifier += " namespace object";
  302. }
  303. return identifier;
  304. }
  305. /**
  306. * @param {LibIdentOptions} options options
  307. * @returns {string | null} an identifier for library inclusion
  308. */
  309. libIdent(options) {
  310. let identifier;
  311. if (this.context) {
  312. identifier = contextify(
  313. options.context,
  314. this.context,
  315. options.associatedObjectForCache
  316. );
  317. } else if (typeof this.options.resource === "string") {
  318. identifier = contextify(
  319. options.context,
  320. this.options.resource,
  321. options.associatedObjectForCache
  322. );
  323. } else if (this.options.resource === false) {
  324. identifier = "false";
  325. } else {
  326. identifier = this.options.resource
  327. .map(res =>
  328. contextify(options.context, res, options.associatedObjectForCache)
  329. )
  330. .join(" ");
  331. }
  332. if (this.layer) identifier = `(${this.layer})/${identifier}`;
  333. if (this.options.mode) {
  334. identifier += ` ${this.options.mode}`;
  335. }
  336. if (this.options.recursive) {
  337. identifier += " recursive";
  338. }
  339. if (this.options.addon) {
  340. identifier += ` ${contextify(
  341. options.context,
  342. this.options.addon,
  343. options.associatedObjectForCache
  344. )}`;
  345. }
  346. if (this.options.regExp) {
  347. identifier += ` ${this._prettyRegExp(this.options.regExp)}`;
  348. }
  349. if (this.options.include) {
  350. identifier += ` include: ${this._prettyRegExp(this.options.include)}`;
  351. }
  352. if (this.options.exclude) {
  353. identifier += ` exclude: ${this._prettyRegExp(this.options.exclude)}`;
  354. }
  355. if (this.options.referencedExports) {
  356. identifier += ` referencedExports: ${this.options.referencedExports
  357. .map(e => e.join("."))
  358. .join(", ")}`;
  359. }
  360. return identifier;
  361. }
  362. /**
  363. * @returns {void}
  364. */
  365. invalidateBuild() {
  366. this._forceBuild = true;
  367. }
  368. /**
  369. * @param {NeedBuildContext} context context info
  370. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  371. * @returns {void}
  372. */
  373. needBuild({ fileSystemInfo }, callback) {
  374. // build if enforced
  375. if (this._forceBuild) return callback(null, true);
  376. const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
  377. // always build when we have no snapshot and context
  378. if (!buildInfo.snapshot)
  379. return callback(null, Boolean(this.context || this.options.resource));
  380. fileSystemInfo.checkSnapshotValid(buildInfo.snapshot, (err, valid) => {
  381. callback(err, !valid);
  382. });
  383. }
  384. /**
  385. * @param {WebpackOptions} options webpack options
  386. * @param {Compilation} compilation the compilation
  387. * @param {ResolverWithOptions} resolver the resolver
  388. * @param {InputFileSystem} fs the file system
  389. * @param {function(WebpackError=): void} callback callback function
  390. * @returns {void}
  391. */
  392. build(options, compilation, resolver, fs, callback) {
  393. this._forceBuild = false;
  394. /** @type {BuildMeta} */
  395. this.buildMeta = {
  396. exportsType: "default",
  397. defaultObject: "redirect-warn"
  398. };
  399. this.buildInfo = {
  400. snapshot: undefined
  401. };
  402. this.dependencies.length = 0;
  403. this.blocks.length = 0;
  404. const startTime = Date.now();
  405. /** @type {ResolveDependencies} */
  406. (this.resolveDependencies)(fs, this.options, (err, dependencies) => {
  407. if (err) {
  408. return callback(
  409. makeWebpackError(err, "ContextModule.resolveDependencies")
  410. );
  411. }
  412. // abort if something failed
  413. // this will create an empty context
  414. if (!dependencies) {
  415. callback();
  416. return;
  417. }
  418. // enhance dependencies with meta info
  419. for (const dep of dependencies) {
  420. dep.loc = {
  421. name: dep.userRequest
  422. };
  423. dep.request = this.options.addon + dep.request;
  424. }
  425. dependencies.sort(
  426. concatComparators(
  427. compareSelect(a => a.loc, compareLocations),
  428. keepOriginalOrder(this.dependencies)
  429. )
  430. );
  431. if (this.options.mode === "sync" || this.options.mode === "eager") {
  432. // if we have an sync or eager context
  433. // just add all dependencies and continue
  434. this.dependencies = dependencies;
  435. } else if (this.options.mode === "lazy-once") {
  436. // for the lazy-once mode create a new async dependency block
  437. // and add that block to this context
  438. if (dependencies.length > 0) {
  439. const block = new AsyncDependenciesBlock({
  440. ...this.options.groupOptions,
  441. name: this.options.chunkName
  442. });
  443. for (const dep of dependencies) {
  444. block.addDependency(dep);
  445. }
  446. this.addBlock(block);
  447. }
  448. } else if (
  449. this.options.mode === "weak" ||
  450. this.options.mode === "async-weak"
  451. ) {
  452. // we mark all dependencies as weak
  453. for (const dep of dependencies) {
  454. dep.weak = true;
  455. }
  456. this.dependencies = dependencies;
  457. } else if (this.options.mode === "lazy") {
  458. // if we are lazy create a new async dependency block per dependency
  459. // and add all blocks to this context
  460. let index = 0;
  461. for (const dep of dependencies) {
  462. let chunkName = this.options.chunkName;
  463. if (chunkName) {
  464. if (!/\[(index|request)\]/.test(chunkName)) {
  465. chunkName += "[index]";
  466. }
  467. chunkName = chunkName.replace(/\[index\]/g, `${index++}`);
  468. chunkName = chunkName.replace(
  469. /\[request\]/g,
  470. Template.toPath(dep.userRequest)
  471. );
  472. }
  473. const block = new AsyncDependenciesBlock(
  474. {
  475. ...this.options.groupOptions,
  476. name: chunkName
  477. },
  478. dep.loc,
  479. dep.userRequest
  480. );
  481. block.addDependency(dep);
  482. this.addBlock(block);
  483. }
  484. } else {
  485. callback(
  486. new WebpackError(`Unsupported mode "${this.options.mode}" in context`)
  487. );
  488. return;
  489. }
  490. if (!this.context && !this.options.resource) return callback();
  491. compilation.fileSystemInfo.createSnapshot(
  492. startTime,
  493. null,
  494. this.context
  495. ? [this.context]
  496. : typeof this.options.resource === "string"
  497. ? [this.options.resource]
  498. : /** @type {string[]} */ (this.options.resource),
  499. null,
  500. SNAPSHOT_OPTIONS,
  501. (err, snapshot) => {
  502. if (err) return callback(err);
  503. /** @type {BuildInfo} */
  504. (this.buildInfo).snapshot = snapshot;
  505. callback();
  506. }
  507. );
  508. });
  509. }
  510. /**
  511. * @param {LazySet<string>} fileDependencies set where file dependencies are added to
  512. * @param {LazySet<string>} contextDependencies set where context dependencies are added to
  513. * @param {LazySet<string>} missingDependencies set where missing dependencies are added to
  514. * @param {LazySet<string>} buildDependencies set where build dependencies are added to
  515. */
  516. addCacheDependencies(
  517. fileDependencies,
  518. contextDependencies,
  519. missingDependencies,
  520. buildDependencies
  521. ) {
  522. if (this.context) {
  523. contextDependencies.add(this.context);
  524. } else if (typeof this.options.resource === "string") {
  525. contextDependencies.add(this.options.resource);
  526. } else if (this.options.resource === false) {
  527. // Do nothing
  528. } else {
  529. for (const res of this.options.resource) contextDependencies.add(res);
  530. }
  531. }
  532. /**
  533. * @param {Dependency[]} dependencies all dependencies
  534. * @param {ChunkGraph} chunkGraph chunk graph
  535. * @returns {Map<string, string | number>} map with user requests
  536. */
  537. getUserRequestMap(dependencies, chunkGraph) {
  538. const moduleGraph = chunkGraph.moduleGraph;
  539. // if we filter first we get a new array
  540. // therefore we don't need to create a clone of dependencies explicitly
  541. // therefore the order of this is !important!
  542. const sortedDependencies =
  543. /** @type {ContextElementDependency[]} */
  544. (dependencies)
  545. .filter(dependency => moduleGraph.getModule(dependency))
  546. .sort((a, b) => {
  547. if (a.userRequest === b.userRequest) {
  548. return 0;
  549. }
  550. return a.userRequest < b.userRequest ? -1 : 1;
  551. });
  552. const map = Object.create(null);
  553. for (const dep of sortedDependencies) {
  554. const module = /** @type {Module} */ (moduleGraph.getModule(dep));
  555. map[dep.userRequest] = chunkGraph.getModuleId(module);
  556. }
  557. return map;
  558. }
  559. /**
  560. * @param {Dependency[]} dependencies all dependencies
  561. * @param {ChunkGraph} chunkGraph chunk graph
  562. * @returns {FakeMap | FakeMapType} fake map
  563. */
  564. getFakeMap(dependencies, chunkGraph) {
  565. if (!this.options.namespaceObject) {
  566. return 9;
  567. }
  568. const moduleGraph = chunkGraph.moduleGraph;
  569. // bitfield
  570. let hasType = 0;
  571. const comparator = compareModulesById(chunkGraph);
  572. // if we filter first we get a new array
  573. // therefore we don't need to create a clone of dependencies explicitly
  574. // therefore the order of this is !important!
  575. const sortedModules = dependencies
  576. .map(
  577. dependency => /** @type {Module} */ (moduleGraph.getModule(dependency))
  578. )
  579. .filter(Boolean)
  580. .sort(comparator);
  581. /** @type {FakeMap} */
  582. const fakeMap = Object.create(null);
  583. for (const module of sortedModules) {
  584. const exportsType = module.getExportsType(
  585. moduleGraph,
  586. this.options.namespaceObject === "strict"
  587. );
  588. const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
  589. switch (exportsType) {
  590. case "namespace":
  591. fakeMap[id] = 9;
  592. hasType |= 1;
  593. break;
  594. case "dynamic":
  595. fakeMap[id] = 7;
  596. hasType |= 2;
  597. break;
  598. case "default-only":
  599. fakeMap[id] = 1;
  600. hasType |= 4;
  601. break;
  602. case "default-with-named":
  603. fakeMap[id] = 3;
  604. hasType |= 8;
  605. break;
  606. default:
  607. throw new Error(`Unexpected exports type ${exportsType}`);
  608. }
  609. }
  610. if (hasType === 1) {
  611. return 9;
  612. }
  613. if (hasType === 2) {
  614. return 7;
  615. }
  616. if (hasType === 4) {
  617. return 1;
  618. }
  619. if (hasType === 8) {
  620. return 3;
  621. }
  622. if (hasType === 0) {
  623. return 9;
  624. }
  625. return fakeMap;
  626. }
  627. /**
  628. * @param {FakeMap | FakeMapType} fakeMap fake map
  629. * @returns {string} fake map init statement
  630. */
  631. getFakeMapInitStatement(fakeMap) {
  632. return typeof fakeMap === "object"
  633. ? `var fakeMap = ${JSON.stringify(fakeMap, null, "\t")};`
  634. : "";
  635. }
  636. /**
  637. * @param {FakeMapType} type type
  638. * @param {boolean=} asyncModule is async module
  639. * @returns {string} return result
  640. */
  641. getReturn(type, asyncModule) {
  642. if (type === 9) {
  643. return `${RuntimeGlobals.require}(id)`;
  644. }
  645. return `${RuntimeGlobals.createFakeNamespaceObject}(id, ${type}${
  646. asyncModule ? " | 16" : ""
  647. })`;
  648. }
  649. /**
  650. * @param {FakeMap | FakeMapType} fakeMap fake map
  651. * @param {boolean=} asyncModule us async module
  652. * @param {string=} fakeMapDataExpression fake map data expression
  653. * @returns {string} module object source
  654. */
  655. getReturnModuleObjectSource(
  656. fakeMap,
  657. asyncModule,
  658. fakeMapDataExpression = "fakeMap[id]"
  659. ) {
  660. if (typeof fakeMap === "number") {
  661. return `return ${this.getReturn(fakeMap, asyncModule)};`;
  662. }
  663. return `return ${
  664. RuntimeGlobals.createFakeNamespaceObject
  665. }(id, ${fakeMapDataExpression}${asyncModule ? " | 16" : ""})`;
  666. }
  667. /**
  668. * @param {Dependency[]} dependencies dependencies
  669. * @param {ModuleId} id module id
  670. * @param {ChunkGraph} chunkGraph the chunk graph
  671. * @returns {string} source code
  672. */
  673. getSyncSource(dependencies, id, chunkGraph) {
  674. const map = this.getUserRequestMap(dependencies, chunkGraph);
  675. const fakeMap = this.getFakeMap(dependencies, chunkGraph);
  676. const returnModuleObject = this.getReturnModuleObjectSource(fakeMap);
  677. return `var map = ${JSON.stringify(map, null, "\t")};
  678. ${this.getFakeMapInitStatement(fakeMap)}
  679. function webpackContext(req) {
  680. var id = webpackContextResolve(req);
  681. ${returnModuleObject}
  682. }
  683. function webpackContextResolve(req) {
  684. if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
  685. var e = new Error("Cannot find module '" + req + "'");
  686. e.code = 'MODULE_NOT_FOUND';
  687. throw e;
  688. }
  689. return map[req];
  690. }
  691. webpackContext.keys = function webpackContextKeys() {
  692. return Object.keys(map);
  693. };
  694. webpackContext.resolve = webpackContextResolve;
  695. module.exports = webpackContext;
  696. webpackContext.id = ${JSON.stringify(id)};`;
  697. }
  698. /**
  699. * @param {Dependency[]} dependencies dependencies
  700. * @param {ModuleId} id module id
  701. * @param {ChunkGraph} chunkGraph the chunk graph
  702. * @returns {string} source code
  703. */
  704. getWeakSyncSource(dependencies, id, chunkGraph) {
  705. const map = this.getUserRequestMap(dependencies, chunkGraph);
  706. const fakeMap = this.getFakeMap(dependencies, chunkGraph);
  707. const returnModuleObject = this.getReturnModuleObjectSource(fakeMap);
  708. return `var map = ${JSON.stringify(map, null, "\t")};
  709. ${this.getFakeMapInitStatement(fakeMap)}
  710. function webpackContext(req) {
  711. var id = webpackContextResolve(req);
  712. if(!${RuntimeGlobals.moduleFactories}[id]) {
  713. var e = new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
  714. e.code = 'MODULE_NOT_FOUND';
  715. throw e;
  716. }
  717. ${returnModuleObject}
  718. }
  719. function webpackContextResolve(req) {
  720. if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
  721. var e = new Error("Cannot find module '" + req + "'");
  722. e.code = 'MODULE_NOT_FOUND';
  723. throw e;
  724. }
  725. return map[req];
  726. }
  727. webpackContext.keys = function webpackContextKeys() {
  728. return Object.keys(map);
  729. };
  730. webpackContext.resolve = webpackContextResolve;
  731. webpackContext.id = ${JSON.stringify(id)};
  732. module.exports = webpackContext;`;
  733. }
  734. /**
  735. * @param {Dependency[]} dependencies dependencies
  736. * @param {ModuleId} id module id
  737. * @param {object} context context
  738. * @param {ChunkGraph} context.chunkGraph the chunk graph
  739. * @param {RuntimeTemplate} context.runtimeTemplate the chunk graph
  740. * @returns {string} source code
  741. */
  742. getAsyncWeakSource(dependencies, id, { chunkGraph, runtimeTemplate }) {
  743. const arrow = runtimeTemplate.supportsArrowFunction();
  744. const map = this.getUserRequestMap(dependencies, chunkGraph);
  745. const fakeMap = this.getFakeMap(dependencies, chunkGraph);
  746. const returnModuleObject = this.getReturnModuleObjectSource(fakeMap, true);
  747. return `var map = ${JSON.stringify(map, null, "\t")};
  748. ${this.getFakeMapInitStatement(fakeMap)}
  749. function webpackAsyncContext(req) {
  750. return webpackAsyncContextResolve(req).then(${
  751. arrow ? "id =>" : "function(id)"
  752. } {
  753. if(!${RuntimeGlobals.moduleFactories}[id]) {
  754. var e = new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
  755. e.code = 'MODULE_NOT_FOUND';
  756. throw e;
  757. }
  758. ${returnModuleObject}
  759. });
  760. }
  761. function webpackAsyncContextResolve(req) {
  762. // Here Promise.resolve().then() is used instead of new Promise() to prevent
  763. // uncaught exception popping up in devtools
  764. return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
  765. if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
  766. var e = new Error("Cannot find module '" + req + "'");
  767. e.code = 'MODULE_NOT_FOUND';
  768. throw e;
  769. }
  770. return map[req];
  771. });
  772. }
  773. webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
  774. "Object.keys(map)"
  775. )};
  776. webpackAsyncContext.resolve = webpackAsyncContextResolve;
  777. webpackAsyncContext.id = ${JSON.stringify(id)};
  778. module.exports = webpackAsyncContext;`;
  779. }
  780. /**
  781. * @param {Dependency[]} dependencies dependencies
  782. * @param {ModuleId} id module id
  783. * @param {object} context context
  784. * @param {ChunkGraph} context.chunkGraph the chunk graph
  785. * @param {RuntimeTemplate} context.runtimeTemplate the chunk graph
  786. * @returns {string} source code
  787. */
  788. getEagerSource(dependencies, id, { chunkGraph, runtimeTemplate }) {
  789. const arrow = runtimeTemplate.supportsArrowFunction();
  790. const map = this.getUserRequestMap(dependencies, chunkGraph);
  791. const fakeMap = this.getFakeMap(dependencies, chunkGraph);
  792. const thenFunction =
  793. fakeMap !== 9
  794. ? `${arrow ? "id =>" : "function(id)"} {
  795. ${this.getReturnModuleObjectSource(fakeMap, true)}
  796. }`
  797. : RuntimeGlobals.require;
  798. return `var map = ${JSON.stringify(map, null, "\t")};
  799. ${this.getFakeMapInitStatement(fakeMap)}
  800. function webpackAsyncContext(req) {
  801. return webpackAsyncContextResolve(req).then(${thenFunction});
  802. }
  803. function webpackAsyncContextResolve(req) {
  804. // Here Promise.resolve().then() is used instead of new Promise() to prevent
  805. // uncaught exception popping up in devtools
  806. return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
  807. if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
  808. var e = new Error("Cannot find module '" + req + "'");
  809. e.code = 'MODULE_NOT_FOUND';
  810. throw e;
  811. }
  812. return map[req];
  813. });
  814. }
  815. webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
  816. "Object.keys(map)"
  817. )};
  818. webpackAsyncContext.resolve = webpackAsyncContextResolve;
  819. webpackAsyncContext.id = ${JSON.stringify(id)};
  820. module.exports = webpackAsyncContext;`;
  821. }
  822. /**
  823. * @param {AsyncDependenciesBlock} block block
  824. * @param {Dependency[]} dependencies dependencies
  825. * @param {ModuleId} id module id
  826. * @param {object} options options object
  827. * @param {RuntimeTemplate} options.runtimeTemplate the runtime template
  828. * @param {ChunkGraph} options.chunkGraph the chunk graph
  829. * @returns {string} source code
  830. */
  831. getLazyOnceSource(block, dependencies, id, { runtimeTemplate, chunkGraph }) {
  832. const promise = runtimeTemplate.blockPromise({
  833. chunkGraph,
  834. block,
  835. message: "lazy-once context",
  836. runtimeRequirements: new Set()
  837. });
  838. const arrow = runtimeTemplate.supportsArrowFunction();
  839. const map = this.getUserRequestMap(dependencies, chunkGraph);
  840. const fakeMap = this.getFakeMap(dependencies, chunkGraph);
  841. const thenFunction =
  842. fakeMap !== 9
  843. ? `${arrow ? "id =>" : "function(id)"} {
  844. ${this.getReturnModuleObjectSource(fakeMap, true)};
  845. }`
  846. : RuntimeGlobals.require;
  847. return `var map = ${JSON.stringify(map, null, "\t")};
  848. ${this.getFakeMapInitStatement(fakeMap)}
  849. function webpackAsyncContext(req) {
  850. return webpackAsyncContextResolve(req).then(${thenFunction});
  851. }
  852. function webpackAsyncContextResolve(req) {
  853. return ${promise}.then(${arrow ? "() =>" : "function()"} {
  854. if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
  855. var e = new Error("Cannot find module '" + req + "'");
  856. e.code = 'MODULE_NOT_FOUND';
  857. throw e;
  858. }
  859. return map[req];
  860. });
  861. }
  862. webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
  863. "Object.keys(map)"
  864. )};
  865. webpackAsyncContext.resolve = webpackAsyncContextResolve;
  866. webpackAsyncContext.id = ${JSON.stringify(id)};
  867. module.exports = webpackAsyncContext;`;
  868. }
  869. /**
  870. * @param {AsyncDependenciesBlock[]} blocks blocks
  871. * @param {ModuleId} id module id
  872. * @param {object} context context
  873. * @param {ChunkGraph} context.chunkGraph the chunk graph
  874. * @param {RuntimeTemplate} context.runtimeTemplate the chunk graph
  875. * @returns {string} source code
  876. */
  877. getLazySource(blocks, id, { chunkGraph, runtimeTemplate }) {
  878. const moduleGraph = chunkGraph.moduleGraph;
  879. const arrow = runtimeTemplate.supportsArrowFunction();
  880. let hasMultipleOrNoChunks = false;
  881. let hasNoChunk = true;
  882. const fakeMap = this.getFakeMap(
  883. blocks.map(b => b.dependencies[0]),
  884. chunkGraph
  885. );
  886. const hasFakeMap = typeof fakeMap === "object";
  887. /** @typedef {{userRequest: string, dependency: ContextElementDependency, chunks: undefined | Chunk[], module: Module, block: AsyncDependenciesBlock}} Item */
  888. /**
  889. * @type {Item[]}
  890. */
  891. const items = blocks
  892. .map(block => {
  893. const dependency =
  894. /** @type {ContextElementDependency} */
  895. (block.dependencies[0]);
  896. return {
  897. dependency,
  898. module: /** @type {Module} */ (moduleGraph.getModule(dependency)),
  899. block,
  900. userRequest: dependency.userRequest,
  901. chunks: undefined
  902. };
  903. })
  904. .filter(item => item.module);
  905. for (const item of items) {
  906. const chunkGroup = chunkGraph.getBlockChunkGroup(item.block);
  907. const chunks = (chunkGroup && chunkGroup.chunks) || [];
  908. item.chunks = chunks;
  909. if (chunks.length > 0) {
  910. hasNoChunk = false;
  911. }
  912. if (chunks.length !== 1) {
  913. hasMultipleOrNoChunks = true;
  914. }
  915. }
  916. const shortMode = hasNoChunk && !hasFakeMap;
  917. const sortedItems = items.sort((a, b) => {
  918. if (a.userRequest === b.userRequest) return 0;
  919. return a.userRequest < b.userRequest ? -1 : 1;
  920. });
  921. /** @type {Record<string, ModuleId | (ModuleId[] | ChunkId[])>} */
  922. const map = Object.create(null);
  923. for (const item of sortedItems) {
  924. const moduleId =
  925. /** @type {ModuleId} */
  926. (chunkGraph.getModuleId(item.module));
  927. if (shortMode) {
  928. map[item.userRequest] = moduleId;
  929. } else {
  930. /** @type {(ModuleId | ChunkId)[]} */
  931. const arrayStart = [moduleId];
  932. if (hasFakeMap) {
  933. arrayStart.push(fakeMap[moduleId]);
  934. }
  935. map[item.userRequest] = arrayStart.concat(
  936. /** @type {Chunk[]} */
  937. (item.chunks).map(chunk => /** @type {ChunkId} */ (chunk.id))
  938. );
  939. }
  940. }
  941. const chunksStartPosition = hasFakeMap ? 2 : 1;
  942. const requestPrefix = hasNoChunk
  943. ? "Promise.resolve()"
  944. : hasMultipleOrNoChunks
  945. ? `Promise.all(ids.slice(${chunksStartPosition}).map(${RuntimeGlobals.ensureChunk}))`
  946. : `${RuntimeGlobals.ensureChunk}(ids[${chunksStartPosition}])`;
  947. const returnModuleObject = this.getReturnModuleObjectSource(
  948. fakeMap,
  949. true,
  950. shortMode ? "invalid" : "ids[1]"
  951. );
  952. const webpackAsyncContext =
  953. requestPrefix === "Promise.resolve()"
  954. ? `
  955. function webpackAsyncContext(req) {
  956. return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
  957. if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
  958. var e = new Error("Cannot find module '" + req + "'");
  959. e.code = 'MODULE_NOT_FOUND';
  960. throw e;
  961. }
  962. ${shortMode ? "var id = map[req];" : "var ids = map[req], id = ids[0];"}
  963. ${returnModuleObject}
  964. });
  965. }`
  966. : `function webpackAsyncContext(req) {
  967. if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
  968. return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
  969. var e = new Error("Cannot find module '" + req + "'");
  970. e.code = 'MODULE_NOT_FOUND';
  971. throw e;
  972. });
  973. }
  974. var ids = map[req], id = ids[0];
  975. return ${requestPrefix}.then(${arrow ? "() =>" : "function()"} {
  976. ${returnModuleObject}
  977. });
  978. }`;
  979. return `var map = ${JSON.stringify(map, null, "\t")};
  980. ${webpackAsyncContext}
  981. webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
  982. "Object.keys(map)"
  983. )};
  984. webpackAsyncContext.id = ${JSON.stringify(id)};
  985. module.exports = webpackAsyncContext;`;
  986. }
  987. /**
  988. * @param {ModuleId} id module id
  989. * @param {RuntimeTemplate} runtimeTemplate runtime template
  990. * @returns {string} source for empty async context
  991. */
  992. getSourceForEmptyContext(id, runtimeTemplate) {
  993. return `function webpackEmptyContext(req) {
  994. var e = new Error("Cannot find module '" + req + "'");
  995. e.code = 'MODULE_NOT_FOUND';
  996. throw e;
  997. }
  998. webpackEmptyContext.keys = ${runtimeTemplate.returningFunction("[]")};
  999. webpackEmptyContext.resolve = webpackEmptyContext;
  1000. webpackEmptyContext.id = ${JSON.stringify(id)};
  1001. module.exports = webpackEmptyContext;`;
  1002. }
  1003. /**
  1004. * @param {ModuleId} id module id
  1005. * @param {RuntimeTemplate} runtimeTemplate runtime template
  1006. * @returns {string} source for empty async context
  1007. */
  1008. getSourceForEmptyAsyncContext(id, runtimeTemplate) {
  1009. const arrow = runtimeTemplate.supportsArrowFunction();
  1010. return `function webpackEmptyAsyncContext(req) {
  1011. // Here Promise.resolve().then() is used instead of new Promise() to prevent
  1012. // uncaught exception popping up in devtools
  1013. return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
  1014. var e = new Error("Cannot find module '" + req + "'");
  1015. e.code = 'MODULE_NOT_FOUND';
  1016. throw e;
  1017. });
  1018. }
  1019. webpackEmptyAsyncContext.keys = ${runtimeTemplate.returningFunction("[]")};
  1020. webpackEmptyAsyncContext.resolve = webpackEmptyAsyncContext;
  1021. webpackEmptyAsyncContext.id = ${JSON.stringify(id)};
  1022. module.exports = webpackEmptyAsyncContext;`;
  1023. }
  1024. /**
  1025. * @param {string} asyncMode module mode
  1026. * @param {CodeGenerationContext} context context info
  1027. * @returns {string} the source code
  1028. */
  1029. getSourceString(asyncMode, { runtimeTemplate, chunkGraph }) {
  1030. const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(this));
  1031. if (asyncMode === "lazy") {
  1032. if (this.blocks && this.blocks.length > 0) {
  1033. return this.getLazySource(this.blocks, id, {
  1034. runtimeTemplate,
  1035. chunkGraph
  1036. });
  1037. }
  1038. return this.getSourceForEmptyAsyncContext(id, runtimeTemplate);
  1039. }
  1040. if (asyncMode === "eager") {
  1041. if (this.dependencies && this.dependencies.length > 0) {
  1042. return this.getEagerSource(this.dependencies, id, {
  1043. chunkGraph,
  1044. runtimeTemplate
  1045. });
  1046. }
  1047. return this.getSourceForEmptyAsyncContext(id, runtimeTemplate);
  1048. }
  1049. if (asyncMode === "lazy-once") {
  1050. const block = this.blocks[0];
  1051. if (block) {
  1052. return this.getLazyOnceSource(block, block.dependencies, id, {
  1053. runtimeTemplate,
  1054. chunkGraph
  1055. });
  1056. }
  1057. return this.getSourceForEmptyAsyncContext(id, runtimeTemplate);
  1058. }
  1059. if (asyncMode === "async-weak") {
  1060. if (this.dependencies && this.dependencies.length > 0) {
  1061. return this.getAsyncWeakSource(this.dependencies, id, {
  1062. chunkGraph,
  1063. runtimeTemplate
  1064. });
  1065. }
  1066. return this.getSourceForEmptyAsyncContext(id, runtimeTemplate);
  1067. }
  1068. if (
  1069. asyncMode === "weak" &&
  1070. this.dependencies &&
  1071. this.dependencies.length > 0
  1072. ) {
  1073. return this.getWeakSyncSource(this.dependencies, id, chunkGraph);
  1074. }
  1075. if (this.dependencies && this.dependencies.length > 0) {
  1076. return this.getSyncSource(this.dependencies, id, chunkGraph);
  1077. }
  1078. return this.getSourceForEmptyContext(id, runtimeTemplate);
  1079. }
  1080. /**
  1081. * @param {string} sourceString source content
  1082. * @param {Compilation=} compilation the compilation
  1083. * @returns {Source} generated source
  1084. */
  1085. getSource(sourceString, compilation) {
  1086. if (this.useSourceMap || this.useSimpleSourceMap) {
  1087. return new OriginalSource(
  1088. sourceString,
  1089. `webpack://${makePathsRelative(
  1090. (compilation && compilation.compiler.context) || "",
  1091. this.identifier(),
  1092. compilation && compilation.compiler.root
  1093. )}`
  1094. );
  1095. }
  1096. return new RawSource(sourceString);
  1097. }
  1098. /**
  1099. * @param {CodeGenerationContext} context context for code generation
  1100. * @returns {CodeGenerationResult} result
  1101. */
  1102. codeGeneration(context) {
  1103. const { chunkGraph, compilation } = context;
  1104. const sources = new Map();
  1105. sources.set(
  1106. "javascript",
  1107. this.getSource(
  1108. this.getSourceString(this.options.mode, context),
  1109. compilation
  1110. )
  1111. );
  1112. const set = new Set();
  1113. const allDeps =
  1114. this.dependencies.length > 0
  1115. ? /** @type {ContextElementDependency[]} */ (this.dependencies).slice()
  1116. : [];
  1117. for (const block of this.blocks)
  1118. for (const dep of block.dependencies)
  1119. allDeps.push(/** @type {ContextElementDependency} */ (dep));
  1120. set.add(RuntimeGlobals.module);
  1121. set.add(RuntimeGlobals.hasOwnProperty);
  1122. if (allDeps.length > 0) {
  1123. const asyncMode = this.options.mode;
  1124. set.add(RuntimeGlobals.require);
  1125. if (asyncMode === "weak") {
  1126. set.add(RuntimeGlobals.moduleFactories);
  1127. } else if (asyncMode === "async-weak") {
  1128. set.add(RuntimeGlobals.moduleFactories);
  1129. set.add(RuntimeGlobals.ensureChunk);
  1130. } else if (asyncMode === "lazy" || asyncMode === "lazy-once") {
  1131. set.add(RuntimeGlobals.ensureChunk);
  1132. }
  1133. if (this.getFakeMap(allDeps, chunkGraph) !== 9) {
  1134. set.add(RuntimeGlobals.createFakeNamespaceObject);
  1135. }
  1136. }
  1137. return {
  1138. sources,
  1139. runtimeRequirements: set
  1140. };
  1141. }
  1142. /**
  1143. * @param {string=} type the source type for which the size should be estimated
  1144. * @returns {number} the estimated size of the module (must be non-zero)
  1145. */
  1146. size(type) {
  1147. // base penalty
  1148. let size = 160;
  1149. // if we don't have dependencies we stop here.
  1150. for (const dependency of this.dependencies) {
  1151. const element = /** @type {ContextElementDependency} */ (dependency);
  1152. size += 5 + element.userRequest.length;
  1153. }
  1154. return size;
  1155. }
  1156. /**
  1157. * @param {ObjectSerializerContext} context context
  1158. */
  1159. serialize(context) {
  1160. const { write } = context;
  1161. write(this._identifier);
  1162. write(this._forceBuild);
  1163. super.serialize(context);
  1164. }
  1165. /**
  1166. * @param {ObjectDeserializerContext} context context
  1167. */
  1168. deserialize(context) {
  1169. const { read } = context;
  1170. this._identifier = read();
  1171. this._forceBuild = read();
  1172. super.deserialize(context);
  1173. }
  1174. }
  1175. makeSerializable(ContextModule, "webpack/lib/ContextModule");
  1176. module.exports = ContextModule;