-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
120 lines (101 loc) · 3.69 KB
/
build.js
File metadata and controls
120 lines (101 loc) · 3.69 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
/**
* Build Script - Production Ready
* Fixes all path references for deployment
*/
import fs from 'fs';
import fsPromises from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const CONFIG = {
SRC_DIR: './src',
PUBLIC_DIR: './public',
DIST_DIR: './dist',
};
function createDirectory(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
async function copyDir(src, dest) {
await fsPromises.mkdir(dest, { recursive: true });
const entries = await fsPromises.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
await copyDir(srcPath, destPath);
} else {
await fsPromises.copyFile(srcPath, destPath);
}
}
}
async function fixHtmlPaths(htmlContent) {
// Replace ALL development paths with production paths
return (
htmlContent
// Fix CSS paths (all variations)
.replace(/href="\.\/\.\/src\/css\//g, 'href="./css/')
.replace(/href="\.\.\/src\/css\//g, 'href="./css/')
.replace(/href="\/src\/css\//g, 'href="./css/')
// Fix JS paths (all variations)
.replace(/src="\.\/\.\/src\/js\//g, 'src="./js/')
.replace(/src="\.\.\/src\/js\//g, 'src="./js/')
.replace(/src="\/src\/js\//g, 'src="./js/')
// Fix asset paths (all variations)
.replace(/src="\.\/\.\/src\/assets\//g, 'src="./assets/')
.replace(/src="\.\.\/src\/assets\//g, 'src="./assets/')
.replace(/src="\/src\/assets\//g, 'src="./assets/')
.replace(/href="\.\/\.\/src\/assets\//g, 'href="./assets/')
.replace(/href="\.\.\/src\/assets\//g, 'href="./assets/')
.replace(/href="\/src\/assets\//g, 'href="./assets/')
// Fix service worker registration
.replace(
/navigator\.serviceWorker\.register\(['"]\/service-worker\.js['"]\)/g,
"navigator.serviceWorker.register('./service-worker.js')"
)
.replace(
/navigator\.serviceWorker\.register\(['"]\.\/service-worker\.js['"]\)/g,
"navigator.serviceWorker.register('./service-worker.js')"
)
);
}
async function build() {
console.log('\n🚀 Building production bundle...\n');
// Clean dist
if (fs.existsSync(CONFIG.DIST_DIR)) {
fs.rmSync(CONFIG.DIST_DIR, { recursive: true, force: true });
}
createDirectory(CONFIG.DIST_DIR);
// Copy public files
await copyDir(CONFIG.PUBLIC_DIR, CONFIG.DIST_DIR);
// Fix index.html paths
const indexPath = path.join(CONFIG.DIST_DIR, 'index.html');
let htmlContent = await fsPromises.readFile(indexPath, 'utf8');
htmlContent = await fixHtmlPaths(htmlContent);
await fsPromises.writeFile(indexPath, htmlContent, 'utf8');
console.log('✅ Fixed paths in index.html');
// Copy src directories to root of dist
await copyDir(path.join(CONFIG.SRC_DIR, 'css'), path.join(CONFIG.DIST_DIR, 'css'));
await copyDir(path.join(CONFIG.SRC_DIR, 'js'), path.join(CONFIG.DIST_DIR, 'js'));
await copyDir(path.join(CONFIG.SRC_DIR, 'assets'), path.join(CONFIG.DIST_DIR, 'assets'));
// Create version file
const pkg = JSON.parse(await fsPromises.readFile('./package.json', 'utf8'));
await fsPromises.writeFile(
path.join(CONFIG.DIST_DIR, 'version.json'),
JSON.stringify(
{
version: pkg.version,
buildDate: new Date().toISOString(),
},
null,
2
)
);
console.log('\n✅ Build completed successfully!');
console.log(`📦 Output: ${CONFIG.DIST_DIR}/`);
}
if (import.meta.url === `file://${process.argv[1]}`) {
build().catch(console.error);
}