AMDRequireDependency.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const NullDependency = require("./NullDependency");
  9. /** @import { ReplaceSource } from "webpack-sources" */
  10. /** @import AsyncDependenciesBlock from "../AsyncDependenciesBlock" */
  11. /** @import Dependency from "../Dependency" */
  12. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  13. /** @import { Range } from "../javascript/JavascriptParser" */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range, Range | null, Range | null, boolean, boolean]>} ObjectDeserializerContext */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range, Range | null, Range | null, boolean, boolean]>} ObjectSerializerContext */
  16. class AMDRequireDependency extends NullDependency {
  17. /**
  18. * Creates an instance of AMDRequireDependency.
  19. * @param {Range} outerRange outer range
  20. * @param {Range} arrayRange array range
  21. * @param {Range | null} functionRange function range
  22. * @param {Range | null} errorCallbackRange error callback range
  23. */
  24. constructor(outerRange, arrayRange, functionRange, errorCallbackRange) {
  25. super();
  26. this.outerRange = outerRange;
  27. this.arrayRange = arrayRange;
  28. this.functionRange = functionRange;
  29. this.errorCallbackRange = errorCallbackRange;
  30. /** @type {boolean} */
  31. this.functionBindThis = false;
  32. /** @type {boolean} */
  33. this.errorCallbackBindThis = false;
  34. }
  35. get category() {
  36. return "amd";
  37. }
  38. /**
  39. * Serializes this instance into the provided serializer context.
  40. * @param {ObjectSerializerContext} context context
  41. */
  42. serialize(context) {
  43. context
  44. .write(this.outerRange)
  45. .write(this.arrayRange)
  46. .write(this.functionRange)
  47. .write(this.errorCallbackRange)
  48. .write(this.functionBindThis)
  49. .write(this.errorCallbackBindThis);
  50. super.serialize(context);
  51. }
  52. /**
  53. * Restores this instance from the provided deserializer context.
  54. * @param {ObjectDeserializerContext} context context
  55. */
  56. deserialize(context) {
  57. this.outerRange = context.read();
  58. const c1 = context.rest;
  59. this.arrayRange = c1.read();
  60. const c2 = c1.rest;
  61. this.functionRange = c2.read();
  62. const c3 = c2.rest;
  63. this.errorCallbackRange = c3.read();
  64. const c4 = c3.rest;
  65. this.functionBindThis = c4.read();
  66. const c5 = c4.rest;
  67. this.errorCallbackBindThis = c5.read();
  68. super.deserialize(c5.rest);
  69. }
  70. }
  71. makeSerializable(
  72. AMDRequireDependency,
  73. "webpack/lib/dependencies/AMDRequireDependency"
  74. );
  75. AMDRequireDependency.Template = class AMDRequireDependencyTemplate extends (
  76. NullDependency.Template
  77. ) {
  78. /**
  79. * Applies the plugin by registering its hooks on the compiler.
  80. * @param {Dependency} dependency the dependency for which the template should be applied
  81. * @param {ReplaceSource} source the current replace source which can be modified
  82. * @param {DependencyTemplateContext} templateContext the context object
  83. * @returns {void}
  84. */
  85. apply(
  86. dependency,
  87. source,
  88. { module, runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
  89. ) {
  90. const dep = /** @type {AMDRequireDependency} */ (dependency);
  91. const depBlock = /** @type {AsyncDependenciesBlock} */ (
  92. moduleGraph.getParentBlock(dep)
  93. );
  94. const promise = runtimeTemplate.blockPromise({
  95. chunkGraph,
  96. block: depBlock,
  97. message: "AMD require",
  98. runtimeRequirements,
  99. originModule: module
  100. });
  101. // has array range but no function range
  102. if (dep.arrayRange && !dep.functionRange) {
  103. const startBlock = `${promise}.then(function() {`;
  104. const endBlock = `;})['catch'](${RuntimeGlobals.uncaughtErrorHandler})`;
  105. runtimeRequirements.add(RuntimeGlobals.uncaughtErrorHandler);
  106. source.replace(dep.outerRange[0], dep.arrayRange[0] - 1, startBlock);
  107. source.replace(dep.arrayRange[1], dep.outerRange[1] - 1, endBlock);
  108. return;
  109. }
  110. // has function range but no array range
  111. if (dep.functionRange && !dep.arrayRange) {
  112. const startBlock = `${promise}.then((`;
  113. const endBlock = `).bind(exports, ${RuntimeGlobals.require}, exports, module))['catch'](${RuntimeGlobals.uncaughtErrorHandler})`;
  114. runtimeRequirements.add(RuntimeGlobals.uncaughtErrorHandler);
  115. source.replace(dep.outerRange[0], dep.functionRange[0] - 1, startBlock);
  116. source.replace(dep.functionRange[1], dep.outerRange[1] - 1, endBlock);
  117. return;
  118. }
  119. // has array range, function range, and errorCallbackRange
  120. if (dep.arrayRange && dep.functionRange && dep.errorCallbackRange) {
  121. const startBlock = `${promise}.then(function() { `;
  122. const errorRangeBlock = `}${
  123. dep.functionBindThis ? ".bind(this)" : ""
  124. })['catch'](`;
  125. const endBlock = `${dep.errorCallbackBindThis ? ".bind(this)" : ""})`;
  126. source.replace(dep.outerRange[0], dep.arrayRange[0] - 1, startBlock);
  127. source.insert(dep.arrayRange[0], "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
  128. source.replace(dep.arrayRange[1], dep.functionRange[0] - 1, "; (");
  129. source.insert(
  130. dep.functionRange[1],
  131. ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__);"
  132. );
  133. source.replace(
  134. dep.functionRange[1],
  135. dep.errorCallbackRange[0] - 1,
  136. errorRangeBlock
  137. );
  138. source.replace(
  139. dep.errorCallbackRange[1],
  140. dep.outerRange[1] - 1,
  141. endBlock
  142. );
  143. return;
  144. }
  145. // has array range, function range, but no errorCallbackRange
  146. if (dep.arrayRange && dep.functionRange) {
  147. const startBlock = `${promise}.then(function() { `;
  148. const endBlock = `}${
  149. dep.functionBindThis ? ".bind(this)" : ""
  150. })['catch'](${RuntimeGlobals.uncaughtErrorHandler})`;
  151. runtimeRequirements.add(RuntimeGlobals.uncaughtErrorHandler);
  152. source.replace(dep.outerRange[0], dep.arrayRange[0] - 1, startBlock);
  153. source.insert(dep.arrayRange[0], "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
  154. source.replace(dep.arrayRange[1], dep.functionRange[0] - 1, "; (");
  155. source.insert(
  156. dep.functionRange[1],
  157. ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__);"
  158. );
  159. source.replace(dep.functionRange[1], dep.outerRange[1] - 1, endBlock);
  160. }
  161. }
  162. };
  163. module.exports = AMDRequireDependency;