FlagAllModulesAsUsedPlugin.js 1.6 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, 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. * Creates an instance of FlagAllModulesAsUsedPlugin.
  14. * @param {string} explanation explanation
  15. */
  16. constructor(explanation) {
  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. if (module.factoryMeta === undefined) {
  41. module.factoryMeta = {};
  42. }
  43. /** @type {FactoryMeta} */
  44. (module.factoryMeta).sideEffectFree = false;
  45. }
  46. });
  47. });
  48. }
  49. }
  50. module.exports = FlagAllModulesAsUsedPlugin;