HarmonyEvaluatedImportSpecifierDependency.js 4.6 KB

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