AMDRequireDependency.js 5.0 KB

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