HarmonyEvaluatedImportSpecifierDependency.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. value =
  104. ids[0] === "__esModule"
  105. ? ids.length === 1 || undefined
  106. : exportsInfo.isExportProvided(ids);
  107. break;
  108. }
  109. case "dynamic": {
  110. if (ids[0] !== "default") {
  111. value = exportsInfo.isExportProvided(ids);
  112. }
  113. break;
  114. }
  115. // default-only could lead to runtime error, when default value is primitive
  116. }
  117. if (typeof value === "boolean") {
  118. source.replace(dep.range[0], dep.range[1] - 1, ` ${value}`);
  119. } else {
  120. const usedName = exportsInfo.getUsedName(ids, runtime);
  121. const code = this._getCodeForIds(
  122. dep,
  123. source,
  124. templateContext,
  125. ids.slice(0, -1)
  126. );
  127. source.replace(
  128. dep.range[0],
  129. dep.range[1] - 1,
  130. `${
  131. usedName ? JSON.stringify(usedName[usedName.length - 1]) : '""'
  132. } in ${code}`
  133. );
  134. }
  135. }
  136. };
  137. module.exports = HarmonyEvaluatedImportSpecifierDependency;