You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FlagDependencyUsagePlugin.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Module")} Module */
  7. /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
  8. /** @typedef {false | true | string[]} UsedExports */
  9. const addToSet = (a, b) => {
  10. for (const item of b) {
  11. if (!a.includes(item)) a.push(item);
  12. }
  13. return a;
  14. };
  15. const isSubset = (biggerSet, subset) => {
  16. if (biggerSet === true) return true;
  17. if (subset === true) return false;
  18. return subset.every(item => biggerSet.indexOf(item) >= 0);
  19. };
  20. class FlagDependencyUsagePlugin {
  21. apply(compiler) {
  22. compiler.hooks.compilation.tap("FlagDependencyUsagePlugin", compilation => {
  23. compilation.hooks.optimizeDependencies.tap(
  24. "FlagDependencyUsagePlugin",
  25. modules => {
  26. const processModule = (module, usedExports) => {
  27. module.used = true;
  28. if (module.usedExports === true) return;
  29. if (usedExports === true) {
  30. module.usedExports = true;
  31. } else if (Array.isArray(usedExports)) {
  32. const old = module.usedExports ? module.usedExports.length : -1;
  33. module.usedExports = addToSet(
  34. module.usedExports || [],
  35. usedExports
  36. );
  37. if (module.usedExports.length === old) {
  38. return;
  39. }
  40. } else if (Array.isArray(module.usedExports)) {
  41. return;
  42. } else {
  43. module.usedExports = false;
  44. }
  45. // for a module without side effects we stop tracking usage here when no export is used
  46. // This module won't be evaluated in this case
  47. if (module.factoryMeta.sideEffectFree) {
  48. if (module.usedExports === false) return;
  49. if (
  50. Array.isArray(module.usedExports) &&
  51. module.usedExports.length === 0
  52. )
  53. return;
  54. }
  55. queue.push([module, module, module.usedExports]);
  56. };
  57. const processDependenciesBlock = (module, depBlock, usedExports) => {
  58. for (const dep of depBlock.dependencies) {
  59. processDependency(module, dep);
  60. }
  61. for (const variable of depBlock.variables) {
  62. for (const dep of variable.dependencies) {
  63. processDependency(module, dep);
  64. }
  65. }
  66. for (const block of depBlock.blocks) {
  67. queue.push([module, block, usedExports]);
  68. }
  69. };
  70. const processDependency = (module, dep) => {
  71. const reference = compilation.getDependencyReference(module, dep);
  72. if (!reference) return;
  73. const referenceModule = reference.module;
  74. const importedNames = reference.importedNames;
  75. const oldUsed = referenceModule.used;
  76. const oldUsedExports = referenceModule.usedExports;
  77. if (
  78. !oldUsed ||
  79. (importedNames &&
  80. (!oldUsedExports || !isSubset(oldUsedExports, importedNames)))
  81. ) {
  82. processModule(referenceModule, importedNames);
  83. }
  84. };
  85. for (const module of modules) {
  86. module.used = false;
  87. }
  88. /** @type {[Module, DependenciesBlock, UsedExports][]} */
  89. const queue = [];
  90. for (const preparedEntrypoint of compilation._preparedEntrypoints) {
  91. if (preparedEntrypoint.module) {
  92. processModule(preparedEntrypoint.module, true);
  93. }
  94. }
  95. while (queue.length) {
  96. const queueItem = queue.pop();
  97. processDependenciesBlock(queueItem[0], queueItem[1], queueItem[2]);
  98. }
  99. }
  100. );
  101. });
  102. }
  103. }
  104. module.exports = FlagDependencyUsagePlugin;