FallbackModule.js 5.3 KB

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