Folder.js 1.7 KB

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