HarmonyEvaluatedImportSpecifierDependency.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const { ExportPresenceModes } = require("./HarmonyImportDependency");
  8. const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
  9. const { ImportPhase } = require("./ImportPhase");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../Dependency")} Dependency */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  14. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  15. /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
  16. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  17. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  19. /** @typedef {import("./HarmonyImportDependency").Ids} Ids */
  20. /**
  21. * Dependency for static evaluating import specifier. e.g.
  22. * @example
  23. * import a from "a";
  24. * "x" in a;
  25. * a.x !== undefined; // if x value statically analyzable
  26. */
  27. class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDependency {
  28. /**
  29. * Creates an instance of HarmonyEvaluatedImportSpecifierDependency.
  30. * @param {string} request the request string
  31. * @param {number} sourceOrder source order
  32. * @param {Ids} ids ids
  33. * @param {string} name name
  34. * @param {Range} range location in source code
  35. * @param {ImportAttributes | undefined} attributes import assertions
  36. * @param {string} operator operator
  37. */
  38. constructor(request, sourceOrder, ids, name, range, attributes, operator) {
  39. super(
  40. request,
  41. sourceOrder,
  42. ids,
  43. name,
  44. range,
  45. ExportPresenceModes.NONE,
  46. ImportPhase.Evaluation,
  47. attributes,
  48. []
  49. );
  50. this.operator = operator;
  51. }
  52. get type() {
  53. return `evaluated X ${this.operator} harmony import specifier`;
  54. }
  55. /**
  56. * Serializes this instance into the provided serializer context.
  57. * @param {ObjectSerializerContext} context context
  58. */
  59. serialize(context) {
  60. super.serialize(context);
  61. const { write } = context;
  62. write(this.operator);
  63. }
  64. /**
  65. * Restores this instance from the provided deserializer context.
  66. * @param {ObjectDeserializerContext} context context
  67. */
  68. deserialize(context) {
  69. super.deserialize(context);
  70. const { read } = context;
  71. this.operator = read();
  72. }
  73. }
  74. makeSerializable(
  75. HarmonyEvaluatedImportSpecifierDependency,
  76. "webpack/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency"
  77. );
  78. HarmonyEvaluatedImportSpecifierDependency.Template = class HarmonyEvaluatedImportSpecifierDependencyTemplate extends (
  79. HarmonyImportSpecifierDependency.Template
  80. ) {
  81. /**
  82. * Applies the plugin by registering its hooks on the compiler.
  83. * @param {Dependency} dependency the dependency for which the template should be applied
  84. * @param {ReplaceSource} source the current replace source which can be modified
  85. * @param {DependencyTemplateContext} templateContext the context object
  86. * @returns {void}
  87. */
  88. apply(dependency, source, templateContext) {
  89. const dep =
  90. /** @type {HarmonyEvaluatedImportSpecifierDependency} */
  91. (dependency);
  92. const { module, moduleGraph, runtime } = templateContext;
  93. const connection = moduleGraph.getConnection(dep);
  94. // Skip rendering depending when dependency is conditional
  95. if (connection && !connection.isTargetActive(runtime)) return;
  96. const exportsInfo = moduleGraph.getExportsInfo(
  97. /** @type {ModuleGraphConnection} */ (connection).module
  98. );
  99. const ids = dep.getIds(moduleGraph);
  100. /** @type {boolean | undefined | null} */
  101. let value;
  102. const exportsType =
  103. /** @type {ModuleGraphConnection} */
  104. (connection).module.getExportsType(
  105. moduleGraph,
  106. /** @type {BuildMeta} */
  107. (module.buildMeta).strictHarmonyModule
  108. );
  109. switch (exportsType) {
  110. case "default-with-named": {
  111. if (ids[0] === "default") {
  112. value =
  113. ids.length === 1 || exportsInfo.isExportProvided(ids.slice(1));
  114. } else {
  115. value = exportsInfo.isExportProvided(ids);
  116. }
  117. break;
  118. }
  119. case "namespace": {
  120. value =
  121. ids[0] === "__esModule"
  122. ? ids.length === 1 || undefined
  123. : exportsInfo.isExportProvided(ids);
  124. break;
  125. }
  126. case "dynamic": {
  127. if (ids[0] !== "default") {
  128. value = exportsInfo.isExportProvided(ids);
  129. }
  130. break;
  131. }
  132. // default-only could lead to runtime error, when default value is primitive
  133. }
  134. if (typeof value === "boolean") {
  135. source.replace(dep.range[0], dep.range[1] - 1, ` ${value}`);
  136. } else {
  137. const usedName = exportsInfo.getUsedName(ids, runtime);
  138. const code = this._getCodeForIds(
  139. dep,
  140. source,
  141. templateContext,
  142. ids.slice(0, -1)
  143. );
  144. source.replace(
  145. dep.range[0],
  146. dep.range[1] - 1,
  147. `${
  148. usedName ? JSON.stringify(usedName[usedName.length - 1]) : '""'
  149. } in ${code}`
  150. );
  151. }
  152. }
  153. };
  154. module.exports = HarmonyEvaluatedImportSpecifierDependency;