-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbuild-js.js
42 lines (39 loc) · 1.21 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
40
41
42
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(async (response) => {
try {
const dest = file.replace(/src\\/g, '')
await mkdir(dest.substring(0, dest.lastIndexOf('\\')), { recursive: true })
await copyFile(file, dest)
const data = await Terser.minify(response)
writeFile(`${dest.substr(0, dest.lastIndexOf('.'))}.min.js`, data.code)
} catch(error) {
console.log(error)
}
})
}
}
if (isMainThread) {
const worker = new Worker(__filename)
worker.postMessage('message')
} else {
parentPort.once('message', () => {
processJs()
})
}