-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathesbuild.js
More file actions
104 lines (94 loc) · 2.63 KB
/
Copy pathesbuild.js
File metadata and controls
104 lines (94 loc) · 2.63 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
import esbuild from 'esbuild';
import { readFile } from 'fs/promises';
import { copy } from 'esbuild-plugin-copy';
async function getExternalDeps() {
const pkg = JSON.parse(await readFile('package.json', 'utf-8'));
return [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.devDependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
];
}
async function getBaseConfig() {
const externalDeps = await getExternalDeps();
return {
entryPoints: ['src/index.ts'],
bundle: true,
splitting: true,
treeShaking: true,
outdir: 'dist',
platform: 'node',
target: 'node22',
format: 'esm',
sourcemap: true,
external: externalDeps,
plugins: [
copy({
// this is equal to process.cwd(), which means we use cwd path as base path to resolve `to` path
// if not specified, this plugin uses ESBuild.build outdir/outfile options as base path.
resolveFrom: 'cwd',
assets: {
from: ['./src/fixtures/**/*'],
to: ['./dist/fixtures'],
},
watch: true,
}),
],
};
}
async function watch() {
try {
const config = await getBaseConfig();
const context = await esbuild.context({
...config,
plugins: [
{
name: 'rebuild-logger',
setup(build) {
build.onEnd((result) => {
const timestamp = new Date().toLocaleTimeString();
if (result.errors.length > 0) {
console.error(`[${timestamp}] Rebuild failed:`, result.errors);
} else {
console.log(`[${timestamp}] Rebuild complete`);
if (result.warnings.length > 0) {
console.warn(`[${timestamp}] Warnings:`, result.warnings);
}
}
});
},
},
],
});
await context.watch();
console.log('Watching for changes...');
} catch (error) {
console.error('Watch mode failed:', error);
process.exit(1);
}
}
async function build() {
try {
const config = await getBaseConfig();
const result = await esbuild.build(config);
if (result.errors.length > 0) {
console.error('Build failed:', result.errors);
process.exit(1);
}
if (result.warnings.length > 0) {
console.warn('Build warnings:', result.warnings);
}
console.log('Build complete!');
} catch (error) {
console.error('Build failed:', error);
process.exit(1);
}
}
// Check if watch mode is requested
const isWatchMode = process.argv.includes('--watch') || process.argv.includes('-w');
if (isWatchMode) {
watch();
} else {
build();
}