ContextElementDependency.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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").RawReferencedExports} RawReferencedExports */
  11. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  12. /** @typedef {import("../Module")} Module */
  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. * Creates an instance of ContextElementDependency.
  21. * @param {string} request request
  22. * @param {string | undefined} userRequest user request
  23. * @param {string | undefined} typePrefix type prefix
  24. * @param {string} category category
  25. * @param {RawReferencedExports | null=} referencedExports referenced exports
  26. * @param {string=} context context
  27. * @param {ImportAttributes=} attributes import assertions
  28. */
  29. constructor(
  30. request,
  31. userRequest,
  32. typePrefix,
  33. category,
  34. referencedExports,
  35. context,
  36. attributes
  37. ) {
  38. super(request);
  39. if (userRequest) {
  40. this.userRequest = userRequest;
  41. }
  42. this._typePrefix = typePrefix;
  43. this._category = category;
  44. this.referencedExports = referencedExports;
  45. this._context = context || undefined;
  46. this.attributes = attributes;
  47. }
  48. get type() {
  49. if (this._typePrefix) {
  50. return `${this._typePrefix} context element`;
  51. }
  52. return "context element";
  53. }
  54. get category() {
  55. return this._category;
  56. }
  57. /**
  58. * Returns an identifier to merge equal requests.
  59. * @returns {string | null} an identifier to merge equal requests
  60. */
  61. getResourceIdentifier() {
  62. let str = super.getResourceIdentifier();
  63. if (this.attributes) {
  64. str += `|attributes${JSON.stringify(this.attributes)}`;
  65. }
  66. return str;
  67. }
  68. /**
  69. * Returns list of exports referenced by this dependency
  70. * @param {ModuleGraph} moduleGraph module graph
  71. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  72. * @returns {ReferencedExports} referenced exports
  73. */
  74. getReferencedExports(moduleGraph, runtime) {
  75. if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
  76. /** @type {ReferencedExports} */
  77. const refs = [];
  78. for (const referencedExport of this.referencedExports) {
  79. if (
  80. this._typePrefix === "import()" &&
  81. referencedExport[0] === "default"
  82. ) {
  83. const selfModule =
  84. /** @type {ContextModule} */
  85. (moduleGraph.getParentModule(this));
  86. const importedModule =
  87. /** @type {Module} */
  88. (moduleGraph.getModule(this));
  89. const exportsType = importedModule.getExportsType(
  90. moduleGraph,
  91. selfModule.options.namespaceObject === "strict"
  92. );
  93. if (
  94. exportsType === "default-only" ||
  95. exportsType === "default-with-named"
  96. ) {
  97. return Dependency.EXPORTS_OBJECT_REFERENCED;
  98. }
  99. }
  100. refs.push({
  101. name: referencedExport,
  102. canMangle: false
  103. });
  104. }
  105. return refs;
  106. }
  107. /**
  108. * Serializes this instance into the provided serializer context.
  109. * @param {ObjectSerializerContext} context context
  110. */
  111. serialize(context) {
  112. const { write } = context;
  113. write(this._typePrefix);
  114. write(this._category);
  115. write(this.referencedExports);
  116. write(this.attributes);
  117. super.serialize(context);
  118. }
  119. /**
  120. * Restores this instance from the provided deserializer context.
  121. * @param {ObjectDeserializerContext} context context
  122. */
  123. deserialize(context) {
  124. const { read } = context;
  125. this._typePrefix = read();
  126. this._category = read();
  127. this.referencedExports = read();
  128. this.attributes = read();
  129. super.deserialize(context);
  130. }
  131. }
  132. makeSerializable(
  133. ContextElementDependency,
  134. "webpack/lib/dependencies/ContextElementDependency"
  135. );
  136. module.exports = ContextElementDependency;