-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.ts
More file actions
84 lines (71 loc) · 1.95 KB
/
build.ts
File metadata and controls
84 lines (71 loc) · 1.95 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
import { $ } from 'bun';
import tailwindcss from '@tailwindcss/postcss';
import postcss from 'postcss';
const DIST_DIR = './dist';
const PUBLIC_DIR = `${DIST_DIR}/public`;
const SERVER_DIR = `${DIST_DIR}/server`;
async function buildStyles() {
const sourceCss = await Bun.file('./src/web/styles.css').text();
const output = await postcss([tailwindcss()]).process(sourceCss, {
from: './src/web/styles.css',
to: `${PUBLIC_DIR}/styles.css`,
});
await Bun.write(`${PUBLIC_DIR}/styles.css`, output.css);
}
async function buildSpa() {
const result = await Bun.build({
entrypoints: ['./src/web/main.tsx'],
outdir: PUBLIC_DIR,
target: 'browser',
format: 'esm',
splitting: false,
minify: false,
sourcemap: 'linked',
});
if (!result.success) {
throw new Error(
`SPA build failed: ${result.logs.map((log) => log.message).join('\n')}`,
);
}
await Bun.write(
`${PUBLIC_DIR}/index.html`,
await Bun.file('./src/web/index.html').text(),
);
}
async function buildServer() {
const result = await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: SERVER_DIR,
target: 'bun',
format: 'esm',
splitting: false,
minify: false,
sourcemap: 'linked',
external: ['@nbw/database', '@nbw/config', '@nbw/validation'],
});
if (!result.success) {
throw new Error(
`Server build failed: ${result.logs
.map((log) => log.message)
.join('\n')}`,
);
}
}
async function buildAll() {
await $`rm -rf ${DIST_DIR}`;
await $`mkdir -p ${PUBLIC_DIR} ${SERVER_DIR}`;
await Promise.all([buildStyles(), buildSpa(), buildServer()]);
}
if (process.argv.includes('--watch')) {
throw new Error(
'Watch mode is handled by `bun --watch run src/index.ts` in `bun run dev`.',
);
}
buildAll()
.then(() => {
process.stdout.write('Built admin app successfully.\n');
})
.catch((error) => {
process.stderr.write(`${String(error)}\n`);
process.exit(1);
});