Grunt JavaScript Obfuscator
grunt-javascript-obfuscator is a Grunt plugin designed to integrate the powerful `javascript-obfuscator` library into Grunt-based build workflows. It allows developers to automate the process of obfuscating JavaScript source files, enhancing code protection and intellectual property concealment. The current stable version is 1.2.0, released in August 2019, indicating a very slow release cadence; it effectively acts as a wrapper around the core `javascript-obfuscator` library. Key differentiators include its ease of integration with existing Grunt setups and direct exposure of all `javascript-obfuscator` options, enabling fine-grained control over the obfuscation process. It requires Grunt `>=0.4.5` and `javascript-obfuscator >=0.7.2` as peer dependencies, meaning users must install both the plugin and the underlying obfuscation library separately.
Common errors
-
Warning: Task "javascript_obfuscator" not found. Use --force to continue.
cause The Grunt plugin `grunt-javascript-obfuscator` has not been loaded in your Gruntfile.fixAdd `grunt.loadNpmTasks('grunt-javascript-obfuscator');` to your `Gruntfile.js`. -
Fatal error: Peer dependency 'javascript-obfuscator@>=0.7.2' not met. Please install 'javascript-obfuscator@>=0.7.2' manually.
cause The required peer dependency `javascript-obfuscator` is not installed or its version does not meet the specified requirement.fixRun `npm install javascript-obfuscator --save-dev` to install the core obfuscator library alongside the Grunt plugin. -
Warning: grunt-javascript-obfuscator: `files` or `src` is required.
cause The `javascript_obfuscator` task is missing the `files` or `src` configuration to specify which JavaScript files should be obfuscated.fixConfigure your task target with either a `files` object (e.g., `files: { 'dest.js': ['src1.js', 'src2.js'] }`) or a `src` array (e.g., `src: ['src/*.js']`).
Warnings
- gotcha The package's peer dependency for Grunt (`>=0.4.5`) is very old. While it might work with newer Grunt versions, compatibility issues with modern Grunt features or Node.js versions are possible.
- breaking Options passed to `grunt-javascript-obfuscator` are directly forwarded to `javascript-obfuscator`. The underlying `javascript-obfuscator` library has undergone many changes and additions since this plugin's last release (v1.2.0 in 2019). Options might have changed names, been deprecated, or introduced new behaviors.
- gotcha JavaScript obfuscation, while increasing difficulty, does not provide encryption or absolute security for your code. It primarily deters casual inspection and makes reverse-engineering more complex.
- gotcha Source maps were introduced in v1.2.0, but generating source maps for highly obfuscated code can still be challenging. Debugging obfuscated code, even with source maps, is significantly harder than debugging original code.
Install
-
npm install grunt-javascript-obfuscator -
yarn add grunt-javascript-obfuscator -
pnpm add grunt-javascript-obfuscator
Imports
- grunt.loadNpmTasks
import { javascriptObfuscator } from 'grunt-javascript-obfuscator';grunt.loadNpmTasks('grunt-javascript-obfuscator'); - javascript_obfuscator task configuration
grunt.registerTask('obfuscate', ['javascript_obfuscator']);grunt.initConfig({ javascript_obfuscator: { ... } }); - Task Options
options: { debugProtection: true, debugProtectionInterval: true }
Quickstart
npm install grunt grunt-cli grunt-javascript-obfuscator javascript-obfuscator --save-dev
// src/input.js
(function(){
var sensitiveData = 'this is a secret value';
function performCalculation(a, b) {
return a * b;
}
console.log("Result: ", performCalculation(5, 10));
if (process.env.NODE_ENV === 'production') {
console.log(sensitiveData);
}
})();
// Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
javascript_obfuscator: {
options: {
compact: true,
controlFlowFlattening: true,
deadCodeInjection: true,
debugProtection: true,
debugProtectionInterval: true,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
renameProperties: false,
selfDefending: true,
simplify: true,
splitStrings: true,
stringArray: true,
stringArrayEncoding: ['base64'],
stringArrayThreshold: 0.75,
transformObjectKeys: true,
unicodeEscapeSequence: false,
sourceMap: true // Added in v1.2.0
},
dist: {
files: {
'dist/obfuscated.js': ['src/input.js']
}
}
}
});
grunt.loadNpmTasks('grunt-javascript-obfuscator');
grunt.registerTask('default', ['javascript_obfuscator']);
};