ContextElementDependency.js 2.8 KB

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