ContextModule.js 38 KB

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