-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
39 lines (39 loc) · 1.07 KB
/
build.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 br = `
`;
(async () => {
const { minify } = require('terser');
const fs = require('fs');
const fsp = require('fs/promises');
const events = require('events');
const readline = require('readline');
const { transform } = require('@babel/core/lib/index');
const tran = (s, o) => new Promise((r, e) => transform(s, o, (n, d) => n ? e(n) : r(d.code)));
/**@type {import('terser').MinifyOptions} */
const opt = {
compress: true,
mangle: true,
};
const sourceArr = [];
const cmtArr = [];
let r = (s) => s[0] === ' ' || s[0] === '/' ? cmtArr.push(s) : (r = (s) => sourceArr.push(s), r(s));
await events.once(
readline.createInterface(fs.createReadStream('./source.js')).on('line', r),
'close',
);
const cmt = cmtArr.join(br) + br;
const source = sourceArr.join(br);
await fsp.writeFile('./main.js', cmt + (await minify(
{
'source.js': `(()=>{${source}})();`,
},
opt,
)).code);
await fsp.writeFile('./es3.js', cmt + (await minify(
{
'es3.js': `!function(){${await tran(source, {
"presets": ["@babel/preset-env"],
})}}();`
},
opt,
)).code);
})();