RequireHeaderDependency.js 1.7 KB

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