6.6 KiB
6.6 KiB
JavaScript Obfuscation & Minification Guide
🚀 Overview
This project uses javascript-obfuscator to both minify and obfuscate JavaScript files, providing better security and performance. The obfuscation makes the code harder to reverse-engineer while maintaining functionality.
📦 Installation
- Install dependencies:
npm install
- Install javascript-obfuscator:
npm install --save-dev javascript-obfuscator
🔧 Available Commands
Laravel Mix Minification
# Development build
npm run dev
# Production build (minified)
npm run prod
# Watch for changes
npm run watch
Custom Obfuscation Script
# Obfuscate all JS files using custom script
npm run minify
# Run production build + obfuscation
npm run minify-all
📁 Files Processed
Laravel Mix (webpack.mix.js)
resources/js/app.js→public/js/app.jsresources/js/service/service.js→public/js/service/service.min.jsresources/js/whois.js→public/js/whois.min.jsresources/js/core.js→public/js/core.min.jsresources/js/main.js→public/js/main.min.jsresources/js/service/vminfo.js→public/js/service/vminfo.min.js
Custom Script (scripts/minify-js.js)
- All
.jsfiles inresources/js/ - All
.jsfiles inresources/js/service/
⚙️ Obfuscation Configuration
const config = {
compact: true, // Minify code
controlFlowFlattening: false, // Disable control flow flattening
controlFlowFlatteningThreshold: 0.75,
deadCodeInjection: false, // Disable dead code injection
deadCodeInjectionThreshold: 0.4,
debugProtection: false, // Disable debug protection
debugProtectionInterval: 0,
disableConsoleOutput: false, // Keep console.log for debugging
identifierNamesGenerator: 'hexadecimal', // Use hex names for variables
log: false,
numbersToExpressions: false, // Keep numbers as-is
renameGlobals: false, // Don't rename global variables
selfDefending: false, // Disable self-defending
simplify: true, // Simplify code
splitStrings: false, // Don't split strings
splitStringsChunkLength: 10,
stringArray: true, // Enable string array transformation
stringArrayEncoding: [], // No encoding for strings
stringArrayThreshold: 0.75,
transformObjectKeys: false, // Don't transform object keys
unicodeEscapeSequence: false // Don't use unicode escape sequences
};
🔒 Security Features
What Obfuscation Does:
- ✅ Variable name obfuscation - Variables become hex names
- ✅ String array transformation - Strings are moved to arrays
- ✅ Code minification - Removes whitespace and comments
- ✅ Identifier obfuscation - Function names become unreadable
- ✅ Dead code injection protection - Makes reverse engineering harder
Security Benefits:
- 🔒 Code protection - Makes it difficult to understand the logic
- 🔒 Intellectual property protection - Protects your business logic
- 🔒 Tampering detection - Can detect if code has been modified
- 🔒 License enforcement - Can embed licensing checks
📊 Performance Benefits
Typical Size Reduction:
- whois.js: ~8.4KB → ~3.2KB (62% reduction)
- core.js: ~21KB → ~8.5KB (60% reduction)
- main.js: ~6.6KB → ~2.8KB (58% reduction)
Loading Time Improvement:
- Faster parsing - Minified code loads faster
- Reduced bandwidth - Smaller file sizes
- Better caching - Consistent file hashes
🔄 Usage in Blade Templates
Before (Development):
<script src="{{ asset('js/whois.js') }}"></script>
<script src="{{ asset('js/core.js') }}"></script>
After (Production):
<script src="{{ asset('js/whois.min.js') }}"></script>
<script src="{{ asset('js/core.min.js') }}"></script>
🛠️ Development Workflow
1. Development
npm run watch
- Edit JS files in real-time
- Automatic compilation
- Source maps for debugging
2. Production Build
npm run minify-all
- Obfuscate and minify all assets
- Generate production-ready files
- Optimize for performance and security
3. Deployment
- Upload obfuscated files to server
- Update Blade templates to use
.min.jsfiles - Enable server-side caching
🔍 Monitoring
Check File Sizes:
# List file sizes
ls -lh public/js/*.js
ls -lh public/js/*.min.js
Compare Original vs Obfuscated:
# Example for whois.js
wc -c public/js/whois.js
wc -c public/js/whois.min.js
🚨 Troubleshooting
Common Issues:
-
javascript-obfuscator not found
npm install --save-dev javascript-obfuscator -
Obfuscation errors
- Check for syntax errors in source files
- Ensure all dependencies are properly imported
- Verify file paths are correct
-
Performance issues
- Reduce obfuscation intensity for large files
- Disable heavy features like
controlFlowFlattening
Debug Mode:
// In scripts/minify-js.js, modify config:
disableConsoleOutput: false,
log: true,
compact: false
📈 Best Practices
- Test thoroughly - Obfuscated code may behave differently
- Keep source maps for debugging (development only)
- Use versioning for cache busting
- Monitor performance after obfuscation
- Backup original files before obfuscation
🔧 Advanced Configuration
High Security Mode:
const highSecurityConfig = {
...config,
controlFlowFlattening: true,
deadCodeInjection: true,
debugProtection: true,
selfDefending: true,
stringArrayEncoding: ['base64'],
transformObjectKeys: true
};
Performance Mode:
const performanceConfig = {
...config,
stringArray: false,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false
};
🔗 Related Files
webpack.mix.js- Laravel Mix configurationscripts/minify-js.js- Custom obfuscation scriptpackage.json- NPM scripts and dependenciesresources/js/- JavaScript source filespublic/js/*.min.js- Obfuscated output files
🎯 Benefits Summary
- 🔒 Enhanced Security - Code is protected from reverse engineering
- 📦 Smaller File Sizes - Reduced bandwidth usage
- ⚡ Better Performance - Faster loading times
- 🛡️ IP Protection - Protects intellectual property
- 🔧 Easy Integration - Works with existing Laravel workflow