You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.2 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NullDependency = require("./NullDependency");
  7. class AMDRequireDependency extends NullDependency {
  8. constructor(block) {
  9. super();
  10. this.block = block;
  11. }
  12. }
  13. AMDRequireDependency.Template = class AMDRequireDependencyTemplate {
  14. apply(dep, source, runtime) {
  15. const depBlock = dep.block;
  16. const promise = runtime.blockPromise({
  17. block: depBlock,
  18. message: "AMD require"
  19. });
  20. // has array range but no function range
  21. if (depBlock.arrayRange && !depBlock.functionRange) {
  22. const startBlock = `${promise}.then(function() {`;
  23. const endBlock = `;}).catch(${runtime.onError()})`;
  24. source.replace(
  25. depBlock.outerRange[0],
  26. depBlock.arrayRange[0] - 1,
  27. startBlock
  28. );
  29. source.replace(
  30. depBlock.arrayRange[1],
  31. depBlock.outerRange[1] - 1,
  32. endBlock
  33. );
  34. return;
  35. }
  36. // has function range but no array range
  37. if (depBlock.functionRange && !depBlock.arrayRange) {
  38. const startBlock = `${promise}.then((`;
  39. const endBlock = `).bind(exports, __webpack_require__, exports, module)).catch(${runtime.onError()})`;
  40. source.replace(
  41. depBlock.outerRange[0],
  42. depBlock.functionRange[0] - 1,
  43. startBlock
  44. );
  45. source.replace(
  46. depBlock.functionRange[1],
  47. depBlock.outerRange[1] - 1,
  48. endBlock
  49. );
  50. return;
  51. }
  52. // has array range, function range, and errorCallbackRange
  53. if (
  54. depBlock.arrayRange &&
  55. depBlock.functionRange &&
  56. depBlock.errorCallbackRange
  57. ) {
  58. const startBlock = `${promise}.then(function() { `;
  59. const errorRangeBlock = `}${
  60. depBlock.functionBindThis ? ".bind(this)" : ""
  61. }).catch(`;
  62. const endBlock = `${
  63. depBlock.errorCallbackBindThis ? ".bind(this)" : ""
  64. })`;
  65. source.replace(
  66. depBlock.outerRange[0],
  67. depBlock.arrayRange[0] - 1,
  68. startBlock
  69. );
  70. source.insert(
  71. depBlock.arrayRange[0] + 0.9,
  72. "var __WEBPACK_AMD_REQUIRE_ARRAY__ = "
  73. );
  74. source.replace(
  75. depBlock.arrayRange[1],
  76. depBlock.functionRange[0] - 1,
  77. "; ("
  78. );
  79. source.insert(
  80. depBlock.functionRange[1],
  81. ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__);"
  82. );
  83. source.replace(
  84. depBlock.functionRange[1],
  85. depBlock.errorCallbackRange[0] - 1,
  86. errorRangeBlock
  87. );
  88. source.replace(
  89. depBlock.errorCallbackRange[1],
  90. depBlock.outerRange[1] - 1,
  91. endBlock
  92. );
  93. return;
  94. }
  95. // has array range, function range, but no errorCallbackRange
  96. if (depBlock.arrayRange && depBlock.functionRange) {
  97. const startBlock = `${promise}.then(function() { `;
  98. const endBlock = `}${
  99. depBlock.functionBindThis ? ".bind(this)" : ""
  100. }).catch(${runtime.onError()})`;
  101. source.replace(
  102. depBlock.outerRange[0],
  103. depBlock.arrayRange[0] - 1,
  104. startBlock
  105. );
  106. source.insert(
  107. depBlock.arrayRange[0] + 0.9,
  108. "var __WEBPACK_AMD_REQUIRE_ARRAY__ = "
  109. );
  110. source.replace(
  111. depBlock.arrayRange[1],
  112. depBlock.functionRange[0] - 1,
  113. "; ("
  114. );
  115. source.insert(
  116. depBlock.functionRange[1],
  117. ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__);"
  118. );
  119. source.replace(
  120. depBlock.functionRange[1],
  121. depBlock.outerRange[1] - 1,
  122. endBlock
  123. );
  124. }
  125. }
  126. };
  127. module.exports = AMDRequireDependency;