-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.ts
93 lines (81 loc) · 2.52 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
// cSpell:ignore metafile,iife,outdir,servedir
import sade from 'sade';
import path from 'node:path';
import fs from 'node:fs';
import esbuild from 'esbuild';
import {
type BuildArgs,
CHANNELS,
DEV_DIR,
DIST_DIR,
options,
SERVE_PORTS,
type Target,
TARGETS,
} from '../esbuild/config';
import { getDevOptions } from '../esbuild/dev';
import { getProdOptions } from '../esbuild/prod';
sade('build [target]', true)
.option('--channel', `One of: ${CHANNELS.join(', ')}`, 'nightly')
.option('--dev', 'Dev-mode (watch, live-reload)', false)
.example('chrome --channel=nightly')
.example('firefox --channel=stable')
.describe([`\`target\` should be one of ${TARGETS.join(', ')}`])
.action(async (target: Target, opts: BuildArgs) => {
const options = { ...opts, target };
if (!CHANNELS.includes(options.channel)) {
console.warn(`Invalid --channel. Must be one of ${CHANNELS.join(', ')}`);
process.exit(1);
}
if (!options.target && !options.dev) {
console.log(`Building all targets with channel: ${options.channel}`);
return Promise.all(TARGETS.map((t) => build({ ...options, target: t })));
}
// Default to chrome in dev build
if (options.dev) {
options.target ||= 'chrome';
}
if (!TARGETS.includes(options.target)) {
console.warn(`Invalid --target. Must be one of ${TARGETS.join(', ')}`);
process.exit(1);
}
console.log(
`Building target: "${options.target}" with channel: "${options.channel}"`,
);
return options.dev ? buildWatch(options) : build(options);
})
.parse(process.argv);
async function build({ target, channel }: BuildArgs) {
const OUTPUT_DIR = path.join(DIST_DIR, target);
const result = await esbuild.build({
...options,
...getProdOptions({ outDir: OUTPUT_DIR, target, channel }),
outdir: OUTPUT_DIR,
});
if (result.metafile) {
fs.writeFileSync(
path.join(OUTPUT_DIR, 'meta.json'),
JSON.stringify(result.metafile),
);
}
}
async function buildWatch({ target, channel }: BuildArgs) {
const OUTPUT_DIR = path.join(DEV_DIR, target);
const ctx = await esbuild.context({
...options,
...getDevOptions({ outDir: OUTPUT_DIR, target, channel }),
outdir: OUTPUT_DIR,
});
try {
await ctx.serve({
host: 'localhost',
port: SERVE_PORTS[target],
servedir: OUTPUT_DIR,
});
} catch (error) {
console.log(error.message);
console.log('>>> PLEASE TRY SAVING BUILD SCRIPT AGAIN');
}
await ctx.watch();
process.on('beforeExit', () => ctx.dispose());
}