-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathbuild.ts
More file actions
200 lines (166 loc) · 4.28 KB
/
build.ts
File metadata and controls
200 lines (166 loc) · 4.28 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { mkdir } from 'fs/promises';
import { join } from 'path';
const OUT_DIR_BROWSER = './dist/engine/core';
const OUT_DIR_DEBUG = './dist/engine/debug';
const OUT_DIR_MODULE = './lib';
const SRC_DIR = './src';
const DEBUG_DIR = './debug';
const commonBuildOptions = {
target: 'browser' as const,
format: 'esm' as const,
sourcemap: 'linked' as const,
minify: true,
};
/**
* Build the ES module version
*/
async function buildModule(): Promise<void> {
console.log('📦 Building ES module...');
const result = await Bun.build({
entrypoints: [join(SRC_DIR, 'index.js')],
outdir: OUT_DIR_MODULE,
naming: 'monogatari.module.js',
...commonBuildOptions,
});
if (!result.success) {
console.error('❌ Module build failed:');
for (const log of result.logs) {
console.error(log);
}
process.exit(1);
}
console.log('✅ ES module built successfully');
}
/**
* Build the browser bundle version
*/
async function buildBrowser(): Promise<void> {
console.log('🌐 Building browser bundle...');
const result = await Bun.build({
entrypoints: [join(SRC_DIR, 'browser.ts')],
outdir: OUT_DIR_BROWSER,
naming: 'monogatari.js',
target: 'browser',
format: 'iife',
sourcemap: 'linked',
minify: true,
});
if (!result.success) {
console.error('❌ Browser build failed:');
for (const log of result.logs) {
console.error(log);
}
process.exit(1);
}
console.log('✅ Browser bundle built successfully');
}
/**
* Build the debug script for browser
*/
async function buildDebug(): Promise<void> {
console.log('🐛 Building debug script...');
const result = await Bun.build({
entrypoints: [join(DEBUG_DIR, 'index.js')],
outdir: OUT_DIR_DEBUG,
naming: 'debug.js',
target: 'browser',
format: 'iife',
sourcemap: 'linked',
minify: true,
});
if (!result.success) {
console.error('❌ Debug build failed:');
for (const log of result.logs) {
console.error(log);
}
process.exit(1);
}
console.log('✅ Debug script built successfully');
}
/**
* Build CSS
*/
async function buildCSS(): Promise<void> {
console.log('🎨 Building CSS...');
const result = await Bun.build({
entrypoints: [join(SRC_DIR, 'index.css')],
outdir: OUT_DIR_BROWSER,
naming: 'monogatari.css',
minify: true,
sourcemap: 'linked',
});
if (!result.success) {
console.error('❌ CSS build failed:');
for (const log of result.logs) {
console.error(log);
}
process.exit(1);
}
console.log('✅ CSS built successfully');
}
/**
* Build TypeScript declaration files
*/
async function buildTypes(): Promise<void> {
console.log('📝 Building type declarations...');
const proc = Bun.spawn(['tsc', '--emitDeclarationOnly', '--declarationDir', './dist/types'], {
stdout: 'inherit',
stderr: 'inherit',
});
const exitCode = await proc.exited;
if (exitCode !== 0) {
console.error('❌ Type declarations build failed');
process.exit(1);
}
console.log('✅ Type declarations built successfully');
}
async function watchMode(): Promise<void> {
console.log('👀 Starting watch mode...');
// Initial build
await Promise.all([buildBrowser(), buildCSS()]);
// Watch for changes
const srcWatcher = Bun.spawn(['bun', 'build', join(SRC_DIR, 'browser.ts'),
'--target', 'browser',
'--format', 'iife',
'--outdir', OUT_DIR_BROWSER,
'--watch'
], {
stdout: 'inherit',
stderr: 'inherit',
});
console.log('🔄 Watching for changes... (Press Ctrl+C to stop)');
// Keep process alive
await srcWatcher.exited;
}
// Parse CLI arguments
const args = process.argv.slice(2);
const flags = new Set(args);
// Ensure output directories exist
await mkdir(OUT_DIR_BROWSER, { recursive: true });
await mkdir(OUT_DIR_DEBUG, { recursive: true });
await mkdir(OUT_DIR_MODULE, { recursive: true });
// Execute based on flags
if (flags.has('--watch')) {
await watchMode();
} else if (flags.has('--module')) {
await buildModule();
} else if (flags.has('--browser')) {
await buildBrowser();
} else if (flags.has('--css')) {
await buildCSS();
} else if (flags.has('--debug')) {
await buildDebug();
} else if (flags.has('--types')) {
await buildTypes();
} else {
// Build all
console.log('🚀 Starting full build...\n');
await Promise.all([
buildModule(),
buildBrowser(),
buildCSS(),
buildDebug(),
]);
await buildTypes();
console.log('\n🎉 Build completed successfully!');
}