-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
56 lines (49 loc) · 1.76 KB
/
build.js
File metadata and controls
56 lines (49 loc) · 1.76 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
const fs = require('fs');
const path = require('path');
const { minify } = require('html-minifier-terser');
const JavaScriptObfuscator = require('javascript-obfuscator');
const SRC = path.join(__dirname, 'page (2).html');
const OUT_DIR = path.join(__dirname, 'dist');
const OUT_FILE = path.join(OUT_DIR, 'index.html');
async function build() {
if (!fs.existsSync(SRC)) {
console.error('Source HTML not found:', SRC);
process.exit(1);
}
let html = fs.readFileSync(SRC, 'utf8');
// Obfuscate inline scripts (simple heuristic: <script>...</script> without src)
html = html.replace(/<script([^>]*)>([\s\S]*?)<\/script>/gi, (match, attrs, code) => {
if (/src\s*=/.test(attrs)) return match; // external scripts stay as-is
if (!code.trim()) return match;
try {
const ob = JavaScriptObfuscator.obfuscate(code, {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
rotateStringArray: true,
stringArrayThreshold: 0.75
}).getObfuscatedCode();
return `<script${attrs}>/* Obfuscated by build */\n${ob}\n</script>`;
} catch (e) {
console.warn('JS obfuscation failed, keeping original script:', e.message);
return match;
}
});
// Minify the full HTML
const minified = await minify(html, {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeAttributeQuotes: false,
minifyCSS: true,
minifyJS: false // already obfuscated
});
if (!fs.existsSync(OUT_DIR)) fs.mkdirSync(OUT_DIR, { recursive: true });
fs.writeFileSync(OUT_FILE, minified, 'utf8');
console.log('Build complete ->', OUT_FILE);
}
build().catch((e) => {
console.error('Build failed:', e);
process.exit(2);
});