MixedExportsPlugin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const MixedExportsWarning = require("../errors/MixedExportsWarning");
  7. const { compareStrings } = require("../util/comparators");
  8. /** @import { PerformanceOptions } from "../../declarations/WebpackOptions" */
  9. /** @import Compiler from "../Compiler" */
  10. /** @import { MixedExportsDetails } from "../errors/MixedExportsWarning" */
  11. const PLUGIN_NAME = "MixedExportsPlugin";
  12. // Enough to name the offenders without listing every entry.
  13. const MAX_REPORTED_ENTRIES = 5;
  14. // Only these hand the namespace object straight to `require()`, so only here
  15. // does a default sitting beside named exports change what a consumer receives.
  16. const AMBIGUOUS_LIBRARY_TYPES = new Set([
  17. "commonjs",
  18. "commonjs2",
  19. "commonjs-module",
  20. "commonjs-static"
  21. ]);
  22. class MixedExportsPlugin {
  23. /**
  24. * Creates an instance of MixedExportsPlugin.
  25. * @param {PerformanceOptions} options the plugin options
  26. */
  27. constructor(options) {
  28. /** @type {PerformanceOptions["hints"]} */
  29. this.hints = options.hints;
  30. }
  31. /**
  32. * Applies the plugin by registering its hooks on the compiler.
  33. * @param {Compiler} compiler the compiler instance
  34. * @returns {void}
  35. */
  36. apply(compiler) {
  37. const hints = this.hints;
  38. if (!hints) return;
  39. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  40. // `afterSeal` is past the hash, which folds every message into it — a
  41. // hint reported earlier would change the build's identity.
  42. compilation.hooks.afterSeal.tap(PLUGIN_NAME, () => {
  43. const { moduleGraph, outputOptions } = compilation;
  44. /** @type {Map<string, MixedExportsDetails[]>} */
  45. const byType = new Map();
  46. for (const [name, entry] of compilation.entries) {
  47. // An entry can carry its own library, which wins over the output one.
  48. const library = entry.options.library || outputOptions.library;
  49. const type = library && library.type;
  50. if (!type || !AMBIGUOUS_LIBRARY_TYPES.has(type)) continue;
  51. // Naming one export already resolves it, whichever it is.
  52. if (library.export) continue;
  53. // Only the last module of an entry reaches the consumer: every chunk
  54. // format renders the startup with `entries[entries.length - 1]`.
  55. const { dependencies } = entry;
  56. const exported = dependencies[dependencies.length - 1];
  57. if (!exported) continue;
  58. const module = moduleGraph.getModule(exported);
  59. if (!module) continue;
  60. const provided = moduleGraph
  61. .getExportsInfo(module)
  62. .getProvidedExports();
  63. // `true` means "everything", which says nothing about a default.
  64. if (!Array.isArray(provided)) continue;
  65. /** @type {string[]} */
  66. const named = [];
  67. let hasDefault = false;
  68. for (const it of provided) {
  69. if (it === "default") {
  70. hasDefault = true;
  71. } else {
  72. named.push(it);
  73. }
  74. }
  75. if (!hasDefault || named.length === 0) continue;
  76. const mixed = byType.get(type);
  77. const details = { name, named: named.sort(compareStrings) };
  78. if (mixed) {
  79. mixed.push(details);
  80. } else {
  81. byType.set(type, [details]);
  82. }
  83. }
  84. if (byType.size === 0) return;
  85. // One report per library type, sorted so a rename cannot reorder them.
  86. for (const type of [...byType.keys()].sort(compareStrings)) {
  87. const mixed = /** @type {MixedExportsDetails[]} */ (byType.get(type));
  88. mixed.sort((a, b) => compareStrings(a.name, b.name));
  89. const warning = new MixedExportsWarning(
  90. mixed.slice(0, MAX_REPORTED_ENTRIES),
  91. type
  92. );
  93. if (hints === "error") {
  94. compilation.errors.push(warning);
  95. } else if (hints === "stats") {
  96. compilation.hints.push(warning);
  97. } else {
  98. compilation.warnings.push(warning);
  99. }
  100. }
  101. });
  102. });
  103. }
  104. }
  105. module.exports = MixedExportsPlugin;