ContextElementDependency.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * @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 {RawReferencedExports | 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. if (userRequest) {
  39. this.userRequest = userRequest;
  40. }
  41. this._typePrefix = typePrefix;
  42. this._category = category;
  43. this.referencedExports = referencedExports;
  44. this._context = context || undefined;
  45. this.attributes = 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 {string | null} an identifier to merge equal requests
  58. */
  59. getResourceIdentifier() {
  60. let str = super.getResourceIdentifier();
  61. if (this.attributes) {
  62. str += `|importAttributes${JSON.stringify(this.attributes)}`;
  63. }
  64. return str;
  65. }
  66. /**
  67. * Returns list of exports referenced by this dependency
  68. * @param {ModuleGraph} moduleGraph module graph
  69. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  70. * @returns {ReferencedExports} referenced exports
  71. */
  72. getReferencedExports(moduleGraph, runtime) {
  73. if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
  74. /** @type {ReferencedExports} */
  75. const refs = [];
  76. for (const referencedExport of this.referencedExports) {
  77. if (
  78. this._typePrefix === "import()" &&
  79. referencedExport[0] === "default"
  80. ) {
  81. const selfModule =
  82. /** @type {ContextModule} */
  83. (moduleGraph.getParentModule(this));
  84. const importedModule =
  85. /** @type {Module} */
  86. (moduleGraph.getModule(this));
  87. const exportsType = importedModule.getExportsType(
  88. moduleGraph,
  89. selfModule.options.namespaceObject === "strict"
  90. );
  91. if (
  92. exportsType === "default-only" ||
  93. exportsType === "default-with-named"
  94. ) {
  95. return Dependency.EXPORTS_OBJECT_REFERENCED;
  96. }
  97. }
  98. refs.push({
  99. name: referencedExport,
  100. canMangle: false
  101. });
  102. }
  103. return refs;
  104. }
  105. /**
  106. * @param {ObjectSerializerContext} context context
  107. */
  108. serialize(context) {
  109. const { write } = context;
  110. write(this._typePrefix);
  111. write(this._category);
  112. write(this.referencedExports);
  113. write(this.attributes);
  114. super.serialize(context);
  115. }
  116. /**
  117. * @param {ObjectDeserializerContext} context context
  118. */
  119. deserialize(context) {
  120. const { read } = context;
  121. this._typePrefix = read();
  122. this._category = read();
  123. this.referencedExports = read();
  124. this.attributes = read();
  125. super.deserialize(context);
  126. }
  127. }
  128. makeSerializable(
  129. ContextElementDependency,
  130. "webpack/lib/dependencies/ContextElementDependency"
  131. );
  132. module.exports = ContextElementDependency;