-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.js
More file actions
179 lines (160 loc) · 4.08 KB
/
Copy pathbuild.js
File metadata and controls
179 lines (160 loc) · 4.08 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//Auto Build v2.2, Pecacheu 2026. GNU GPL v3
import { spawn } from 'child_process';
import fs from 'fs/promises';
import path from 'path';
import mHTML from '@minify-html/node';
import { htmlPlugin } from '@pecacheu/esbuild-plugin-html';
import C from 'chalk';
import * as esbuild from 'esbuild';
import { minify as mJS } from 'terser';
//Paths
const app = '/form.ts',
src = 'src',
dist = 'dist',
srcCli = src + '/web',
distCli = dist + '/web',
srcSrv = src + '/srv';
//Options
const Mode = process.argv[2],
Watch = Mode === 'watch',
Dev = Watch || Mode === 'dev',
Meta = Mode === 'meta',
log = console.log,
UTF = {encoding: 'utf8'},
R_NL = /\n$/,
R_SC = /;\n?(\/\/#.+)?$/,
minExt = ['.js', '.html'],
trimExt = ['.css', '.svg'],
_hFD = [],
jsOpts = {
ecma: 2020,
module: true,
format: {inline_script: false, comments: false},
mangle: {properties: {regex: /^[_#]/}},
compress: {passes: 2, arguments: true, keep_fargs: false, keep_infinity: true, unsafe: true}
},
htmlOpts = {
allow_optimal_entities: true,
allow_removing_spaces_between_attributes: true,
minify_css: true,
minify_js: true
},
htmlLoadOpts = {
scriptLoading: 'module'
},
esOpts = {
entryPoints: [srcCli + app],
bundle: true,
minify: !Dev,
sourcemap: Dev,
format: 'esm',
splitting: true,
metafile: Meta ? true : undefined,
loader: {
'.svg': 'file',
'.jpg': 'file',
'.woff': 'copy',
'.woff2': 'copy'
},
plugins: [],
target: `es${jsOpts.ecma}`,
outdir: distCli
};
//==== Minify ====
async function minify(pin, _, fn) {
let fin = `${pin}/${fn}`, ext = path.extname(fn), f;
const fls = fin.slice(dist.length + 1);
try {
if(fin.indexOf('.min') !== -1) return;
if(minExt.includes(ext)) { //Minify
if(ext === '.js') {
const map = `${fin}.map`;
f = await fs.readFile(fin, UTF);
await getSrc(map);
const out = await mJS(f, jsOpts);
f = out.code.replace(R_SC, '$1');
if(out.map) {
await fs.writeFile(map, out.map);
log(C.cyan(`- ${fls}.map`));
}
} else {
f = mHTML.minify(await fs.readFile(fin), htmlOpts);
}
await fs.writeFile(fin, f);
log(C.cyan(`- ${fls}`));
} else if(trimExt.includes(ext)) { //Trim
f = (await fs.readFile(fin, UTF)).replace(R_NL, '');
await fs.writeFile(fin, f);
log(C.magenta(`- ${fls}`));
} else { //Log
log(C.dim(`- ${fls}`));
}
} catch(e) {
log(C.red(`- ${fls}`));
throw e;
} finally {delete jsOpts.sourceMap}
}
async function getSrc(map) {
try {
jsOpts.sourceMap = {
content: await fs.readFile(map, UTF),
url: path.basename(map)
};
} catch(e) {}
}
function addHTML(pin, _, fn) {
if(fn.endsWith('.html') && fn[0] !== '+') _hFD.push({
filename: fn, htmlFile: `${pin}/${fn}`, ...htmlLoadOpts
});
}
//==== Support ====
async function mkdir(p) {
try {await fs.mkdir(p, true)} catch(e) {
if(e.code !== 'EEXIST') throw e;
}
}
async function rm(p) {
try {await fs.rm(p, {recursive: true})} catch(e) {
if(e.code !== 'ENOENT') throw e;
}
}
const run = cmd => new Promise((res, rej) => {
cmd = spawn(cmd, {shell: true, stdio: 'inherit'});
cmd.on('exit', c => c ? rej(`Exit Code ${c}`) : res());
cmd.on('error', rej);
});
async function recurse(func, pin, pout) {
let pl = [], d = await fs.readdir(pin, {withFileTypes: true});
if(pout) await mkdir(pout);
for(let f of d) {
if(f.isFile()) pl.push(func(pin, pout, f.name));
else if(f.isDirectory()) pl.push(recurse(func,
path.join(pin, f.name), pout && path.join(pout, f.name)));
}
await Promise.all(pl);
}
//==== Pipeline ====
if(!Dev) {
log(C.bgYellow('Clean'));
await rm(dist);
log(C.bgYellow('Build Server'));
await run(`npx tsc -p ${srcSrv}`);
}
await mkdir(srcCli);
log(C.bgYellow('Build Client'));
await recurse(addHTML, srcCli);
esOpts.plugins.push(htmlPlugin({files: _hFD}));
const ctx = await esbuild.context(esOpts);
if(Watch) {
await ctx.watch();
log('Watching for changes...');
} else {
const build = await ctx.rebuild();
if(Meta) await fs.writeFile('meta.json', JSON.stringify(build.metafile));
if(!Dev) {
log(C.bgYellow('Minify'));
await recurse(minify, dist);
}
log(C.green('Done!'));
await ctx.dispose();
}