CommonJsRequireContextDependency.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /** @import { Range } from "../javascript/JavascriptParser" */
  11. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range | undefined, boolean | string | undefined]>} ObjectDeserializerContext */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range | undefined, boolean | string | undefined]>} ObjectSerializerContext */
  13. /** @import { ContextDependencyOptions } from "./ContextDependency" */
  14. /** @import { ReferencedExports } from "../Dependency" */
  15. /** @import ModuleGraph from "../ModuleGraph" */
  16. /** @import { RuntimeSpec } from "../util/runtime" */
  17. class CommonJsRequireContextDependency extends ContextDependency {
  18. /**
  19. * Creates an instance of CommonJsRequireContextDependency.
  20. * @param {ContextDependencyOptions} options options for the context module
  21. * @param {Range} range location in source code
  22. * @param {Range=} valueRange location of the require call
  23. * @param {boolean | string=} inShorthand true or name
  24. * @param {string=} context context
  25. */
  26. constructor(options, range, valueRange, inShorthand, context) {
  27. super(options, context);
  28. this.range = range;
  29. this.valueRange = valueRange;
  30. // inShorthand must be serialized by subclasses that use it
  31. /** @type {string | boolean | undefined} */
  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. context.write(this.range).write(this.valueRange).write(this.inShorthand);
  58. super.serialize(context);
  59. }
  60. /**
  61. * Restores this instance from the provided deserializer context.
  62. * @param {ObjectDeserializerContext} context context
  63. */
  64. deserialize(context) {
  65. this.range = context.read();
  66. const c1 = context.rest;
  67. this.valueRange = c1.read();
  68. const c2 = c1.rest;
  69. this.inShorthand = c2.read();
  70. super.deserialize(c2.rest);
  71. }
  72. }
  73. makeSerializable(
  74. CommonJsRequireContextDependency,
  75. "webpack/lib/dependencies/CommonJsRequireContextDependency"
  76. );
  77. CommonJsRequireContextDependency.Template =
  78. ContextDependencyTemplateAsRequireCall;
  79. module.exports = CommonJsRequireContextDependency;