ContextElementDependency.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  10. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  11. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  13. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  14. class ContextElementDependency extends ModuleDependency {
  15. /**
  16. * @param {string} request request
  17. * @param {string|undefined} userRequest user request
  18. * @param {string} typePrefix type prefix
  19. * @param {string} category category
  20. * @param {string[][]=} referencedExports referenced exports
  21. * @param {string=} context context
  22. */
  23. constructor(
  24. request,
  25. userRequest,
  26. typePrefix,
  27. category,
  28. referencedExports,
  29. context
  30. ) {
  31. super(request);
  32. this.referencedExports = referencedExports;
  33. this._typePrefix = typePrefix;
  34. this._category = category;
  35. this._context = context || undefined;
  36. if (userRequest) {
  37. this.userRequest = userRequest;
  38. }
  39. }
  40. get type() {
  41. if (this._typePrefix) {
  42. return `${this._typePrefix} context element`;
  43. }
  44. return "context element";
  45. }
  46. get category() {
  47. return this._category;
  48. }
  49. /**
  50. * Returns list of exports referenced by this dependency
  51. * @param {ModuleGraph} moduleGraph module graph
  52. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  53. * @returns {(string[] | ReferencedExport)[]} referenced exports
  54. */
  55. getReferencedExports(moduleGraph, runtime) {
  56. return this.referencedExports
  57. ? this.referencedExports.map(e => ({
  58. name: e,
  59. canMangle: false
  60. }))
  61. : Dependency.EXPORTS_OBJECT_REFERENCED;
  62. }
  63. /**
  64. * @param {ObjectSerializerContext} context context
  65. */
  66. serialize(context) {
  67. const { write } = context;
  68. write(this._typePrefix);
  69. write(this._category);
  70. write(this.referencedExports);
  71. super.serialize(context);
  72. }
  73. /**
  74. * @param {ObjectDeserializerContext} context context
  75. */
  76. deserialize(context) {
  77. const { read } = context;
  78. this._typePrefix = read();
  79. this._category = read();
  80. this.referencedExports = read();
  81. super.deserialize(context);
  82. }
  83. }
  84. makeSerializable(
  85. ContextElementDependency,
  86. "webpack/lib/dependencies/ContextElementDependency"
  87. );
  88. module.exports = ContextElementDependency;