FlagEntryExportAsUsedPlugin.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. class FlagEntryExportAsUsedPlugin {
  9. constructor(nsObjectUsed, explanation) {
  10. this.nsObjectUsed = nsObjectUsed;
  11. this.explanation = explanation;
  12. }
  13. /**
  14. * Apply the plugin
  15. * @param {Compiler} compiler the compiler instance
  16. * @returns {void}
  17. */
  18. apply(compiler) {
  19. compiler.hooks.thisCompilation.tap(
  20. "FlagEntryExportAsUsedPlugin",
  21. compilation => {
  22. const moduleGraph = compilation.moduleGraph;
  23. compilation.hooks.seal.tap("FlagEntryExportAsUsedPlugin", () => {
  24. for (const [
  25. entryName,
  26. { dependencies: deps, options }
  27. ] of compilation.entries) {
  28. const runtime = getEntryRuntime(compilation, entryName, options);
  29. for (const dep of deps) {
  30. const module = moduleGraph.getModule(dep);
  31. if (module) {
  32. const exportsInfo = moduleGraph.getExportsInfo(module);
  33. if (this.nsObjectUsed) {
  34. exportsInfo.setUsedInUnknownWay(runtime);
  35. } else {
  36. exportsInfo.setAllKnownExportsUsed(runtime);
  37. }
  38. moduleGraph.addExtraReason(module, this.explanation);
  39. }
  40. }
  41. }
  42. });
  43. }
  44. );
  45. }
  46. }
  47. module.exports = FlagEntryExportAsUsedPlugin;