RequireHeaderDependency.js 2.2 KB

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