FlagAllModulesAsUsedPlugin.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. const PLUGIN_NAME = "FlagAllModulesAsUsedPlugin";
  10. class FlagAllModulesAsUsedPlugin {
  11. constructor(explanation) {
  12. this.explanation = explanation;
  13. }
  14. /**
  15. * Apply the plugin
  16. * @param {Compiler} compiler the compiler instance
  17. * @returns {void}
  18. */
  19. apply(compiler) {
  20. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  21. const moduleGraph = compilation.moduleGraph;
  22. compilation.hooks.optimizeDependencies.tap(PLUGIN_NAME, modules => {
  23. /** @type {RuntimeSpec} */
  24. let runtime = undefined;
  25. for (const [name, { options }] of compilation.entries) {
  26. runtime = mergeRuntimeOwned(
  27. runtime,
  28. getEntryRuntime(compilation, name, options)
  29. );
  30. }
  31. for (const module of modules) {
  32. const exportsInfo = moduleGraph.getExportsInfo(module);
  33. exportsInfo.setUsedInUnknownWay(runtime);
  34. moduleGraph.addExtraReason(module, this.explanation);
  35. if (module.factoryMeta === undefined) {
  36. module.factoryMeta = {};
  37. }
  38. module.factoryMeta.sideEffectFree = false;
  39. }
  40. });
  41. });
  42. }
  43. }
  44. module.exports = FlagAllModulesAsUsedPlugin;