-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
76 lines (67 loc) · 2.16 KB
/
build.mjs
File metadata and controls
76 lines (67 loc) · 2.16 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
import esbuild from 'esbuild';
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import chalk from 'chalk';
import pkg from 'fast-glob'; // fast-glob is a CommonJS module?
const {globSync} = pkg;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const buildPath = path.resolve(__dirname);
const isWatchMode = process.argv.includes('--watch');
async function buildFiles() {
const buildContexts = [
{
label: 'client',
platform: 'browser',
target: ['chrome103'],
entryPoints: globSync('./src/client/index.ts'),
format: 'iife',
},
{
label: 'server',
platform: 'node',
target: ['node22'],
entryPoints: globSync('./src/server/index.ts'),
format: 'cjs',
},
{
label: 'common',
platform: 'browser',
target: ['chrome103'],
entryPoints: globSync('./src/common/*.ts'),
format: 'iife',
}
];
for (const ctx of buildContexts) {
const label = ctx.label;
delete ctx.label;
const outputDir = path.normalize(path.join(buildPath, 'resource', label));
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, {recursive: true});
}
try {
const context = await esbuild.context({
...ctx,
outdir: outputDir,
bundle: true,
minify: true,
minifyWhitespace: true,
logLevel: 'info',
sourcemap: true,
sourcesContent: false,
});
if (isWatchMode) {
console.log(chalk.yellowBright(`Watching for changes in ${label}...`));
await context.watch();
} else {
await context.rebuild();
console.log(chalk.green(`Build completed for ${label}`));
await context.dispose();
}
} catch (error) {
console.error(`\nBuild errored for ${label}:`, error);
}
}
}
buildFiles();