RequireEnsureDependency.js 3.5 KB

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