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