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. /** @import { ReplaceSource } from "webpack-sources" */
  10. /** @import AsyncDependenciesBlock from "../AsyncDependenciesBlock" */
  11. /** @import Dependency from "../Dependency" */
  12. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  13. /** @import { Range } from "../javascript/JavascriptParser" */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range, Range | false]>} ObjectDeserializerContext */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range, Range | false]>} 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. 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. this.range = context.read();
  49. const c1 = context.rest;
  50. this.contentRange = c1.read();
  51. const c2 = c1.rest;
  52. this.errorHandlerRange = c2.read();
  53. super.deserialize(c2.rest);
  54. }
  55. }
  56. makeSerializable(
  57. RequireEnsureDependency,
  58. "webpack/lib/dependencies/RequireEnsureDependency"
  59. );
  60. RequireEnsureDependency.Template = class RequireEnsureDependencyTemplate extends (
  61. NullDependency.Template
  62. ) {
  63. /**
  64. * Applies the plugin by registering its hooks on the compiler.
  65. * @param {Dependency} dependency the dependency for which the template should be applied
  66. * @param {ReplaceSource} source the current replace source which can be modified
  67. * @param {DependencyTemplateContext} templateContext the context object
  68. * @returns {void}
  69. */
  70. apply(
  71. dependency,
  72. source,
  73. { module, runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
  74. ) {
  75. const dep = /** @type {RequireEnsureDependency} */ (dependency);
  76. const depBlock = /** @type {AsyncDependenciesBlock} */ (
  77. moduleGraph.getParentBlock(dep)
  78. );
  79. const promise = runtimeTemplate.blockPromise({
  80. chunkGraph,
  81. block: depBlock,
  82. message: "require.ensure",
  83. runtimeRequirements,
  84. originModule: module
  85. });
  86. const range = dep.range;
  87. const contentRange = dep.contentRange;
  88. const errorHandlerRange = dep.errorHandlerRange;
  89. source.replace(range[0], contentRange[0] - 1, `${promise}.then((`);
  90. if (errorHandlerRange) {
  91. source.replace(
  92. contentRange[1],
  93. errorHandlerRange[0] - 1,
  94. `).bind(null, ${RuntimeGlobals.require}))['catch'](`
  95. );
  96. source.replace(errorHandlerRange[1], range[1] - 1, ")");
  97. } else {
  98. source.replace(
  99. contentRange[1],
  100. range[1] - 1,
  101. `).bind(null, ${RuntimeGlobals.require}))['catch'](${RuntimeGlobals.uncaughtErrorHandler})`
  102. );
  103. }
  104. }
  105. };
  106. module.exports = RequireEnsureDependency;