AMDRequireDependency.js 5.6 KB

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