Gulp JavaScript Obfuscator
gulp-javascript-obfuscator is a Gulp plugin that integrates the powerful `javascript-obfuscator` library into Gulp build workflows. Currently at version 1.1.6, this package provides a stream-based interface for obfuscating JavaScript files, adding a layer of protection against reverse engineering and making code harder to read. This plugin is typically stable, with its release cadence tied to updates in the underlying `javascript-obfuscator` for feature enhancements and Gulp-specific maintenance. A key differentiator is its seamless integration with `gulp-sourcemaps`, which allows for proper debugging of obfuscated code by automatically generating corresponding source maps. This ensures a robust and maintainable obfuscation process within existing Gulp pipelines and supports chaining with other Gulp plugins like Babel for pre-processing.
Common errors
-
Error in plugin "gulp-javascript-obfuscator" Message: The number of constructor arguments in the derived class t must be >= than the number of constructor arguments of its base class.
cause Incompatibility between `javascript-obfuscator`'s internal dependencies and specific Node.js versions (e.g., Node.js 13.6+).fixDowngrade your Node.js version to a compatible LTS release (e.g., Node.js 12.x) or check for newer versions of `gulp-javascript-obfuscator` and `javascript-obfuscator` that support your Node.js version. -
SyntaxError: Unexpected token ... (related to spread syntax in `chalk` or other dependencies)
cause Older Node.js environments or misconfigured transpilation pipelines struggling with modern JavaScript syntax used by `javascript-obfuscator` or its sub-dependencies.fixEnsure your Node.js version supports the syntax used by the library and its dependencies, or use a transpiler like Babel if targeting older environments. Check `javascript-obfuscator`'s compatibility matrix. -
TypeError: javascriptObfuscator is not a function
cause The `gulp-javascript-obfuscator` module was not correctly `require`d, or the variable name does not match the imported symbol.fixVerify that `const javascriptObfuscator = require('gulp-javascript-obfuscator');` is correctly placed and that `javascriptObfuscator` is used as a function in the `.pipe()` chain. -
Error: Cannot find module 'vinyl-sourcemaps-apply'
cause A dependency issue where `gulp-sourcemaps` or `gulp-javascript-obfuscator` cannot find a required sub-dependency for sourcemap processing.fixRun `npm install` or `yarn install` again to ensure all transitive dependencies are correctly installed. This can sometimes be resolved by clearing the npm cache (`npm cache clean --force`).
Warnings
- breaking When using `gulp-sourcemaps` for source map generation, the `sourceMap` option within `javascript-obfuscator` configurations MUST NOT be set. The plugin handles source map integration automatically, and explicitly setting `sourceMap: true` will cause conflicts or incorrect behavior.
- deprecated The method of generating source maps by directly setting `sourceMap: true` in the `javascript-obfuscator` options (without `gulp-sourcemaps`) is deprecated. This older approach generates a `.map` file directly into the Gulp stream but is not recommended for future use and may be removed in later versions.
- gotcha Older Node.js versions (e.g., 13.6 and above) have been reported to cause 'Spread Syntax error' or 'The number of constructor arguments in the derived class must be >= than the number of constructor arguments of its base class' when using `gulp-javascript-obfuscator` due to incompatibilities with underlying dependencies.
Install
-
npm install gulp-javascript-obfuscator -
yarn add gulp-javascript-obfuscator -
pnpm add gulp-javascript-obfuscator
Imports
- javascriptObfuscator
import javascriptObfuscator from 'gulp-javascript-obfuscator';
const javascriptObfuscator = require('gulp-javascript-obfuscator'); - gulp
import gulp from 'gulp';
const gulp = require('gulp'); - sourcemaps
import sourcemaps from 'gulp-sourcemaps';
const sourcemaps = require('gulp-sourcemaps');
Quickstart
const gulp = require('gulp');
const javascriptObfuscator = require('gulp-javascript-obfuscator');
const sourcemaps = require('gulp-sourcemaps');
const babel = require('@babel/core'); // Optional: for pre-processing JS
const babelPresetEnv = require('@babel/preset-env'); // Optional: for Babel preset
gulp.task('obfuscate-js', () => {
return gulp.src('src/**/*.js')
.pipe(sourcemaps.init()) // Initialize sourcemaps before any transformations
.pipe(babel({
presets: [babelPresetEnv]
})) // Optional: Transpile modern JS to a compatible version
.pipe(javascriptObfuscator({
compact: true, // Example option: compact the obfuscated code
// Other javascript-obfuscator options can go here
// IMPORTANT: Do NOT set `sourceMap: true` here when using gulp-sourcemaps
}))
.pipe(sourcemaps.write('.')) // Write sourcemaps to the same directory as output
.pipe(gulp.dest('dist')); // Output obfuscated and sourcemap files to 'dist'
});
// To run this task: npx gulp obfuscate-js