-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-prerender.js
More file actions
77 lines (61 loc) · 1.91 KB
/
Copy pathbuild-prerender.js
File metadata and controls
77 lines (61 loc) · 1.91 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
#!/usr/bin/env node
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
console.log('\n🏗️ Building and pre-rendering website...\n');
// Step 1: Build the project
console.log('📦 Step 1: Building project with Vite...\n');
const build = spawn('npx', ['vite', 'build'], { stdio: 'inherit', shell: true });
build.on('close', (code) => {
if (code !== 0) {
console.error('\n❌ Build failed!\n');
process.exit(code);
}
console.log('\n✅ Build complete!\n');
// Step 2: Start preview server
console.log('🌐 Step 2: Starting preview server...\n');
const preview = spawn('npx', ['vite', 'preview', '--port', '4173'], {
stdio: 'pipe',
shell: true
});
let serverReady = false;
preview.stdout.on('data', (data) => {
const output = data.toString();
if (output.includes('Local') && !serverReady) {
serverReady = true;
// Step 3: Run prerendering
console.log('✅ Preview server ready!\n');
console.log('🎨 Step 3: Pre-rendering routes...\n');
const prerender = spawn('node', ['prerender.js'], {
stdio: 'inherit',
shell: true
});
prerender.on('close', (prerenderCode) => {
// Step 4: Stop preview server
preview.kill();
if (prerenderCode !== 0) {
console.error('\n❌ Pre-rendering failed!\n');
process.exit(prerenderCode);
}
console.log('\n🎉 Build and pre-rendering complete!\n');
console.log('📂 Output: dist/\n');
process.exit(0);
});
}
});
preview.stderr.on('data', (data) => {
// Suppress preview server logs unless error
if (data.toString().includes('Error')) {
console.error(data.toString());
}
});
// Timeout fallback (30 seconds)
setTimeout(() => {
if (!serverReady) {
console.error('\n❌ Preview server failed to start within 30 seconds\n');
preview.kill();
process.exit(1);
}
}, 30000);
});