AMDRequireDependency.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const DepBlockHelpers = require("./DepBlockHelpers");
  8. class AMDRequireDependency extends NullDependency {
  9. constructor(block) {
  10. super();
  11. this.block = block;
  12. }
  13. }
  14. AMDRequireDependency.Template = class AMDRequireDependencyTemplate {
  15. apply(dep, source, outputOptions, requestShortener) {
  16. const depBlock = dep.block;
  17. const wrapper = DepBlockHelpers.getLoadDepBlockWrapper(depBlock, outputOptions, requestShortener, "require");
  18. // has array range but no function range
  19. if(depBlock.arrayRange && !depBlock.functionRange) {
  20. const startBlock = wrapper[0] + "function() {";
  21. const endBlock = `;}${wrapper[1]}__webpack_require__.oe${wrapper[2]}`;
  22. source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1, startBlock);
  23. source.replace(depBlock.arrayRange[1], depBlock.outerRange[1] - 1, endBlock);
  24. return;
  25. }
  26. // has function range but no array range
  27. if(depBlock.functionRange && !depBlock.arrayRange) {
  28. const startBlock = wrapper[0] + "function() {(";
  29. const endBlock = `.call(exports, __webpack_require__, exports, module));}${wrapper[1]}__webpack_require__.oe${wrapper[2]}`;
  30. source.replace(depBlock.outerRange[0], depBlock.functionRange[0] - 1, startBlock);
  31. source.replace(depBlock.functionRange[1], depBlock.outerRange[1] - 1, endBlock);
  32. return;
  33. }
  34. // has array range, function range, and errorCallbackRange
  35. if(depBlock.arrayRange && depBlock.functionRange && depBlock.errorCallbackRange) {
  36. const startBlock = wrapper[0] + "function() { ";
  37. const errorRangeBlock = `}${depBlock.functionBindThis ? ".bind(this)" : ""}${wrapper[1]}`;
  38. const endBlock = `${depBlock.errorCallbackBindThis ? ".bind(this)" : ""}${wrapper[2]}`;
  39. source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1, startBlock);
  40. source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
  41. source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");
  42. source.insert(depBlock.functionRange[1], ".apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
  43. source.replace(depBlock.functionRange[1], depBlock.errorCallbackRange[0] - 1, errorRangeBlock);
  44. source.replace(depBlock.errorCallbackRange[1], depBlock.outerRange[1] - 1, endBlock);
  45. return;
  46. }
  47. // has array range, function range, but no errorCallbackRange
  48. if(depBlock.arrayRange && depBlock.functionRange) {
  49. const startBlock = wrapper[0] + "function() { ";
  50. const endBlock = `}${depBlock.functionBindThis ? ".bind(this)" : ""}${wrapper[1]}__webpack_require__.oe${wrapper[2]}`;
  51. source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1, startBlock);
  52. source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
  53. source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");
  54. source.insert(depBlock.functionRange[1], ".apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
  55. source.replace(depBlock.functionRange[1], depBlock.outerRange[1] - 1, endBlock);
  56. }
  57. }
  58. };
  59. module.exports = AMDRequireDependency;