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.

58 lines
1.4 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 RequireEnsureDependency extends NullDependency {
  8. constructor(block) {
  9. super();
  10. this.block = block;
  11. }
  12. get type() {
  13. return "require.ensure";
  14. }
  15. }
  16. RequireEnsureDependency.Template = class RequireEnsureDependencyTemplate {
  17. apply(dep, source, runtime) {
  18. const depBlock = dep.block;
  19. const promise = runtime.blockPromise({
  20. block: depBlock,
  21. message: "require.ensure"
  22. });
  23. const errorCallbackExists =
  24. depBlock.expr.arguments.length === 4 ||
  25. (!depBlock.chunkName && depBlock.expr.arguments.length === 3);
  26. const startBlock = `${promise}.then((`;
  27. const middleBlock = ").bind(null, __webpack_require__)).catch(";
  28. const endBlock = `).bind(null, __webpack_require__)).catch(${runtime.onError()})`;
  29. source.replace(
  30. depBlock.expr.range[0],
  31. depBlock.expr.arguments[1].range[0] - 1,
  32. startBlock
  33. );
  34. if (errorCallbackExists) {
  35. source.replace(
  36. depBlock.expr.arguments[1].range[1],
  37. depBlock.expr.arguments[2].range[0] - 1,
  38. middleBlock
  39. );
  40. source.replace(
  41. depBlock.expr.arguments[2].range[1],
  42. depBlock.expr.range[1] - 1,
  43. ")"
  44. );
  45. } else {
  46. source.replace(
  47. depBlock.expr.arguments[1].range[1],
  48. depBlock.expr.range[1] - 1,
  49. endBlock
  50. );
  51. }
  52. }
  53. };
  54. module.exports = RequireEnsureDependency;