CommonJsRequireDependency.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ModuleDependency = require("./ModuleDependency");
  9. const ModuleDependencyTemplateAsId = require("./ModuleDependencyTemplateAsId");
  10. /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
  11. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  12. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  13. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  16. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  17. class CommonJsRequireDependency extends ModuleDependency {
  18. /**
  19. * Creates an instance of CommonJsRequireDependency.
  20. * @param {string} request request
  21. * @param {Range=} range location in source code
  22. * @param {string=} context request context
  23. * @param {RawReferencedExports | null=} referencedExports list of referenced exports
  24. */
  25. constructor(request, range, context, referencedExports = null) {
  26. super(request);
  27. this.range = range;
  28. this._context = context;
  29. this.referencedExports = referencedExports;
  30. }
  31. get type() {
  32. return "cjs require";
  33. }
  34. get category() {
  35. return "commonjs";
  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.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
  45. return this.referencedExports.map((name) => ({
  46. name,
  47. canMangle: false
  48. }));
  49. }
  50. /**
  51. * Serializes this instance into the provided serializer context.
  52. * @param {ObjectSerializerContext} context context
  53. */
  54. serialize(context) {
  55. const { write } = context;
  56. write(this.referencedExports);
  57. super.serialize(context);
  58. }
  59. /**
  60. * Restores this instance from the provided deserializer context.
  61. * @param {ObjectDeserializerContext} context context
  62. */
  63. deserialize(context) {
  64. const { read } = context;
  65. this.referencedExports = read();
  66. super.deserialize(context);
  67. }
  68. }
  69. CommonJsRequireDependency.Template = ModuleDependencyTemplateAsId;
  70. makeSerializable(
  71. CommonJsRequireDependency,
  72. "webpack/lib/dependencies/CommonJsRequireDependency"
  73. );
  74. module.exports = CommonJsRequireDependency;