You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

3 months ago
  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 ContextDependency = require("./ContextDependency");
  8. const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
  9. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  10. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  11. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  12. class CommonJsRequireContextDependency extends ContextDependency {
  13. /**
  14. * @param {TODO} options options for the context module
  15. * @param {Range} range location in source code
  16. * @param {Range | undefined} valueRange location of the require call
  17. * @param {boolean | string } inShorthand true or name
  18. * @param {string} context context
  19. */
  20. constructor(options, range, valueRange, inShorthand, context) {
  21. super(options, context);
  22. this.range = range;
  23. this.valueRange = valueRange;
  24. // inShorthand must be serialized by subclasses that use it
  25. this.inShorthand = inShorthand;
  26. }
  27. get type() {
  28. return "cjs require context";
  29. }
  30. /**
  31. * @param {ObjectSerializerContext} context context
  32. */
  33. serialize(context) {
  34. const { write } = context;
  35. write(this.range);
  36. write(this.valueRange);
  37. write(this.inShorthand);
  38. super.serialize(context);
  39. }
  40. /**
  41. * @param {ObjectDeserializerContext} context context
  42. */
  43. deserialize(context) {
  44. const { read } = context;
  45. this.range = read();
  46. this.valueRange = read();
  47. this.inShorthand = read();
  48. super.deserialize(context);
  49. }
  50. }
  51. makeSerializable(
  52. CommonJsRequireContextDependency,
  53. "webpack/lib/dependencies/CommonJsRequireContextDependency"
  54. );
  55. CommonJsRequireContextDependency.Template =
  56. ContextDependencyTemplateAsRequireCall;
  57. module.exports = CommonJsRequireContextDependency;