ContextElementDependency.js 2.2 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 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. get category() {
  45. return this._category;
  46. }
  47. /**
  48. * Returns list of exports referenced by this dependency
  49. * @param {ModuleGraph} moduleGraph module graph
  50. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  51. * @returns {(string[] | ReferencedExport)[]} referenced exports
  52. */
  53. getReferencedExports(moduleGraph, runtime) {
  54. return this.referencedExports
  55. ? this.referencedExports.map(e => ({
  56. name: e,
  57. canMangle: false
  58. }))
  59. : Dependency.EXPORTS_OBJECT_REFERENCED;
  60. }
  61. serialize(context) {
  62. const { write } = context;
  63. write(this._typePrefix);
  64. write(this._category);
  65. write(this.referencedExports);
  66. super.serialize(context);
  67. }
  68. deserialize(context) {
  69. const { read } = context;
  70. this._typePrefix = read();
  71. this._category = read();
  72. this.referencedExports = read();
  73. super.deserialize(context);
  74. }
  75. }
  76. makeSerializable(
  77. ContextElementDependency,
  78. "webpack/lib/dependencies/ContextElementDependency"
  79. );
  80. module.exports = ContextElementDependency;