-
Notifications
You must be signed in to change notification settings - Fork 2
/
bundle.mjs
51 lines (44 loc) · 1.5 KB
/
bundle.mjs
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
import * as esbuild from 'esbuild';
import * as fs from 'fs/promises';
import * as process from 'process';
import { exec } from 'child_process';
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const isDevelopment = process.env.NODE_ENV === 'development';
const isWatchMode = isDevelopment && process.env.WATCH !== undefined;
const isProduction = process.env.NODE_ENV === 'production';
console.log(process.env.WATCH);
const PARTY_BUNDLE = 'party';
const PARTY_API = isDevelopment ? 'http://localhost:3005' : 'https://api.tunage.app'
let ctx = await esbuild.context({
entryPoints: ['ts/main.ts'],
bundle: true,
outfile: `public/assets/javascript/${PARTY_BUNDLE}.js`,
define: {
'process.env.NODE_ENV': `'${process.env.NODE_ENV}'`,
'process.env.PARTY_API': `'${process.env.PARTY_API ?? PARTY_API}'`
},
minify: isProduction,
sourcemap: isProduction ? false : 'inline',
});
if (isWatchMode) await buildIndex().then(async () => ctx.watch());
else if (isDevelopment) await buildIndex().then(async () => {
await ctx.rebuild();
process.exit(0);
});
else await serveSite();
async function buildIndex() {
await fs.copyFile('index.html', 'public/index.html');
}
async function serveSite() {
setTimeout(() => exec("open http://localhost:3000"), 500);
await buildIndex();
await ctx.serve({
port: 3000,
servedir: "public",
onRequest: async (request) => {
console.log(request.path);
if (request.path.endsWith('/index.html')) await buildIndex();
}
});
process.exit(0);
}