-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.ts
99 lines (79 loc) · 2 KB
/
build.ts
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
// Publish:
// $ npm version <asd>
// $ git push origin main --tags
// #-> CI builds w/ publish
import { existsSync } from 'node:fs';
import { join, resolve } from 'node:path';
import oxc from 'npm:oxc-transform@^0.30';
import { minify } from 'npm:[email protected]';
const Quiet = Deno.args.includes('--quiet');
const src = resolve('src');
const outdir = resolve('npm');
const Inputs = [
'access.ts',
'find.ts',
'package.ts',
'resolve.ts',
'walk.ts',
];
function log(...args: unknown[]) {
Quiet || console.log(...args);
}
function bail(label: string, errors: string[]): never {
console.error('[%s] error(s)\n', label, errors.join(''));
Deno.exit(1);
}
function copy(file: string) {
if (existsSync(file)) {
log('> writing "%s" file', file);
return Deno.copyFile(file, join(outdir, file));
}
}
async function transform(filename: string) {
let entry = join(src, filename);
let source = await Deno.readTextFile(entry);
let xform = oxc.transform(entry, source, {
typescript: {
onlyRemoveTypeImports: true,
declaration: {
stripInternal: true,
},
},
});
if (xform.errors.length > 0) {
bail('transform', xform.errors);
}
let rgx = /\.tsx?$/;
let esm = filename.replace(rgx, '.mjs');
let dts = filename.replace(rgx, '.d.mts');
let outfile = join(outdir, dts);
log('> writing "%s" file', dts);
await Deno.writeTextFile(outfile, xform.declaration!);
outfile = join(outdir, esm);
log('> writing "%s" file', esm);
await Deno.writeTextFile(outfile, xform.code);
try {
let min = await minify(xform.code, {
ecma: 2020,
mangle: true,
compress: true,
toplevel: true,
module: true,
});
if (!min.code) throw 1;
log('::notice::%s (%d b)', esm, min.code.length);
} catch (err) {
bail('terser', err);
}
}
if (existsSync(outdir)) {
log('! removing "npm" directory');
await Deno.remove(outdir, { recursive: true });
}
await Deno.mkdir(outdir);
for (let name of Inputs) {
await transform(name);
}
await copy('package.json');
await copy('readme.md');
await copy('license');