10
0

ContextElementDependency.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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("../ContextModule")} ContextModule */
  10. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  11. /** @typedef {import("../Module")} Module */
  12. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  17. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  18. class ContextElementDependency extends ModuleDependency {
  19. /**
  20. * @param {string} request request
  21. * @param {string|undefined} userRequest user request
  22. * @param {string | undefined} typePrefix type prefix
  23. * @param {string} category category
  24. * @param {(string[][] | null)=} referencedExports referenced exports
  25. * @param {string=} context context
  26. * @param {ImportAttributes=} attributes import assertions
  27. */
  28. constructor(
  29. request,
  30. userRequest,
  31. typePrefix,
  32. category,
  33. referencedExports,
  34. context,
  35. attributes
  36. ) {
  37. super(request);
  38. this.referencedExports = referencedExports;
  39. this._typePrefix = typePrefix;
  40. this._category = category;
  41. this._context = context || undefined;
  42. if (userRequest) {
  43. this.userRequest = userRequest;
  44. }
  45. this.assertions = attributes;
  46. }
  47. get type() {
  48. if (this._typePrefix) {
  49. return `${this._typePrefix} context element`;
  50. }
  51. return "context element";
  52. }
  53. get category() {
  54. return this._category;
  55. }
  56. /**
  57. * Returns list of exports referenced by this dependency
  58. * @param {ModuleGraph} moduleGraph module graph
  59. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  60. * @returns {(string[] | ReferencedExport)[]} referenced exports
  61. */
  62. getReferencedExports(moduleGraph, runtime) {
  63. if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
  64. const refs = [];
  65. for (const referencedExport of this.referencedExports) {
  66. if (
  67. this._typePrefix === "import()" &&
  68. referencedExport[0] === "default"
  69. ) {
  70. const selfModule =
  71. /** @type {ContextModule} */
  72. (moduleGraph.getParentModule(this));
  73. const importedModule =
  74. /** @type {Module} */
  75. (moduleGraph.getModule(this));
  76. const exportsType = importedModule.getExportsType(
  77. moduleGraph,
  78. selfModule.options.namespaceObject === "strict"
  79. );
  80. if (
  81. exportsType === "default-only" ||
  82. exportsType === "default-with-named"
  83. ) {
  84. return Dependency.EXPORTS_OBJECT_REFERENCED;
  85. }
  86. }
  87. refs.push({
  88. name: referencedExport,
  89. canMangle: false
  90. });
  91. }
  92. return refs;
  93. }
  94. /**
  95. * @param {ObjectSerializerContext} context context
  96. */
  97. serialize(context) {
  98. const { write } = context;
  99. write(this._typePrefix);
  100. write(this._category);
  101. write(this.referencedExports);
  102. write(this.assertions);
  103. super.serialize(context);
  104. }
  105. /**
  106. * @param {ObjectDeserializerContext} context context
  107. */
  108. deserialize(context) {
  109. const { read } = context;
  110. this._typePrefix = read();
  111. this._category = read();
  112. this.referencedExports = read();
  113. this.assertions = read();
  114. super.deserialize(context);
  115. }
  116. }
  117. makeSerializable(
  118. ContextElementDependency,
  119. "webpack/lib/dependencies/ContextElementDependency"
  120. );
  121. module.exports = ContextElementDependency;