-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
75 lines (66 loc) · 1.87 KB
/
esbuild.config.mjs
File metadata and controls
75 lines (66 loc) · 1.87 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
import { build, context } from 'esbuild';
import { mkdir, rm, writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';
const watch = process.argv.includes('--watch');
const outdir = resolve('dist/assets/js');
async function prepareOutDir() {
await rm(outdir, { recursive: true, force: true });
await mkdir(outdir, { recursive: true });
}
const baseConfig = {
entryPoints: ['src/js/graph.ts', 'src/js/search.ts', 'src/js/analytics.ts', 'src/js/glitch.ts'],
outdir,
bundle: true,
sourcemap: true,
format: 'esm',
target: ['es2022'],
splitting: true,
chunkNames: 'chunks/[name]-[hash]',
metafile: true,
loader: {
'.json': 'json',
},
define: {
'process.env.NODE_ENV': JSON.stringify(watch ? 'development' : 'production'),
},
minify: !watch,
entryNames: '[name]',
};
async function writeMetaFile(metafile) {
const metaPath = resolve(outdir, 'meta.json');
await writeFile(metaPath, JSON.stringify(metafile, null, 2), 'utf-8');
}
async function runBuild() {
await prepareOutDir();
if (watch) {
const ctx = await context(baseConfig);
await ctx.watch();
console.log('🔁 esbuild is watching for changes');
} else {
const result = await build({
entryPoints: ['src/js/graph.ts', 'src/js/search.ts', 'src/js/analytics.ts', 'src/js/glitch.ts'],
outdir,
bundle: true,
sourcemap: true,
format: 'esm',
target: ['es2022'],
splitting: true,
chunkNames: 'chunks/[name]-[hash]',
metafile: true,
entryNames: '[name]',
loader: {
'.json': 'json',
},
define: {
'process.env.NODE_ENV': JSON.stringify(watch ? 'development' : 'production'),
},
minify: !watch,
});
await writeMetaFile(result.metafile);
console.log('✅ esbuild build complete');
}
}
runBuild().catch((error) => {
console.error(error);
process.exit(1);
});