RequireEnsureDependency.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 RequireEnsureDependency extends NullDependency {
  14. constructor(range, contentRange, errorHandlerRange) {
  15. super();
  16. this.range = range;
  17. this.contentRange = contentRange;
  18. this.errorHandlerRange = errorHandlerRange;
  19. }
  20. get type() {
  21. return "require.ensure";
  22. }
  23. serialize(context) {
  24. const { write } = context;
  25. write(this.range);
  26. write(this.contentRange);
  27. write(this.errorHandlerRange);
  28. super.serialize(context);
  29. }
  30. deserialize(context) {
  31. const { read } = context;
  32. this.range = read();
  33. this.contentRange = read();
  34. this.errorHandlerRange = read();
  35. super.deserialize(context);
  36. }
  37. }
  38. makeSerializable(
  39. RequireEnsureDependency,
  40. "webpack/lib/dependencies/RequireEnsureDependency"
  41. );
  42. RequireEnsureDependency.Template = class RequireEnsureDependencyTemplate extends (
  43. NullDependency.Template
  44. ) {
  45. /**
  46. * @param {Dependency} dependency the dependency for which the template should be applied
  47. * @param {ReplaceSource} source the current replace source which can be modified
  48. * @param {DependencyTemplateContext} templateContext the context object
  49. * @returns {void}
  50. */
  51. apply(
  52. dependency,
  53. source,
  54. { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
  55. ) {
  56. const dep = /** @type {RequireEnsureDependency} */ (dependency);
  57. const depBlock = /** @type {AsyncDependenciesBlock} */ (
  58. moduleGraph.getParentBlock(dep)
  59. );
  60. const promise = runtimeTemplate.blockPromise({
  61. chunkGraph,
  62. block: depBlock,
  63. message: "require.ensure",
  64. runtimeRequirements
  65. });
  66. const range = dep.range;
  67. const contentRange = dep.contentRange;
  68. const errorHandlerRange = dep.errorHandlerRange;
  69. source.replace(range[0], contentRange[0] - 1, `${promise}.then((`);
  70. if (errorHandlerRange) {
  71. source.replace(
  72. contentRange[1],
  73. errorHandlerRange[0] - 1,
  74. ").bind(null, __webpack_require__))['catch']("
  75. );
  76. source.replace(errorHandlerRange[1], range[1] - 1, ")");
  77. } else {
  78. source.replace(
  79. contentRange[1],
  80. range[1] - 1,
  81. `).bind(null, __webpack_require__))['catch'](${RuntimeGlobals.uncaughtErrorHandler})`
  82. );
  83. }
  84. }
  85. };
  86. module.exports = RequireEnsureDependency;