-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbundle.mjs
More file actions
147 lines (127 loc) · 4.07 KB
/
bundle.mjs
File metadata and controls
147 lines (127 loc) · 4.07 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
/**
* Bundle script for website server
*
* Creates production-ready bundles using Rolldown and collects WASM files.
* Generates two separate bundles:
* - server.mjs: Full server bundle with all dependencies
* - worker.mjs: Minimal worker bundle for tinypool workers
*
* Usage: node scripts/bundle.mjs
*/
import { execSync } from 'node:child_process';
import { cpSync, existsSync, mkdirSync, readdirSync, rmSync, statSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { rolldown } from 'rolldown';
const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, '..');
const distBundledDir = join(rootDir, 'dist-bundled');
const wasmDir = join(distBundledDir, 'wasm');
/**
* Clean dist-bundled directory
*/
function cleanDistBundled() {
console.log('Cleaning dist-bundled...');
if (existsSync(distBundledDir)) {
rmSync(distBundledDir, { recursive: true });
}
mkdirSync(distBundledDir, { recursive: true });
}
/**
* Build TypeScript to JavaScript
*/
function buildTypeScript() {
console.log('Building TypeScript...');
execSync('npm run build', { cwd: rootDir, stdio: 'inherit' });
}
/**
* Bundle server with Rolldown (full bundle with all server dependencies)
*/
async function bundleAll() {
console.log('Bundling with code splitting...');
const build = await rolldown({
input: {
server: join(rootDir, 'dist/index.js'),
worker: join(rootDir, 'dist/worker-entry.js'),
},
platform: 'node',
external: ['tinypool', 'tiktoken'],
});
await build.write({
dir: distBundledDir,
format: 'esm',
entryFileNames: '[name].mjs',
chunkFileNames: '[name]-[hash].mjs',
// Note: No banner - Rolldown generates necessary shims via rolldown-runtime chunk
minify: true,
legalComments: 'inline',
// Force code splitting with advancedChunks
advancedChunks: {
groups: [
{
name: 'shared',
minSize: 100_000, // 100KB minimum for shared chunks
minShareCount: 2, // Module must be used by at least 2 entry points
},
],
},
});
// Report bundle sizes
const files = readdirSync(distBundledDir).filter((f) => f.endsWith('.mjs'));
for (const file of files) {
const size = getFileSizeMB(join(distBundledDir, file));
console.log(`Bundle created: dist-bundled/${file} (${size} MB)`);
}
}
/**
* Get file size in MB
*/
function getFileSizeMB(filePath) {
const stats = statSync(filePath);
return (stats.size / 1024 / 1024).toFixed(2);
}
/**
* Collect WASM files from node_modules
*/
function collectWasmFiles() {
console.log('Collecting WASM files...');
// Create wasm directory
if (!existsSync(wasmDir)) {
mkdirSync(wasmDir, { recursive: true });
}
// Copy web-tree-sitter.wasm to dist-bundled root
// (web-tree-sitter looks for WASM file in the same directory as the JS file)
const webTreeSitterWasm = join(rootDir, 'node_modules/web-tree-sitter/web-tree-sitter.wasm');
if (existsSync(webTreeSitterWasm)) {
cpSync(webTreeSitterWasm, join(distBundledDir, 'web-tree-sitter.wasm'));
console.log('Copied web-tree-sitter.wasm to dist-bundled/');
} else {
console.warn('Warning: web-tree-sitter.wasm not found');
}
// Find and copy tree-sitter language WASM files
const treeSitterWasmsDir = join(rootDir, 'node_modules/@repomix/tree-sitter-wasms/out');
if (existsSync(treeSitterWasmsDir)) {
const wasmFiles = readdirSync(treeSitterWasmsDir).filter((f) => f.endsWith('.wasm'));
for (const file of wasmFiles) {
cpSync(join(treeSitterWasmsDir, file), join(wasmDir, file));
}
console.log(`Copied ${wasmFiles.length} language WASM files to dist-bundled/wasm/`);
} else {
console.warn('Warning: tree-sitter-wasms not found');
}
}
/**
* Main bundle process
*/
async function main() {
console.log('Starting bundle process...\n');
cleanDistBundled();
buildTypeScript();
await bundleAll();
collectWasmFiles();
console.log('\nBundle complete!');
}
main().catch((err) => {
console.error('Bundle failed:', err);
process.exit(1);
});