Webpack Obfuscator Plugin and Loader
webpack-obfuscator is a JavaScript Obfuscator plugin and loader designed specifically for Webpack 5 and above. It integrates the robust `javascript-obfuscator` library into the Webpack build process, allowing developers to protect their JavaScript code by making it harder to read and reverse-engineer. The current stable version is 3.6.1. Releases appear to be frequent, focusing on dependency updates and minor enhancements, indicating an active maintenance schedule. A key differentiator is its seamless integration as both a Webpack plugin and a loader, offering flexibility in how obfuscation is applied, including the ability to exclude specific bundles or files. It also supports the obfuscator.io Pro API for advanced, cloud-based obfuscation techniques, requiring `javascript-obfuscator` version 5.0.0 or higher for this feature.
Common errors
-
TypeError: WebpackObfuscator is not a constructor
cause Incorrect import statement (e.g., using named import for a default export) or trying to `require` a pure ESM package.fixFor CommonJS, `const WebpackObfuscator = require('webpack-obfuscator');` is shown in the README. For ESM, use `import WebpackObfuscator from 'webpack-obfuscator';` -
Error: Cannot find module 'javascript-obfuscator'
cause The `javascript-obfuscator` package, a peer dependency, is not installed.fixInstall the peer dependency: `npm install --save-dev javascript-obfuscator`. -
Error: The 'options' property is only allowed for 'plugins' with a 'tap' hook
cause Attempting to pass loader options directly to the `WebpackObfuscator` plugin constructor without nesting them under `options` for the loader itself, or mistaking plugin usage for loader usage.fixWhen using `WebpackObfuscator.loader`, options should be nested under `options` in the `use` object: `{ use: { loader: WebpackObfuscator.loader, options: { rotateStringArray: true } } }`. -
Webpack compilation fails or code is not obfuscated as expected (Webpack 4 user)
cause Using `webpack-obfuscator` v3.x with Webpack 4, which is incompatible.fixDowngrade `webpack-obfuscator` to a 2.x version (e.g., `npm install webpack-obfuscator@^2.0.0 --save-dev`) or upgrade your project to Webpack 5.
Warnings
- breaking Version 3.0.0 and higher of `webpack-obfuscator` are only compatible with Webpack 5 and above. Older Webpack versions (e.g., Webpack 4) require `webpack-obfuscator` version 2.x.
- breaking Starting with version 3.0.0, `javascript-obfuscator` is a peer dependency and must be installed explicitly alongside `webpack-obfuscator`. Previously, it was a direct dependency.
- gotcha Using the Pro API for cloud-based obfuscation requires `javascript-obfuscator` version 5.0.0 or higher. Ensure your `javascript-obfuscator` peer dependency meets this requirement if you intend to use Pro API features.
- gotcha When using the loader, it's recommended to add `enforce: 'post'` to your webpack rule. This ensures the obfuscator loader runs after all other normal loaders, preventing potential conflicts or issues where other loaders might interfere with the obfuscation process.
- gotcha The `excludes` option for the plugin takes bundle names (output file names), not source file names. When using `[name].js` in webpack's output, these are the names to exclude. For the loader, `exclude` takes source file paths.
Install
-
npm install webpack-obfuscator -
yarn add webpack-obfuscator -
pnpm add webpack-obfuscator
Imports
- WebpackObfuscator
const WebpackObfuscator = require('webpack-obfuscator');import WebpackObfuscator from 'webpack-obfuscator';
- WebpackObfuscator.loader
import { loader } from 'webpack-obfuscator'; // The loader is a static property on the default export.import WebpackObfuscator from 'webpack-obfuscator'; // then use WebpackObfuscator.loader
- WebpackObfuscator Plugin Usage (TypeScript)
import WebpackObfuscator from 'webpack-obfuscator'; const config: Configuration = { // ... plugins: [ new WebpackObfuscator({ rotateStringArray: true }, ['excluded_bundle_name.js']) ] };
Quickstart
const path = require('path');
const WebpackObfuscator = require('webpack-obfuscator');
module.exports = {
mode: 'production',
entry: {
'main': './src/index.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
plugins: [
new WebpackObfuscator({
rotateStringArray: true,
stringArrayThreshold: 0.75
}, ['main.js'])
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
enforce: 'post',
use: {
loader: WebpackObfuscator.loader,
options: {
compact: true
}
}
}
]
}
};