FlagEntryExportAsUsedPlugin.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. /**
  11. * Creates an instance of FlagEntryExportAsUsedPlugin.
  12. * @param {boolean} nsObjectUsed true, if the ns object is used
  13. * @param {string} explanation explanation for the reason
  14. */
  15. constructor(nsObjectUsed, explanation) {
  16. this.nsObjectUsed = nsObjectUsed;
  17. this.explanation = explanation;
  18. }
  19. /**
  20. * Applies the plugin by registering its hooks on the compiler.
  21. * @param {Compiler} compiler the compiler instance
  22. * @returns {void}
  23. */
  24. apply(compiler) {
  25. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  26. const moduleGraph = compilation.moduleGraph;
  27. compilation.hooks.seal.tap(PLUGIN_NAME, () => {
  28. for (const [
  29. entryName,
  30. { dependencies: deps, options }
  31. ] of compilation.entries) {
  32. const runtime = getEntryRuntime(compilation, entryName, options);
  33. for (const dep of deps) {
  34. const module = moduleGraph.getModule(dep);
  35. if (module) {
  36. const exportsInfo = moduleGraph.getExportsInfo(module);
  37. if (this.nsObjectUsed) {
  38. exportsInfo.setUsedInUnknownWay(runtime);
  39. } else {
  40. exportsInfo.setAllKnownExportsUsed(runtime);
  41. }
  42. moduleGraph.addExtraReason(module, this.explanation);
  43. }
  44. }
  45. }
  46. });
  47. });
  48. }
  49. }
  50. module.exports = FlagEntryExportAsUsedPlugin;