ConcatenatedModule.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import invokeMap from 'lodash/invokeMap';
  2. import ContentFolder from './ContentFolder';
  3. import ContentModule from './ContentModule';
  4. import Module from './Module';
  5. import {getModulePathParts} from './utils';
  6. export default class ConcatenatedModule extends Module {
  7. constructor(name, data, parent) {
  8. super(name, data, parent);
  9. this.name += ' (concatenated)';
  10. this.children = Object.create(null);
  11. this.fillContentModules();
  12. }
  13. get parsedSize() {
  14. return this.getParsedSize() ?? this.getEstimatedSize('parsedSize');
  15. }
  16. get gzipSize() {
  17. return this.getGzipSize() ?? this.getEstimatedSize('gzipSize');
  18. }
  19. getEstimatedSize(sizeType) {
  20. const parentModuleSize = this.parent[sizeType];
  21. if (parentModuleSize !== undefined) {
  22. return Math.floor((this.size / this.parent.size) * parentModuleSize);
  23. }
  24. return undefined;
  25. }
  26. fillContentModules() {
  27. this.data.modules.forEach(moduleData => this.addContentModule(moduleData));
  28. }
  29. addContentModule(moduleData) {
  30. const pathParts = getModulePathParts(moduleData);
  31. if (!pathParts) {
  32. return;
  33. }
  34. const [folders, fileName] = [pathParts.slice(0, -1), pathParts[pathParts.length - 1]];
  35. let currentFolder = this;
  36. folders.forEach(folderName => {
  37. let childFolder = currentFolder.getChild(folderName);
  38. if (!childFolder) {
  39. childFolder = currentFolder.addChildFolder(new ContentFolder(folderName, this));
  40. }
  41. currentFolder = childFolder;
  42. });
  43. const ModuleConstructor = moduleData.modules ? ConcatenatedModule : ContentModule;
  44. const module = new ModuleConstructor(fileName, moduleData, this);
  45. currentFolder.addChildModule(module);
  46. }
  47. getChild(name) {
  48. return this.children[name];
  49. }
  50. addChildModule(module) {
  51. module.parent = this;
  52. this.children[module.name] = module;
  53. }
  54. addChildFolder(folder) {
  55. folder.parent = this;
  56. this.children[folder.name] = folder;
  57. return folder;
  58. }
  59. mergeNestedFolders() {
  60. invokeMap(this.children, 'mergeNestedFolders');
  61. }
  62. toChartData() {
  63. return {
  64. ...super.toChartData(),
  65. concatenated: true,
  66. groups: invokeMap(this.children, 'toChartData'),
  67. };
  68. }
  69. }