CommonJsRequireContextDependency.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const ContextDependency = require("./ContextDependency");
  9. const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
  10. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  11. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  13. /** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
  14. /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
  15. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  16. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  17. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  18. class CommonJsRequireContextDependency extends ContextDependency {
  19. /**
  20. * Creates an instance of CommonJsRequireContextDependency.
  21. * @param {ContextDependencyOptions} options options for the context module
  22. * @param {Range} range location in source code
  23. * @param {Range=} valueRange location of the require call
  24. * @param {boolean | string=} inShorthand true or name
  25. * @param {string=} context context
  26. */
  27. constructor(options, range, valueRange, inShorthand, context) {
  28. super(options, context);
  29. this.range = range;
  30. this.valueRange = valueRange;
  31. // inShorthand must be serialized by subclasses that use it
  32. this.inShorthand = inShorthand;
  33. }
  34. get type() {
  35. return "cjs require context";
  36. }
  37. /**
  38. * Returns list of exports referenced by this dependency
  39. * @param {ModuleGraph} moduleGraph module graph
  40. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  41. * @returns {ReferencedExports} referenced exports
  42. */
  43. getReferencedExports(moduleGraph, runtime) {
  44. if (!this.options.referencedExports) {
  45. return Dependency.EXPORTS_OBJECT_REFERENCED;
  46. }
  47. return this.options.referencedExports.map((name) => ({
  48. name,
  49. canMangle: false
  50. }));
  51. }
  52. /**
  53. * Serializes this instance into the provided serializer context.
  54. * @param {ObjectSerializerContext} context context
  55. */
  56. serialize(context) {
  57. const { write } = context;
  58. write(this.range);
  59. write(this.valueRange);
  60. write(this.inShorthand);
  61. super.serialize(context);
  62. }
  63. /**
  64. * Restores this instance from the provided deserializer context.
  65. * @param {ObjectDeserializerContext} context context
  66. */
  67. deserialize(context) {
  68. const { read } = context;
  69. this.range = read();
  70. this.valueRange = read();
  71. this.inShorthand = read();
  72. super.deserialize(context);
  73. }
  74. }
  75. makeSerializable(
  76. CommonJsRequireContextDependency,
  77. "webpack/lib/dependencies/CommonJsRequireContextDependency"
  78. );
  79. CommonJsRequireContextDependency.Template =
  80. ContextDependencyTemplateAsRequireCall;
  81. module.exports = CommonJsRequireContextDependency;