-
Notifications
You must be signed in to change notification settings - Fork 10
/
build-js.js
35 lines (32 loc) · 1.01 KB
/
build-js.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
const { Worker, isMainThread, parentPort } = require('worker_threads')
const { readdir, readFile, writeFile } = require('fs').promises
const { resolve } = require('path')
const Terser = require('terser')
async function* recursiveSearch(dir) {
const dirents = await readdir(dir, { withFileTypes: true })
for (const dirent of dirents) {
const res = resolve(dir, dirent.name)
if (dirent.isDirectory()) {
yield* recursiveSearch(res)
} else if (!res.includes('.min.js')) {
yield res
}
}
}
async function processJs() {
for await (const file of recursiveSearch(`${__dirname}/js/`)) {
readFile(file, { encoding: 'utf8' })
.then(async (response) => {
const data = await Terser.minify(response, { output: { comments: false } })
writeFile(`${file.substr(0, file.lastIndexOf('.'))}.min.js`, data.code)
})
}
}
if (isMainThread) {
const worker = new Worker(__filename)
worker.postMessage('message')
} else {
parentPort.once('message', () => {
processJs()
})
}