-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathbuild.js
92 lines (84 loc) · 2.08 KB
/
build.js
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
85
86
87
88
89
90
91
92
import * as esbuild from 'esbuild';
import { argv } from 'node:process';
import { chmod } from 'node:fs/promises';
import { platform } from 'node:os';
const watch = argv.includes('--watch');
const nodeBuiltinsPlugin = {
name: 'node-builtins',
setup(build) {
// Handle punycode redirects
build.onResolve({ filter: /^(node:)?punycode$/ }, () => ({
path: 'punycode/',
external: true
}));
// Handle other node builtins
build.onResolve({ filter: /^(util|http)$/ }, args => ({
path: args.path,
namespace: 'node-builtin'
}));
build.onLoad({ filter: /.*/, namespace: 'node-builtin' }, args => ({
contents: `export * from 'node:${args.path}'; export { default } from 'node:${args.path}';`,
loader: 'js'
}));
}
};
const buildOptions = {
entryPoints: ['./src/index.ts'],
bundle: true,
minify: true,
treeShaking: true,
platform: 'node',
format: 'esm',
target: 'node20',
outfile: './dist/index.mjs',
banner: {
js: `#!/usr/bin/env node
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
`
},
resolveExtensions: ['.ts', '.js'],
external: [
// Node built-ins
'node:*',
'stream',
'events',
'url',
'path',
'fs',
'util',
'http',
'https',
'os',
'crypto',
'process',
'punycode',
// External dependencies
'dotenv',
'repomix',
'eventsource-client',
// Playwright and its dependencies (peer dependency)
'playwright',
'playwright-core',
'chromium-bidi',
'chromium-bidi/*',
],
keepNames: true,
mainFields: ['module', 'main'],
define: {},
plugins: [nodeBuiltinsPlugin]
};
if (watch) {
const context = await esbuild.context(buildOptions);
// eslint-disable-next-line no-undef
console.log('Watching for changes...');
await context.watch();
} else {
await esbuild.build(buildOptions);
// Make the output file executable on Unix-like systems
if (platform() !== 'win32') {
await chmod('./dist/index.mjs', 0o755);
}
// eslint-disable-next-line no-undef
console.log('Build complete');
}