-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
80 lines (73 loc) · 2.18 KB
/
build.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
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
import * as esbuild from 'esbuild';
import { tailwindPlugin } from 'esbuild-plugin-tailwindcss';
import * as fs from 'fs';
import * as path from 'path';
import * as proc from 'child_process';
/**
* Main esbuild build-file.
* Usage:
* - build for production: node build.mjs
* - serve for development: node build.mjs serve
* - build kubernetes manifest: node build.mjs k8s
*/
const mainFile = 'app.tsx';
const mainDir = 'src/main';
const kubernetesFile = 'app.ts';
const kubernetesDir = 'src/kubernetes';
const resourceDir = 'src/resources';
const excludes = ['*.jpg', '*.png', '*.svg', '*.webp', '*.ttf'];
const outputDir = 'out';
const serve = process.argv[2] === 'serve';
const kubernetes = process.argv[2] === 'k8s';
// esbuild main config
const mainConf = {
entryPoints: [`${mainDir}/${mainFile}`],
bundle: true,
minify: !serve,
sourcemap: serve,
plugins: [tailwindPlugin()],
external: excludes,
outdir: outputDir,
};
// esbuild kubernetes config
const kubernetesConf = {
entryPoints: [`${kubernetesDir}/${kubernetesFile}`],
bundle: true,
platform: 'node',
packages: 'external',
outfile: 'temp.k8.js',
};
if (serve) {
// serves the app (node build.mjs serve)
// or builds it for production (node build.mjs)
const ctx = await esbuild.context(mainConf);
await ctx.watch();
const { host, port } = await ctx.serve({
port: 3000,
servedir: mainConf.outdir,
});
console.log(`🚀 Serving at http://${host}:${port}/`);
} else if(kubernetes) {
// builds the kubernetes manifest for deployment
await esbuild.build(kubernetesConf);
proc.fork(kubernetesConf.outfile).on('exit', (code) => {
fs.unlinkSync(kubernetesConf.outfile);
});
} else {
await esbuild.build(mainConf);
await esbuild.stop();
}
// copy static resources to output dir
(function copyFiles(sourceDir, targetDir) {
fs.readdir(sourceDir, (err, files) => {
if (err) {
console.error('Error reading resource directory:', err);
return;
}
files.forEach((file) => {
const sourceFile = path.join(sourceDir, file);
const targetFile = path.join(targetDir, file);
fs.cpSync(sourceFile, targetFile, { recursive: true });
});
});
})(resourceDir, outputDir);