-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
160 lines (140 loc) · 4.23 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
const fs = require('fs');
const http = require('http');
const opener = require('opener');
const path = require('path');
const resolve = require("enhanced-resolve");
let myResolve;
function renderViewer(jsonString) {
return new Promise((resolve) => {
fs.readFile(path.resolve(__dirname, './dependencies.html'), 'utf-8', (err, data) => {
if (err) throw err;
const html = data.replace(/<%=(\w+)%>/g, (match, $1) => jsonString);
resolve(html);
});
});
}
function openBrowser(url, info) {
try {
opener(url);
console.log(info);
} catch (err) {
console.error(`Opener failed to open "${url}":\n${err}`);
}
}
async function startServer(jsonString) {
const port = 8888;
const host = '127.0.0.1';
const isOpenBrowser = true;
const html = await renderViewer(jsonString);
http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html);
} else {
res.end('blank page');
}
}).listen(port, host, () => {
const url = `http://${host}:${port}`;
const logInfo = (
`Webpack Source Code Dependencies Analyzer is started at ${(url)}\n`
+ `Use ${('Ctrl+C')} to close it`
);
if (isOpenBrowser) {
openBrowser(url, logInfo);
}
});
}
const transformArrayToTree = (array) => {
if (!array || array?.length === 0) return null;
array.forEach(v => {
(v.children || []).forEach(k => {
const childDep = array.find(l => l.resource === k.resource);
childDep && (k.children = childDep.children);
})
})
return array[0];
};
/**
* 通过source获取真实文件路径
* @param parser
* @param source
*/
function getResource(parser, source){
if(!myResolve){
myResolve = resolve.create.sync(parser.state.options.resolve);
}
return myResolve(parser.state.current.context,source);
}
class WebpackCodeDependenciesAnalysis {
constructor() {
this.pluginName = 'WebpackCodeDependenciesAnalysisPlugin';
// 文件数组
this.files = [];
// 当前编译的文件
this.currentFile = null;
}
apply(compiler) {
compiler.hooks.compilation.tap(this.pluginName, (compilation, { normalModuleFactory }) => {
const collectFile = (parser) => {
const { rawRequest, resource } = parser.state.current;
if (resource !== this.currentFile) {
this.currentFile = resource;
this.files.push({
name: rawRequest,
resource,
children: []
});
}
}
const handler = (parser, options) => {
parser.hooks.importCall.tap(this.pluginName, (expr) => {
// 跳过node_modules
if (parser.state.current.resource.includes('node_modules')) {
return;
}
collectFile(parser);
let ast = {};
const isWebpack5 = "webpack" in compiler;
// webpack@5 has webpack property, webpack@4 don't have the property
if(isWebpack5){
// webpack@5
ast = expr.source;
} else {
//webpack@4
const { arguments: arg } = expr;
ast = arg[0];
}
const { type, value } = ast;
if (type === 'Literal') {
const resource = getResource(parser, value);
this.files[this.files.length - 1].children.push({
name: value,
resource
});
}
})
parser.hooks.import.tap(this.pluginName, (statement, source) => {
if (parser.state.current.resource.includes('node_modules')
) {
return;
}
collectFile(parser);
this.files[this.files.length - 1].children.push({
name: source,
resource: getResource(parser, source)
});
});
}
normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap(this.pluginName, handler);
});
compiler.hooks.make.tap(this.pluginName, (compilation) => {
compilation.hooks.finishModules.tap(this.pluginName, (modules) => {
// const tree = transformArrayToTree(this.files);
startServer(JSON.stringify(this.files));
});
});
}
}
module.exports = WebpackCodeDependenciesAnalysis;