-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-build.js
36 lines (30 loc) · 939 Bytes
/
post-build.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
// post-build.js
import fs from 'fs'
import path from 'path'
function isOnlyComments (content) {
const noInlineComments = content.replace(/\/\/.*/g, '').trim()
const noBlockComments = noInlineComments
.replace(/\/\*[\s\S]*?\*\//g, '')
.trim()
return noBlockComments.length === 0
}
function removeCommentOnlyFiles (directory) {
const files = fs.readdirSync(directory)
for (const file of files) {
const fullPath = path.join(directory, file)
const stat = fs.statSync(fullPath)
if (stat.isDirectory()) {
removeCommentOnlyFiles(fullPath)
} else if (fullPath.endsWith('.js')) {
const content = fs.readFileSync(fullPath, 'utf-8')
if (isOnlyComments(content)) {
fs.unlinkSync(fullPath)
// console.log(`Removed empty ${fullPath}`)
// if (fs.existsSync(mapPath)) {
// fs.unlinkSync(mapPath)
// }
}
}
}
}
removeCommentOnlyFiles('./dist')