FallbackModule.js 6.0 KB

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