CommonJsRequireContextDependency.js 2.1 KB

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