FallbackModule.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
  4. */
  5. "use strict";
  6. const { RawSource } = require("webpack-sources");
  7. const Module = require("../Module");
  8. const { WEBPACK_MODULE_TYPE_FALLBACK } = require("../ModuleTypeConstants");
  9. const RuntimeGlobals = require("../RuntimeGlobals");
  10. const Template = require("../Template");
  11. const makeSerializable = require("../util/makeSerializable");
  12. const FallbackItemDependency = require("./FallbackItemDependency");
  13. /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  14. /** @typedef {import("../Chunk")} Chunk */
  15. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  16. /** @typedef {import("../ChunkGroup")} ChunkGroup */
  17. /** @typedef {import("../Compilation")} Compilation */
  18. /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
  19. /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
  20. /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
  21. /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
  22. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  23. /** @typedef {import("../RequestShortener")} RequestShortener */
  24. /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  25. /** @typedef {import("../WebpackError")} WebpackError */
  26. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  27. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  28. /** @typedef {import("../util/Hash")} Hash */
  29. /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
  30. const TYPES = new Set(["javascript"]);
  31. const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
  32. class FallbackModule extends Module {
  33. /**
  34. * @param {string[]} requests list of requests to choose one
  35. */
  36. constructor(requests) {
  37. super(WEBPACK_MODULE_TYPE_FALLBACK);
  38. this.requests = requests;
  39. this._identifier = `fallback ${this.requests.join(" ")}`;
  40. }
  41. /**
  42. * @returns {string} a unique identifier of the module
  43. */
  44. identifier() {
  45. return this._identifier;
  46. }
  47. /**
  48. * @param {RequestShortener} requestShortener the request shortener
  49. * @returns {string} a user readable identifier of the module
  50. */
  51. readableIdentifier(requestShortener) {
  52. return this._identifier;
  53. }
  54. /**
  55. * @param {LibIdentOptions} options options
  56. * @returns {string | null} an identifier for library inclusion
  57. */
  58. libIdent(options) {
  59. return `${this.layer ? `(${this.layer})/` : ""}webpack/container/fallback/${
  60. this.requests[0]
  61. }/and ${this.requests.length - 1} more`;
  62. }
  63. /**
  64. * @param {Chunk} chunk the chunk which condition should be checked
  65. * @param {Compilation} compilation the compilation
  66. * @returns {boolean} true, if the chunk is ok for the module
  67. */
  68. chunkCondition(chunk, { chunkGraph }) {
  69. return chunkGraph.getNumberOfEntryModules(chunk) > 0;
  70. }
  71. /**
  72. * @param {NeedBuildContext} context context info
  73. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  74. * @returns {void}
  75. */
  76. needBuild(context, callback) {
  77. callback(null, !this.buildInfo);
  78. }
  79. /**
  80. * @param {WebpackOptions} options webpack options
  81. * @param {Compilation} compilation the compilation
  82. * @param {ResolverWithOptions} resolver the resolver
  83. * @param {InputFileSystem} fs the file system
  84. * @param {function(WebpackError=): void} callback callback function
  85. * @returns {void}
  86. */
  87. build(options, compilation, resolver, fs, callback) {
  88. this.buildMeta = {};
  89. this.buildInfo = {
  90. strict: true
  91. };
  92. this.clearDependenciesAndBlocks();
  93. for (const request of this.requests)
  94. this.addDependency(new FallbackItemDependency(request));
  95. callback();
  96. }
  97. /**
  98. * @param {string=} type the source type for which the size should be estimated
  99. * @returns {number} the estimated size of the module (must be non-zero)
  100. */
  101. size(type) {
  102. return this.requests.length * 5 + 42;
  103. }
  104. /**
  105. * @returns {SourceTypes} types available (do not mutate)
  106. */
  107. getSourceTypes() {
  108. return TYPES;
  109. }
  110. /**
  111. * @param {CodeGenerationContext} context context for code generation
  112. * @returns {CodeGenerationResult} result
  113. */
  114. codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
  115. const ids = this.dependencies.map(dep =>
  116. chunkGraph.getModuleId(/** @type {Module} */ (moduleGraph.getModule(dep)))
  117. );
  118. const code = Template.asString([
  119. `var ids = ${JSON.stringify(ids)};`,
  120. "var error, result, i = 0;",
  121. `var loop = ${runtimeTemplate.basicFunction("next", [
  122. "while(i < ids.length) {",
  123. Template.indent([
  124. `try { next = ${RuntimeGlobals.require}(ids[i++]); } catch(e) { return handleError(e); }`,
  125. "if(next) return next.then ? next.then(handleResult, handleError) : handleResult(next);"
  126. ]),
  127. "}",
  128. "if(error) throw error;"
  129. ])}`,
  130. `var handleResult = ${runtimeTemplate.basicFunction("result", [
  131. "if(result) return result;",
  132. "return loop();"
  133. ])};`,
  134. `var handleError = ${runtimeTemplate.basicFunction("e", [
  135. "error = e;",
  136. "return loop();"
  137. ])};`,
  138. "module.exports = loop();"
  139. ]);
  140. const sources = new Map();
  141. sources.set("javascript", new RawSource(code));
  142. return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS };
  143. }
  144. /**
  145. * @param {ObjectSerializerContext} context context
  146. */
  147. serialize(context) {
  148. const { write } = context;
  149. write(this.requests);
  150. super.serialize(context);
  151. }
  152. /**
  153. * @param {ObjectDeserializerContext} context context
  154. * @returns {FallbackModule} deserialized fallback module
  155. */
  156. static deserialize(context) {
  157. const { read } = context;
  158. const obj = new FallbackModule(read());
  159. obj.deserialize(context);
  160. return obj;
  161. }
  162. }
  163. makeSerializable(FallbackModule, "webpack/lib/container/FallbackModule");
  164. module.exports = FallbackModule;