HarmonyEvaluatedImportSpecifierDependency.js 3.6 KB

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