This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
executable file
·127 lines (107 loc) · 3.16 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
#!/usr/bin/env node
const madge = require('madge')
const path = require('path')
const fs = require('fs')
const packageJSON = require(path.resolve(process.cwd(), 'package.json'))
try {
main()
} catch (e) {
console.error('Error while running next-unused:', e)
}
function main() {
const userConfig = packageJSON['next-unused'] || {}
const {
debug,
include = [],
exclude = [],
entrypoints: userEntrypoints = []
} = userConfig
if (debug) {
if (include.length) {
console.log(`[debug] Found include config: ${include}`)
}
if (exclude.length) {
console.log(`[debug] Found exclude config: ${exclude}`)
}
if (userEntrypoints.length) {
console.log(`[debug] Found entrypoints config: ${userEntrypoints}`)
}
}
for (const entrypoint of userEntrypoints) {
if (!include.includes(entrypoint)) {
include.push(entrypoint)
if (debug) {
console.log(
`[debug] Entrypoint "${entrypoint}" is missing in includes, adding it`
)
}
}
}
// Automatically include top level pages/ directory by default
const topLevelPagesPath = path.resolve(process.cwd(), 'pages')
const hasTopLevelPages = fs.existsSync(topLevelPagesPath)
const madgePath = []
if (hasTopLevelPages) {
if (!include.includes('pages')) {
include.push('pages')
}
}
include.forEach((inc) => {
madgePath.push(path.resolve(process.cwd(), inc))
})
// Negative lookahead – ignore searching for any files
// that aren't part of our include list
const searchDirs = new RegExp(`^(?!(${include.join('|')}))`, 'i')
if (debug === true) {
console.log(`[debug] Using exclude regex: ${searchDirs} and ${exclude}`)
}
if (debug === true) {
console.log(
`[debug] Searching directories: ${JSON.stringify(madgePath, null, 2)}`
)
}
const tsConfigPath = path.resolve(process.cwd(), 'tsconfig.json')
madge(process.cwd(), {
webpackConfig: path.resolve(__dirname, 'madge.webpack.js'),
tsConfig: fs.existsSync(tsConfigPath) ? tsConfigPath : undefined,
excludeRegExp: [searchDirs, ...exclude],
fileExtensions: ['js', 'jsx', 'ts', 'tsx']
}).then((res) => {
const tree = res.obj()
const entrypoints = Object.keys(tree).filter((f) => {
return (
f.startsWith('pages/') ||
userEntrypoints.some((x) => f.startsWith(x.includes('/') ? x : `${x}/`))
)
})
if (!entrypoints.length) {
return console.log('No entrypoints found.')
}
if (debug) {
console.log('[debug] Found entrypoints')
console.log(entrypoints)
}
pruneTree(entrypoints, tree)
const unusedFilenames = Object.keys(tree)
if (!unusedFilenames.length) {
console.log('No unused files!')
} else {
console.log(
`Found ${unusedFilenames.length} unused ${
unusedFilenames.length === 1 ? 'file' : 'files'
}:`
)
unusedFilenames.map((f) => console.log(f))
}
})
}
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)
}
}