-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
74 lines (62 loc) · 1.85 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
import { Command } from 'cliffy';
import * as esbuild from 'esbuild';
import esbuildCachePlugin from 'esbuild-plugin-cache';
import * as path from 'std/path/mod.ts';
import { babelPlugin } from './plugins/babelPlugin.ts';
const srcPath = path.resolve('./src');
const destPath = path.resolve('./dist');
const examplePath = path.resolve('./example');
const lockMap = JSON.parse(await Deno.readTextFile('./deno.lock'));
const importMap = JSON.parse(await Deno.readTextFile('./import_map.json'));
const denoCacheDirectory = await esbuildCachePlugin.util.getDenoDir();
const { options } = await new Command()
.option('-d, --dev', 'development mode')
.option('-w, --watch', 'watch mode')
.option('-s, --serve', 'serve mode')
.parse(Deno.args);
const buildOptions = (dev = false, watch = false): esbuild.BuildOptions => ({
entryPoints: [
{ in: path.join(srcPath, 'mod.ts'), out: 'ghCards' },
],
bundle: true,
outdir: destPath,
platform: 'browser',
plugins: [
esbuildCachePlugin({
denoCacheDirectory,
lockMap,
importMap,
}),
babelPlugin({
filter: /\.js$/,
verbose: true,
}),
],
sourcemap: dev ? 'inline' : 'linked',
minify: !dev,
});
const config = buildOptions(options.dev, options.watch || options.serve);
if (!(options.watch || options.serve)) {
await esbuild.build(config);
Deno.exit();
}
const ctx = await esbuild.context(config);
await ctx.watch();
console.log('Watching...');
if (options.serve) {
const { host, port } = await ctx.serve({
servedir: config.outdir,
});
console.log(`Serving on ${host}:${port}`);
}
const decoder = new TextDecoder();
for await (const chunk of Deno.stdin.readable) {
const text = decoder.decode(chunk).trim();
if (text === 'r') {
await ctx.rebuild().catch(() => {});
} else if (text === 'q') {
await ctx.dispose();
break;
}
}
await esbuild.stop();