Rollup Plugin for JavaScript Obfuscator
rollup-plugin-obfuscator is a powerful Rollup plugin designed to integrate `javascript-obfuscator` into the build process. Unlike some alternatives, this plugin requires `javascript-obfuscator` to be installed as a separate peer dependency, ensuring users can always utilize the latest version of the obfuscator. The current stable version is 1.1.0, with updates typically aligning with new releases or needs of `javascript-obfuscator`. A key differentiator is its ability to perform obfuscation either on the entire bundle or, more efficiently, on individual files while excluding open-source dependencies, leading to significant performance improvements during the build process. It provides granular control over which files are obfuscated via include/exclude patterns and supports all configuration options available in `javascript-obfuscator` itself.
Common errors
-
Error: Cannot find module 'javascript-obfuscator'
cause The `javascript-obfuscator` package, a peer dependency, has not been installed.fixInstall `javascript-obfuscator` alongside the plugin: `npm install --save-dev javascript-obfuscator`. -
TypeError: Cannot read properties of undefined (reading 'transform')
cause Often indicates an issue where the plugin factory didn't return a valid plugin object or `javascript-obfuscator` wasn't correctly loaded.fixEnsure `javascript-obfuscator` is installed and the plugin configuration is syntactically correct, particularly the `options` object. Check your Rollup configuration for correct plugin array syntax. -
Rollup 'transform' hook (plugin rollup-plugin-obfuscator) failed with error: Error: Options must be an object!
cause The `options` property passed to `rollup-plugin-obfuscator` is not a plain JavaScript object or is missing.fixVerify that the `options` property within the plugin configuration is a valid object, e.g., `obfuscator({ options: { /* ... */ } })`.
Warnings
- gotcha This plugin requires `javascript-obfuscator` to be installed separately as a peer dependency. Forgetting to install it will lead to runtime errors.
- gotcha When `global` option is `false` (default), obfuscation is applied per-file. This means `include` and `exclude` options determine which individual files are processed. Ensure these patterns correctly target your source code and avoid external dependencies.
- gotcha Overly aggressive `javascript-obfuscator` settings can break your application, especially with complex frameworks or dynamic code evaluations. Test thoroughly after applying obfuscation.
Install
-
npm install rollup-plugin-obfuscator -
yarn add rollup-plugin-obfuscator -
pnpm add rollup-plugin-obfuscator
Imports
- obfuscator
import { obfuscator } from 'rollup-plugin-obfuscator'; const obfuscator = require('rollup-plugin-obfuscator');import obfuscator from 'rollup-plugin-obfuscator';
- RollupOptions
import type { RollupOptions } from 'rollup'; - ObfuscatorOptions
import type { ObfuscatorOptions } from 'javascript-obfuscator';
Quickstart
import obfuscator from 'rollup-plugin-obfuscator';
export default {
input: 'src/main.js',
output: {
dir: 'dist',
format: 'esm'
},
plugins: [
obfuscator({
// Obfuscate the entire bundle (default is file-by-file)
// global: true,
options: {
// Your javascript-obfuscator options here
// See what's allowed: https://github.com/javascript-obfuscator/javascript-obfuscator
compact: true,
controlFlowFlattening: true,
deadCodeInjection: true,
identifierNamesGenerator: 'hexadecimal',
selfDefending: true,
sideEffects: true,
splitStrings: true,
stringArray: true,
stringArrayThreshold: 0.75
}
}),
]
}