-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.js
39 lines (31 loc) · 972 Bytes
/
compile.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
/**
* A script to compress sources with Terser
*/
const fs = require('fs');
const path = require('path');
const util = require('util');
const terser = require('terser');
const distPath = path.resolve(process.cwd(), 'dist');
const readDir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const processFile = async filePath => {
const contents = await readFile(filePath, 'utf8');
const result = terser.minify(contents);
if (result.error) {
throw new Error(`Error in file ${filePath}: ${result.error}`);
}
await writeFile(filePath, result.code, 'utf8');
};
const main = async dir => {
// Only support one level at the moment
const files = await readDir(dir);
const jsFiles = files
.filter(file => file.endsWith('.js'))
.map(file => path.resolve(dir, file));
await Promise.all(jsFiles.map(processFile));
};
main(distPath).catch(e => {
console.error(e);
process.exit(1);
});