Blame view

node_modules/webpack-bundle-analyzer/src/tree/BaseFolder.js 1.85 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  import _ from 'lodash';
  
  import Node from './Node';
  
  export default class BaseFolder extends Node {
  
    constructor(name, parent) {
      super(name, parent);
      this.children = Object.create(null);
    }
  
    get src() {
      if (!_.has(this, '_src')) {
        this._src = this.walk((node, src) => (src += node.src || ''), '', false);
      }
  
      return this._src;
    }
  
    get size() {
      if (!_.has(this, '_size')) {
        this._size = this.walk((node, size) => (size + node.size), 0, false);
      }
  
      return this._size;
    }
  
    getChild(name) {
      return this.children[name];
    }
  
    addChildModule(module) {
      const { name } = module;
      const currentChild = this.children[name];
  
      // For some reason we already have this node in children and it's a folder.
      if (currentChild && currentChild instanceof BaseFolder) return;
  
      if (currentChild) {
        // We already have this node in children and it's a module.
        // Merging it's data.
        currentChild.mergeData(module.data);
      } else {
        // Pushing new module
        module.parent = this;
        this.children[name] = module;
      }
  
      delete this._size;
      delete this._src;
    }
  
    addChildFolder(folder) {
      folder.parent = this;
      this.children[folder.name] = folder;
      delete this._size;
      delete this._src;
  
      return folder;
    }
  
    walk(walker, state = {}, deep = true) {
      let stopped = false;
  
      _.each(this.children, child => {
        if (deep && child.walk) {
          state = child.walk(walker, state, stop);
        } else {
          state = walker(child, state, stop);
        }
  
        if (stopped) return false;
      });
  
      return state;
  
      function stop(finalState) {
        stopped = true;
        return finalState;
      }
    }
  
    toChartData() {
      return {
        label: this.name,
        path: this.path,
        statSize: this.size,
        groups: _.invokeMap(this.children, 'toChartData')
      };
    }
  
  };