AMDRequireDependency.js 5.8 KB

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