FlagEntryExportAsUsedPlugin.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { getEntryRuntime } = require("./util/runtime");
  7. /** @typedef {import("./Compiler")} Compiler */
  8. const PLUGIN_NAME = "FlagEntryExportAsUsedPlugin";
  9. class FlagEntryExportAsUsedPlugin {
  10. constructor(nsObjectUsed, explanation) {
  11. this.nsObjectUsed = nsObjectUsed;
  12. this.explanation = explanation;
  13. }
  14. /**
  15. * Apply the plugin
  16. * @param {Compiler} compiler the compiler instance
  17. * @returns {void}
  18. */
  19. apply(compiler) {
  20. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
  21. const moduleGraph = compilation.moduleGraph;
  22. compilation.hooks.seal.tap(PLUGIN_NAME, () => {
  23. for (const [
  24. entryName,
  25. { dependencies: deps, options }
  26. ] of compilation.entries) {
  27. const runtime = getEntryRuntime(compilation, entryName, options);
  28. for (const dep of deps) {
  29. const module = moduleGraph.getModule(dep);
  30. if (module) {
  31. const exportsInfo = moduleGraph.getExportsInfo(module);
  32. if (this.nsObjectUsed) {
  33. exportsInfo.setUsedInUnknownWay(runtime);
  34. } else {
  35. exportsInfo.setAllKnownExportsUsed(runtime);
  36. }
  37. moduleGraph.addExtraReason(module, this.explanation);
  38. }
  39. }
  40. }
  41. });
  42. });
  43. }
  44. }
  45. module.exports = FlagEntryExportAsUsedPlugin;