forked from DustinBrett/daedalOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminifyJs.js
More file actions
39 lines (33 loc) · 942 Bytes
/
minifyJs.js
File metadata and controls
39 lines (33 loc) · 942 Bytes
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 { readdirSync, readFileSync, statSync, writeFileSync } = require("fs");
const { minify } = require("terser");
const { extname, join } = require("path");
const OUT_PATH = "out";
const JS_MINIFIER_CONFIG = {
compress: true,
ecma: 2021,
mangle: true,
output: {
comments: false,
},
sourceMap: false,
};
const minifyJsFiles = (path) =>
Promise.all(
readdirSync(path).map(async (entry) => {
const fullPath = join(path, entry);
const stats = statSync(fullPath);
if (stats.isDirectory()) {
minifyJsFiles(fullPath);
} else if (extname(entry).toLowerCase() === ".js") {
const js = readFileSync(fullPath);
const { code: minifiedJs, error } = await minify(
js.toString(),
JS_MINIFIER_CONFIG
);
if (!error && minifiedJs?.length > 0) {
writeFileSync(fullPath, minifiedJs);
}
}
})
);
minifyJsFiles(OUT_PATH);