FlagEntryExportAsUsedPlugin.js 1.6 KB

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