get-all-modules.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. let ConcatenatedModule;
  2. try {
  3. // eslint-disable-next-line global-require,import/no-unresolved,import/no-extraneous-dependencies
  4. ConcatenatedModule = require('webpack/lib/optimize/ConcatenatedModule');
  5. // eslint-disable-next-line no-empty
  6. } catch (e) {}
  7. /**
  8. * Get all modules from main & child compilations.
  9. * Merge modules from ConcatenatedModule (when webpack.optimize.ModuleConcatenationPlugin is used)
  10. * @param {Compilation} compilation
  11. * @return {NormalModule[]}
  12. */
  13. function getAllModules(compilation) {
  14. let modules = compilation.modules;
  15. // Look up in child compilations
  16. if (compilation.children.length > 0) {
  17. const childModules = compilation.children.map(getAllModules)
  18. .reduce((acc, compilationModules) => acc.concat(compilationModules), []);
  19. modules = modules.concat(childModules);
  20. }
  21. // Merge modules from ConcatenatedModule
  22. if (ConcatenatedModule) {
  23. const concatenatedModules = modules
  24. .filter(m => m instanceof ConcatenatedModule)
  25. .reduce((acc, m) => {
  26. /**
  27. * @see https://git.io/v7XDu
  28. * In webpack@3.5.1 `modules` public property was removed
  29. * To workaround this private `_orderedConcatenationList` property is used to collect modules
  30. */
  31. const subModules = 'modules' in m
  32. ? m.modules
  33. : m._orderedConcatenationList.map(entry => entry.module);
  34. return acc.concat(subModules);
  35. }, []);
  36. if (concatenatedModules.length > 0) {
  37. modules = modules.concat(concatenatedModules);
  38. }
  39. }
  40. return modules;
  41. }
  42. module.exports = getAllModules;