hasOnlyUnusedExports.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const { UsageState } = require("../ExportsInfo");
  7. /** @import ChunkGraph from "../ChunkGraph" */
  8. /** @import Module from "../Module" */
  9. /** @import ModuleGraph from "../ModuleGraph" */
  10. /**
  11. * Tells whether a module provides exports and no runtime uses any of them.
  12. * One exporting nothing is there for its side effects, a different mistake.
  13. * @param {Module} module the module to look up
  14. * @param {ModuleGraph} moduleGraph the module graph
  15. * @param {ChunkGraph} chunkGraph the chunk graph
  16. * @returns {boolean} true when it provides exports and none are used
  17. */
  18. const hasOnlyUnusedExports = (module, moduleGraph, chunkGraph) => {
  19. const runtimes = [...chunkGraph.getModuleRuntimes(module)];
  20. let provided = 0;
  21. for (const exportInfo of moduleGraph.getExportsInfo(module).exports) {
  22. if (!exportInfo.provided) continue;
  23. provided++;
  24. for (const runtime of runtimes) {
  25. // `NoInfo` included: unknown usage is not unused.
  26. if (exportInfo.getUsed(runtime) !== UsageState.Unused) return false;
  27. }
  28. }
  29. return provided > 0;
  30. };
  31. module.exports = hasOnlyUnusedExports;