e82c49530367a2bfeaccf25ca5752b4a82b0ccd358a09aef57d913e6a1cfa703d032768c0552e1eb177d0d8f2acecfb98b8a3b5cf62fe34ee078fc0c33a06d 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import _ from 'lodash';
  2. import gzipSize from 'gzip-size';
  3. import Module from './Module';
  4. import BaseFolder from './BaseFolder';
  5. import ConcatenatedModule from './ConcatenatedModule';
  6. import {getModulePathParts} from './utils';
  7. export default class Folder extends BaseFolder {
  8. get parsedSize() {
  9. return this.src ? this.src.length : 0;
  10. }
  11. get gzipSize() {
  12. if (!_.has(this, '_gzipSize')) {
  13. this._gzipSize = this.src ? gzipSize.sync(this.src) : 0;
  14. }
  15. return this._gzipSize;
  16. }
  17. addModule(moduleData) {
  18. const pathParts = getModulePathParts(moduleData);
  19. if (!pathParts) {
  20. return;
  21. }
  22. const [folders, fileName] = [pathParts.slice(0, -1), _.last(pathParts)];
  23. let currentFolder = this;
  24. _.each(folders, folderName => {
  25. let childNode = currentFolder.getChild(folderName);
  26. if (
  27. // Folder is not created yet
  28. !childNode ||
  29. // In some situations (invalid usage of dynamic `require()`) webpack generates a module with empty require
  30. // context, but it's moduleId points to a directory in filesystem.
  31. // In this case we replace this `File` node with `Folder`.
  32. // See `test/stats/with-invalid-dynamic-require.json` as an example.
  33. !(childNode instanceof Folder)
  34. ) {
  35. childNode = currentFolder.addChildFolder(new Folder(folderName));
  36. }
  37. currentFolder = childNode;
  38. });
  39. const ModuleConstructor = moduleData.modules ? ConcatenatedModule : Module;
  40. const module = new ModuleConstructor(fileName, moduleData, this);
  41. currentFolder.addChildModule(module);
  42. }
  43. toChartData() {
  44. return {
  45. ...super.toChartData(),
  46. parsedSize: this.parsedSize,
  47. gzipSize: this.gzipSize
  48. };
  49. }
  50. };