-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
95 lines (80 loc) · 3.4 KB
/
build.js
File metadata and controls
95 lines (80 loc) · 3.4 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
const fs = require('fs-extra');
const path = require('path');
const JavaScriptObfuscator = require('javascript-obfuscator');
const { minify } = require('html-minifier-terser');
const csso = require('csso');
const { glob } = require('glob');
const distDir = path.join(__dirname, 'dist');
async function build() {
console.log('🚀 Starting Production Build...');
// 1. Clean dist directory
await fs.emptyDir(distDir);
console.log('🧹 Cleaned dist/ directory.');
// 2. Process JS Files (Obfuscation)
const jsFiles = await glob('js/**/*.js');
for (const file of jsFiles) {
const content = await fs.readFile(file, 'utf8');
console.log(`🔒 Obfuscating: ${file}`);
const obfuscationResult = JavaScriptObfuscator.obfuscate(content, {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
numbersToExpressions: true,
simplify: true,
stringArray: true,
stringArrayEncoding: ['base64'],
stringArrayThreshold: 0.75,
splitStrings: true,
unicodeEscapeSequence: false,
renameGlobals: false,
selfDefending: true,
debugProtection: true,
debugProtectionInterval: 4000
});
const targetPath = path.join(distDir, file);
await fs.ensureDir(path.dirname(targetPath));
await fs.writeFile(targetPath, obfuscationResult.getObfuscatedCode());
}
// 3. Process CSS Files (Minification)
const cssFiles = await glob('css/**/*.css');
for (const file of cssFiles) {
const content = await fs.readFile(file, 'utf8');
console.log(`🎨 Minifying CSS: ${file}`);
const minifiedCss = csso.minify(content).css;
const targetPath = path.join(distDir, file);
await fs.ensureDir(path.dirname(targetPath));
await fs.writeFile(targetPath, minifiedCss);
}
// 4. Process HTML Files (Minification)
const htmlFiles = await glob('*.html');
for (const file of htmlFiles) {
let content = await fs.readFile(file, 'utf8');
console.log(`🌐 Minifying HTML: ${file}`);
// Inject cosmetic protection script before minifying
const protectionScript = `
<script>
// Cosmetic Protection
document.addEventListener('contextmenu', event => event.preventDefault());
console.log('%c STOP! ', 'color: red; font-size: 40px; font-weight: bold; text-shadow: 2px 2px black;');
console.log('%cThis is a browser feature intended for developers. If someone told you to copy-paste something here to enable a feature, it is a scam.', 'font-size: 16px;');
</script>
`;
content = content.replace('</body>', `${protectionScript}</body>`);
const minifiedHtml = await minify(content, {
removeAttributeQuotes: true,
collapseWhitespace: true,
removeComments: true,
minifyJS: true,
minifyCSS: true
});
const targetPath = path.join(distDir, file);
await fs.writeFile(targetPath, minifiedHtml);
}
// 5. Copy README/License if needed (Optional)
// await fs.copy('README.md', path.join(distDir, 'README.md'));
console.log('✅ Build Complete! Production assets are in dist/');
}
build().catch(err => {
console.error('❌ Build Failed:', err);
process.exit(1);
});