-
Notifications
You must be signed in to change notification settings - Fork 14
/
build-js.js
39 lines (36 loc) · 1.12 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
36
37
38
39
const { Worker, isMainThread, parentPort } = require('worker_threads')
const { readdir, readFile, writeFile, mkdir, copyFile } = 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 {
yield res
}
}
}
async function processJs() {
for await (const file of recursiveSearch(`${__dirname}/src/js/`)) {
readFile(file, { encoding: 'utf8' })
.then((data) => {
const dest = file.replace(/src\\/g, '')
mkdir(dest.substring(0, dest.lastIndexOf('\\')), { recursive: true })
.then(() => {
copyFile(file, dest)
writeFile(`${dest.substr(0, dest.lastIndexOf('.'))}.min.js`, Terser.minify(data).code)
})
})
}
}
if (isMainThread) {
const worker = new Worker(__filename)
worker.postMessage('message')
} else {
parentPort.once('message', () => {
processJs()
})
}