-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify.js
85 lines (80 loc) · 3.01 KB
/
minify.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// minify.js
var Terser = require("terser");
//To use esbuild: npm install esbuild and comment out line below, and comment out Terser line above
var esbuild;
try {
esbuild = require('esbuild');
}
catch (e) { }
var fs = require("fs");
var path = require("path");
function getAllFiles(dirPath, outPath, arrayOfFiles) {
let files = fs.readdirSync(dirPath);
outPath = outPath || dirPath;
arrayOfFiles = arrayOfFiles || [];
files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, outPath + "/" + file, arrayOfFiles);
} else {
arrayOfFiles.push([path.join(__dirname, dirPath, "/", file), path.join(__dirname, outPath, "/", path.parse(file).name + '.js')]);
}
});
return arrayOfFiles.filter(path => path[0].match(/\.(js|ts)$/));
}
async function minifyFiles(filePaths, verbose, config) {
let t = Date.now();
let min = process.argv.indexOf('-m') !== -1 || process.argv.indexOf('-miniify') !== -1 || process.argv.indexOf('--miniify') !== -1;
config = config || './tsconfig.json'
await Promise.all(filePaths.map(async (filePath) => {
if (verbose)
console.log(`${filePath[0]} -> ${filePath[1]}`);
if (process.argv.indexOf('-es') !== -1 || process.argv.indexOf('-esbuild') !== -1) {
if (!esbuild) return;
esbuild.buildSync({
entryPoints: [filePath[0]],
bundle: false,
outfile: filePath[1],
loader: { ".ts": "ts" },
minify: min,
tsconfig: config,
target: "es2020",
format: "cjs"
});
}
else
fs.writeFileSync(
filePath[1],
(await Terser.minify(fs.readFileSync(filePath[0], "utf8"), {
ecma: 2017,
format: {
comments: false,
ecma: 2017
}
})).code
);
//To use esbuild uncomment block below and comment out fs.write block above
/*
fs.writeFileSync(
filePath[1],
(esbuild.transformSync(fs.readFileSync(filePath[0], "utf8"), {
minify: min,
target: 'es2017',
format: "cjs"
})).code
);
*/
}));
console.log(`Completed in ${(Date.now() - t) / 1000} seconds`);
}
let files;
if (process.argv.indexOf('-es') !== -1 || process.argv.indexOf('-esbuild') !== -1) {
if (!esbuild) {
console.log('esbuild not installed');
return;
}
files = getAllFiles("./src/common", './build/js');
files.push(...getAllFiles("./src/worker", './build/js'));
}
else
files = getAllFiles("./out", './build/js');
minifyFiles(files, process.argv.indexOf('-v') !== -1 || process.argv.indexOf('-verbose') !== -1 || process.argv.indexOf('--verbose') !== -1, './src/worker/tsconfig.json').then(r => r);