RequireResolveHeaderDependency.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const NullDependency = require("./NullDependency");
  8. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("../Dependency")} Dependency */
  10. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  11. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  14. class RequireResolveHeaderDependency extends NullDependency {
  15. /**
  16. * @param {Range} range range
  17. */
  18. constructor(range) {
  19. super();
  20. if (!Array.isArray(range)) throw new Error("range must be valid");
  21. this.range = range;
  22. }
  23. /**
  24. * @param {ObjectSerializerContext} context context
  25. */
  26. serialize(context) {
  27. const { write } = context;
  28. write(this.range);
  29. super.serialize(context);
  30. }
  31. /**
  32. * @param {ObjectDeserializerContext} context context
  33. * @returns {RequireResolveHeaderDependency} RequireResolveHeaderDependency
  34. */
  35. static deserialize(context) {
  36. const obj = new RequireResolveHeaderDependency(context.read());
  37. obj.deserialize(context);
  38. return obj;
  39. }
  40. }
  41. makeSerializable(
  42. RequireResolveHeaderDependency,
  43. "webpack/lib/dependencies/RequireResolveHeaderDependency"
  44. );
  45. RequireResolveHeaderDependency.Template = class RequireResolveHeaderDependencyTemplate extends (
  46. NullDependency.Template
  47. ) {
  48. /**
  49. * @param {Dependency} dependency the dependency for which the template should be applied
  50. * @param {ReplaceSource} source the current replace source which can be modified
  51. * @param {DependencyTemplateContext} templateContext the context object
  52. * @returns {void}
  53. */
  54. apply(dependency, source, templateContext) {
  55. const dep = /** @type {RequireResolveHeaderDependency} */ (dependency);
  56. source.replace(dep.range[0], dep.range[1] - 1, "/*require.resolve*/");
  57. }
  58. /**
  59. * @param {string} name name
  60. * @param {RequireResolveHeaderDependency} dep dependency
  61. * @param {ReplaceSource} source source
  62. */
  63. applyAsTemplateArgument(name, dep, source) {
  64. source.replace(dep.range[0], dep.range[1] - 1, "/*require.resolve*/");
  65. }
  66. };
  67. module.exports = RequireResolveHeaderDependency;