-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmadge.js
62 lines (56 loc) · 1.41 KB
/
madge.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
// scripts/detect-unused.js
const madge = require("madge");
const path = require("path");
// Ref:https://github.com/vercel/next.js/discussions/10655
function pruneTree(subtree, tree) {
if (!subtree || subtree.length === 0) return;
for (let child of subtree) {
const nextSubtree = tree[child];
if (tree[child]) {
delete tree[child];
}
pruneTree(nextSubtree, tree);
}
}
madge(path.join(__dirname), {
baseDir: path.join(__dirname),
excludeRegExp: [
/^\.next[\\/]/,
/^next\.config\.js/,
/^public[\\/]/,
/^scripts[\\/]/,
/^archive[\\/]/,
/^coverage[\\/]/,
/^cypress[\\/]/,
/\.spec\.js/,
/\.config\.js/,
/top-banner/,
/mock-session/,
"madge.js",
"redirects.js",
"lighthouserc.js",
"lighthouse-paths.js",
"jest-setup.js",
"plopfile.js",
".eslintrc.js",
],
}).then((res) => {
const tree = res.obj();
const entrypoints = Object.keys(tree).filter(
(e) => e.startsWith("pages/") || e.startsWith("pages\\")
);
pruneTree(entrypoints, tree);
const unusedFiles = Object.keys(tree);
if (unusedFiles.length) {
console.log(
`⚠️ Found ${unusedFiles.length} files that no one is depending on, please consider removing:`
);
unusedFiles.forEach((file) => {
console.log("\x1b[33m%s\x1b[0m", file);
});
process.exit(1);
} else {
console.log("🎉 No used files!");
process.exit(0);
}
});