/
index.js
160 lines (135 loc) · 4.94 KB
/
index.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
var _ = require('lodash');
var assert = require('assert');
function WebpackDependencyStats (webpackStats, options) {
assert(typeof options === 'object' && options.srcFolder, 'Source root folder is required');
options = _.extend({
onlyLocal: true
}, options);
this.cache = {
dependencies: {},
dependents: {}
};
this.moduleNames = {};
this.allModules = {};
this.filteredModules = this.extractModuleData(webpackStats, options);
this.contextHelpers = WebpackDependencyStats.getContextHelperNames(this.filteredModules);
}
WebpackDependencyStats.prototype.getAllDependencies = function () {
if (Object.keys(this.cache.dependencies).length) {
return this.cache.dependencies;
}
var moduleIds = Object.keys(this.filteredModules.byId).map((id) => this.filteredModules.byId[id].id);
moduleIds.forEach((id) => { this.cache.dependencies[id] = []; });
moduleIds
.map((id) => ({ id: id, depenedents: this.getDependentIdsById(id) }))
.forEach((dependentsMap) => dependentsMap.depenedents
.forEach((id) => this.cache.dependencies[id] && this.cache.dependencies[id].push(dependentsMap.id)));
moduleIds.forEach((id) => {
this.cache.dependencies[id].sort();
this.cache.dependencies[id] = _.sortedUniq(_.differenceWith(this.cache.dependencies[id], this.contextHelpers.ids));
});
return this.cache.dependencies;
};
WebpackDependencyStats.prototype.getModuleNames = function () {
return Object.keys(this.filteredModules.byId).map((id) => this.getModuleNameById(id));
};
WebpackDependencyStats.prototype.getDependencies = function (moduleName) {
return this.getDependencyIds(moduleName)
.map((id) => this.getModuleNameById(id));
};
WebpackDependencyStats.prototype.getDependents = function (moduleName) {
return this.getDependentIds(moduleName)
.map((id) => this.getModuleNameById(id));
};
WebpackDependencyStats.prototype.getDependencyIds = function (moduleName) {
var module = this.filteredModules.byName[moduleName];
if (!module) {
throw new Error('Module "' + moduleName + '" was not found in webpack stats');
}
return this.getDependencyIdsById(module.id);
};
WebpackDependencyStats.prototype.getDependentIds = function (moduleName) {
var module = this.filteredModules.byName[moduleName];
if (!module) {
throw new Error('Module "' + moduleName + '" was not found in webpack stats');
}
return this.getDependentIdsById(module.id);
};
WebpackDependencyStats.prototype.getDependencyIdsById = function (id) {
return this.getAllDependencies()[id];
};
WebpackDependencyStats.prototype.getDependentIdsById = function (id) {
var module = this.filteredModules.byId[id];
if (!module) {
throw new Error('Module "' + id + '" was not found in webpack stats');
}
if (this.cache.dependents[id]) {
return this.cache.dependents[id];
}
var dependents = [];
this.cache.dependents[id] = dependents;
/**
* Add dependent id but keep the array sorted
*/
function addDependent (id) {
dependents.splice(_.sortedIndex(dependents, id), 0, id);
}
module.reasons.forEach((reason) => {
if (this.filteredModules.byId[reason.moduleId]) {
addDependent(reason.moduleId);
this.getDependentIdsById(reason.moduleId).forEach(addDependent);
}
});
return _.differenceWith(_.sortedUniq(dependents), this.contextHelpers.ids);
};
WebpackDependencyStats.prototype.getModuleNameById = function (id) {
if (!this.moduleNames[id]) {
var name = this.allModules[id].name;
this.moduleNames[id] = name
.replace(/^.+[!]/, '')
.replace(/^.+[~]/, '~');
}
return this.moduleNames[id];
};
/**
* Extracts the real modules from webpackStats
*/
WebpackDependencyStats.prototype.extractModuleData = function (webpackStats, options) {
options = _.extend({
onlyLocal: true
}, options);
var stats = webpackStats.toJson({
context: options.srcFolder,
source: false,
timings: false,
version: false,
errorDetails: false,
chunks: false,
chunkModules: false
});
var modules = { byId: {}, byName: {} };
stats.modules
.forEach((module) => {
this.allModules[module.id] = module;
});
stats.modules
// Remove internal modules
.filter((module) => module.name.indexOf('(webpack)') === -1)
// Remove files outside the src folder
.filter((module) => options.onlyLocal
? this.getModuleNameById(module.id).substr(0, 2) === './'
: ['~', '.'].indexOf(this.getModuleNameById(module.id).substr(0, 1)) !== -1)
.forEach((module) => {
modules.byName[this.getModuleNameById(module.id)] = module;
modules.byId[module.id] = module;
});
return modules;
};
WebpackDependencyStats.getContextHelperNames = function (modules) {
var names = Object.keys(modules.byName)
.filter((moduleName) => modules.byName[moduleName].reasons.some((reason) => reason.type === 'require.context'));
var ids = names.map((name) => modules.byName[name].id);
return { names: names, ids: ids };
};
module.exports = WebpackDependencyStats;