FlagAllModulesAsUsedPlugin.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { getEntryRuntime, mergeRuntimeOwned } = require("./util/runtime");
  7. /** @import Compiler from "./Compiler" */
  8. /** @import { RuntimeSpec } from "./util/runtime" */
  9. const PLUGIN_NAME = "FlagAllModulesAsUsedPlugin";
  10. class FlagAllModulesAsUsedPlugin {
  11. /**
  12. * Creates an instance of FlagAllModulesAsUsedPlugin.
  13. * @param {string} explanation explanation
  14. */
  15. constructor(explanation) {
  16. /** @type {string} */
  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.compilation.tap(PLUGIN_NAME, (compilation) => {
  26. const moduleGraph = compilation.moduleGraph;
  27. compilation.hooks.optimizeDependencies.tap(PLUGIN_NAME, (modules) => {
  28. /** @type {RuntimeSpec} */
  29. let runtime;
  30. for (const [name, { options }] of compilation.entries) {
  31. runtime = mergeRuntimeOwned(
  32. runtime,
  33. getEntryRuntime(compilation, name, options)
  34. );
  35. }
  36. for (const module of modules) {
  37. const exportsInfo = moduleGraph.getExportsInfo(module);
  38. exportsInfo.setUsedInUnknownWay(runtime);
  39. moduleGraph.addExtraReason(module, this.explanation);
  40. }
  41. });
  42. });
  43. }
  44. }
  45. module.exports = FlagAllModulesAsUsedPlugin;