ContextModule.js 35 KB

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