FlagAllModulesAsUsedPlugin.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. /** @typedef {import("./Compiler")} Compiler */
  8. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  9. class FlagAllModulesAsUsedPlugin {
  10. constructor(explanation) {
  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.compilation.tap(
  20. "FlagAllModulesAsUsedPlugin",
  21. compilation => {
  22. const moduleGraph = compilation.moduleGraph;
  23. compilation.hooks.optimizeDependencies.tap(
  24. "FlagAllModulesAsUsedPlugin",
  25. modules => {
  26. /** @type {RuntimeSpec} */
  27. let runtime = undefined;
  28. for (const [name, { options }] of compilation.entries) {
  29. runtime = mergeRuntimeOwned(
  30. runtime,
  31. getEntryRuntime(compilation, name, options)
  32. );
  33. }
  34. for (const module of modules) {
  35. const exportsInfo = moduleGraph.getExportsInfo(module);
  36. exportsInfo.setUsedInUnknownWay(runtime);
  37. moduleGraph.addExtraReason(module, this.explanation);
  38. if (module.factoryMeta === undefined) {
  39. module.factoryMeta = {};
  40. }
  41. module.factoryMeta.sideEffectFree = false;
  42. }
  43. }
  44. );
  45. }
  46. );
  47. }
  48. }
  49. module.exports = FlagAllModulesAsUsedPlugin;