RequireResolveHeaderDependency.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. class RequireResolveHeaderDependency extends NullDependency {
  12. constructor(range) {
  13. super();
  14. if (!Array.isArray(range)) throw new Error("range must be valid");
  15. this.range = range;
  16. }
  17. serialize(context) {
  18. const { write } = context;
  19. write(this.range);
  20. super.serialize(context);
  21. }
  22. static deserialize(context) {
  23. const obj = new RequireResolveHeaderDependency(context.read());
  24. obj.deserialize(context);
  25. return obj;
  26. }
  27. }
  28. makeSerializable(
  29. RequireResolveHeaderDependency,
  30. "webpack/lib/dependencies/RequireResolveHeaderDependency"
  31. );
  32. RequireResolveHeaderDependency.Template = class RequireResolveHeaderDependencyTemplate extends (
  33. NullDependency.Template
  34. ) {
  35. /**
  36. * @param {Dependency} dependency the dependency for which the template should be applied
  37. * @param {ReplaceSource} source the current replace source which can be modified
  38. * @param {DependencyTemplateContext} templateContext the context object
  39. * @returns {void}
  40. */
  41. apply(dependency, source, templateContext) {
  42. const dep = /** @type {RequireResolveHeaderDependency} */ (dependency);
  43. source.replace(dep.range[0], dep.range[1] - 1, "/*require.resolve*/");
  44. }
  45. applyAsTemplateArgument(name, dep, source) {
  46. source.replace(dep.range[0], dep.range[1] - 1, "/*require.resolve*/");
  47. }
  48. };
  49. module.exports = RequireResolveHeaderDependency;