chainedImports.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../Dependency")} Dependency */
  7. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  8. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  9. /**
  10. * @summary Get the subset of ids and their corresponding range in an id chain that should be re-rendered by webpack.
  11. * Only those in the chain that are actually referring to namespaces or imports should be re-rendered.
  12. * Deeper member accessors on the imported object should not be re-rendered. If deeper member accessors are re-rendered,
  13. * there is a potential loss of meaning with rendering a quoted accessor as an unquoted accessor, or vice versa,
  14. * because minifiers treat quoted accessors differently. e.g. import { a } from "./module"; a["b"] vs a.b
  15. * @param {string[]} untrimmedIds chained ids
  16. * @param {Range} untrimmedRange range encompassing allIds
  17. * @param {Range[]} ranges cumulative range of ids for each of allIds
  18. * @param {ModuleGraph} moduleGraph moduleGraph
  19. * @param {Dependency} dependency dependency
  20. * @returns {{trimmedIds: string[], trimmedRange: Range}} computed trimmed ids and cumulative range of those ids
  21. */
  22. exports.getTrimmedIdsAndRange = (
  23. untrimmedIds,
  24. untrimmedRange,
  25. ranges,
  26. moduleGraph,
  27. dependency
  28. ) => {
  29. let trimmedIds = trimIdsToThoseImported(
  30. untrimmedIds,
  31. moduleGraph,
  32. dependency
  33. );
  34. let trimmedRange = untrimmedRange;
  35. if (trimmedIds.length !== untrimmedIds.length) {
  36. // The array returned from dep.idRanges is right-aligned with the array returned from dep.names.
  37. // Meaning, the two arrays may not always have the same number of elements, but the last element of
  38. // dep.idRanges corresponds to [the expression fragment to the left of] the last element of dep.names.
  39. // Use this to find the correct replacement range based on the number of ids that were trimmed.
  40. const idx =
  41. ranges === undefined
  42. ? -1 /* trigger failure case below */
  43. : ranges.length + (trimmedIds.length - untrimmedIds.length);
  44. if (idx < 0 || idx >= ranges.length) {
  45. // cspell:ignore minifiers
  46. // Should not happen but we can't throw an error here because of backward compatibility with
  47. // external plugins in wp5. Instead, we just disable trimming for now. This may break some minifiers.
  48. trimmedIds = untrimmedIds;
  49. // TODO webpack 6 remove the "trimmedIds = ids" above and uncomment the following line instead.
  50. // throw new Error("Missing range starts data for id replacement trimming.");
  51. } else {
  52. trimmedRange = ranges[idx];
  53. }
  54. }
  55. return { trimmedIds, trimmedRange };
  56. };
  57. /**
  58. * @summary Determine which IDs in the id chain are actually referring to namespaces or imports,
  59. * and which are deeper member accessors on the imported object.
  60. * @param {string[]} ids untrimmed ids
  61. * @param {ModuleGraph} moduleGraph moduleGraph
  62. * @param {Dependency} dependency dependency
  63. * @returns {string[]} trimmed ids
  64. */
  65. function trimIdsToThoseImported(ids, moduleGraph, dependency) {
  66. let trimmedIds = [];
  67. const exportsInfo = moduleGraph.getExportsInfo(
  68. moduleGraph.getModule(dependency)
  69. );
  70. let currentExportsInfo = /** @type {ExportsInfo=} */ exportsInfo;
  71. for (let i = 0; i < ids.length; i++) {
  72. if (i === 0 && ids[i] === "default") {
  73. continue; // ExportInfo for the next level under default is still at the root ExportsInfo, so don't advance currentExportsInfo
  74. }
  75. const exportInfo = currentExportsInfo.getExportInfo(ids[i]);
  76. if (exportInfo.provided === false) {
  77. // json imports have nested ExportInfo for elements that things that are not actually exported, so check .provided
  78. trimmedIds = ids.slice(0, i);
  79. break;
  80. }
  81. const nestedInfo = exportInfo.getNestedExportsInfo();
  82. if (!nestedInfo) {
  83. // once all nested exports are traversed, the next item is the actual import so stop there
  84. trimmedIds = ids.slice(0, i + 1);
  85. break;
  86. }
  87. currentExportsInfo = nestedInfo;
  88. }
  89. // Never trim to nothing. This can happen for invalid imports (e.g. import { notThere } from "./module", or import { anything } from "./missingModule")
  90. return trimmedIds.length ? trimmedIds : ids;
  91. }