ContextElementDependency.js 2.6 KB

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