-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbuild.mjs
More file actions
77 lines (68 loc) · 1.64 KB
/
build.mjs
File metadata and controls
77 lines (68 loc) · 1.64 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
import * as esbuild from 'esbuild';
import {execFileSync} from 'child_process';
import {mkdirSync, readFileSync, writeFileSync} from 'fs';
const watch = process.argv.includes('--watch');
// Read target from tsconfig.json.
const tsconfig = JSON.parse(
readFileSync('tsconfig.json', 'utf-8').replace(/,(\s*[}\]])/g, '$1'),
);
const target = tsconfig.compilerOptions?.target?.toLowerCase() ?? 'esnext';
// Ensure output directory exists.
mkdirSync('out', {recursive: true});
/** @type {esbuild.Plugin} */
const tscPlugin = {
name: 'tsc',
setup(build) {
build.onStart(() => {
try {
execFileSync('pnpm', ['run', 'typecheck'], {
stdio: 'inherit',
});
} catch {
return {errors: [{text: 'Type check failed'}]};
}
});
build.onEnd((result) => {
if (result.errors.length > 0) {
return;
}
for (const file of result.outputFiles ?? []) {
writeFileSync(file.path, file.contents);
}
});
},
};
/** @type {esbuild.BuildOptions} */
const common = {
entryPoints: ['src/phoenix.ts'],
bundle: true,
format: 'esm',
target,
// Ensure we only write on success.
write: false,
plugins: [tscPlugin],
};
/** @type {esbuild.BuildOptions} */
const prod = {
...common,
outfile: 'out/phoenix.js',
minify: true,
logLevel: 'info',
};
/** @type {esbuild.BuildOptions} */
const debug = {
...common,
outfile: 'out/phoenix.debug.js',
sourcemap: 'inline',
logLevel: watch ? 'silent' : 'info',
};
if (watch) {
const [prodCtx, debugCtx] = await Promise.all([
esbuild.context(prod),
esbuild.context(debug),
]);
await Promise.all([prodCtx.watch(), debugCtx.watch()]);
} else {
await esbuild.build(prod);
await esbuild.build(debug);
}