-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
143 lines (122 loc) · 4.86 KB
/
Copy pathbuild.js
File metadata and controls
143 lines (122 loc) · 4.86 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
#!/usr/bin/env node
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const isWatch = process.argv.includes('--watch');
// Build configuration
const buildOptions = {
entryPoints: ['public/app.js'],
bundle: false, // No bundling needed - single file
minify: true,
sourcemap: false, // No source maps in production
target: ['es2020'],
outfile: 'dist/app.min.js',
charset: 'utf8',
legalComments: 'none', // Remove all comments
drop: ['console', 'debugger'], // Remove console.log and debugger statements
};
// Ensure dist directory exists
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist', { recursive: true });
}
async function build() {
try {
console.log('Building JavaScript...');
// Build app.js
await esbuild.build(buildOptions);
// Get file hash for cache busting
const content = fs.readFileSync('dist/app.min.js', 'utf8');
const hash = crypto.createHash('md5').update(content).digest('hex').substring(0, 8);
// Copy and process other static files
console.log('Copying static files...');
// Copy CSS (minified)
const cssResult = await esbuild.build({
entryPoints: ['public/style.css'],
bundle: false,
minify: true,
outfile: 'dist/style.min.css',
charset: 'utf8',
});
// Copy index.html and update references
let html = fs.readFileSync('public/index.html', 'utf8');
// Update script reference to use minified version with hash
html = html.replace(
/<script src="app\.js[^"]*"><\/script>/,
`<script src="app.min.js?v=${hash}"></script>`
);
// Update CSS reference to use minified version with hash
const cssContent = fs.readFileSync('dist/style.min.css', 'utf8');
const cssHash = crypto.createHash('md5').update(cssContent).digest('hex').substring(0, 8);
html = html.replace(
/<link rel="stylesheet" href="style\.css[^"]*">/,
`<link rel="stylesheet" href="style.min.css?v=${cssHash}">`
);
fs.writeFileSync('dist/index.html', html);
// Copy hilfe.html
if (fs.existsSync('public/hilfe.html')) {
let hilfeHtml = fs.readFileSync('public/hilfe.html', 'utf8');
hilfeHtml = hilfeHtml.replace(
/<link rel="stylesheet" href="style\.css[^"]*">/,
`<link rel="stylesheet" href="style.min.css?v=${cssHash}">`
);
fs.writeFileSync('dist/hilfe.html', hilfeHtml);
}
// Copy inspektion.html if exists
if (fs.existsSync('public/inspektion.html')) {
let inspHtml = fs.readFileSync('public/inspektion.html', 'utf8');
inspHtml = inspHtml.replace(
/<link rel="stylesheet" href="style\.css[^"]*">/,
`<link rel="stylesheet" href="style.min.css?v=${cssHash}">`
);
fs.writeFileSync('dist/inspektion.html', inspHtml);
}
// Copy datenschutz.html if exists
if (fs.existsSync('public/datenschutz.html')) {
let datenschutzHtml = fs.readFileSync('public/datenschutz.html', 'utf8');
datenschutzHtml = datenschutzHtml.replace(
/<link rel="stylesheet" href="style\.css[^"]*">/,
`<link rel="stylesheet" href="style.min.css?v=${cssHash}">`
);
fs.writeFileSync('dist/datenschutz.html', datenschutzHtml);
}
// Copy impressum.html if exists
if (fs.existsSync('public/impressum.html')) {
let impressumHtml = fs.readFileSync('public/impressum.html', 'utf8');
impressumHtml = impressumHtml.replace(
/<link rel="stylesheet" href="style\.css[^"]*">/,
`<link rel="stylesheet" href="style.min.css?v=${cssHash}">`
);
fs.writeFileSync('dist/impressum.html', impressumHtml);
}
// Get file sizes
const originalJs = fs.statSync('public/app.js').size;
const minifiedJs = fs.statSync('dist/app.min.js').size;
const originalCss = fs.statSync('public/style.css').size;
const minifiedCss = fs.statSync('dist/style.min.css').size;
console.log('');
console.log('Build complete!');
console.log('');
console.log('JavaScript:');
console.log(` Original: ${(originalJs / 1024).toFixed(1)} KB`);
console.log(` Minified: ${(minifiedJs / 1024).toFixed(1)} KB (${((1 - minifiedJs/originalJs) * 100).toFixed(0)}% reduction)`);
console.log('');
console.log('CSS:');
console.log(` Original: ${(originalCss / 1024).toFixed(1)} KB`);
console.log(` Minified: ${(minifiedCss / 1024).toFixed(1)} KB (${((1 - minifiedCss/originalCss) * 100).toFixed(0)}% reduction)`);
console.log('');
console.log('Output directory: dist/');
console.log('Files: index.html, app.min.js, style.min.css, hilfe.html, datenschutz.html, impressum.html');
} catch (error) {
console.error('Build failed:', error);
process.exit(1);
}
}
if (isWatch) {
console.log('Watching for changes...');
// For watch mode, we'd need to set up file watchers
// For now, just run the build once
build();
} else {
build();
}