FlagEntryExportAsUsedPlugin.js 1.5 KB

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