RequireEnsureDependency.js 3.2 KB

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