Vite JavaScript Obfuscator Plugin
vite-plugin-javascript-obfuscator is a Vite plugin designed to integrate the `javascript-obfuscator` library into Vite projects, enabling code obfuscation during the build or serve process. The current stable version is 3.1.0. This plugin allows developers to apply various obfuscation techniques to their JavaScript, JSX, TypeScript, and TSX files, helping to protect source code from reverse engineering and tampering. It provides a configurable interface to pass options directly to `javascript-obfuscator`, including `debugProtection`, `controlFlowFlattening`, and many others, allowing fine-grained control over the obfuscation intensity and effects. Its key differentiator is seamless integration within the Vite ecosystem, leveraging Vite's HMR for development and optimized builds for production, making it easy to add a layer of code protection without complex build-tool configurations. There is no publicly stated strict release cadence, but updates align with `javascript-obfuscator` and Vite ecosystem changes.
Common errors
-
Files are not being obfuscated.
cause The `include` and `exclude` options are not correctly configured, or the `apply` option prevents obfuscation in the current environment (e.g., 'serve' when building).fixVerify that `include` patterns correctly match the target files and `exclude` patterns are not overly broad. Ensure `apply` is set to 'build' (for production builds) or omitted (for both serve and build). -
Build fails after enabling obfuscation or application throws runtime errors.
cause Aggressive obfuscation settings (e.g., `debugProtection`, `controlFlowFlattening`) may introduce breaking changes to the application's logic or incompatible syntax.fixStart with minimal obfuscation options and gradually enable more aggressive settings, testing thoroughly after each change. Specifically review `javascript-obfuscator` options that might interfere with reflection, string access, or environment-specific code. -
ReferenceError: defineConfig is not defined
cause Missing the necessary import for `defineConfig` in `vite.config.js`.fixAdd `import { defineConfig } from 'vite';` at the top of your `vite.config.js` file.
Warnings
- gotcha Extensive obfuscation, especially with aggressive settings like `debugProtection` or `controlFlowFlattening`, can significantly increase build times and runtime performance overhead. It also makes debugging deployed applications extremely difficult.
- gotcha Aggressive obfuscation can inadvertently break application functionality, particularly in areas involving reflection, specific string patterns, or third-party libraries that rely on predictable code structure.
- gotcha Obfuscation does not provide absolute security. While it makes reverse engineering harder, it cannot prevent a determined attacker from eventually understanding or modifying the code. It should be part of a broader security strategy.
- deprecated Older versions of `javascript-obfuscator` and consequently this plugin might have less effective obfuscation techniques or introduce new vulnerabilities. Always ensure you are using a relatively recent and well-maintained version.
Install
-
npm install vite-plugin-javascript-obfuscator -
yarn add vite-plugin-javascript-obfuscator -
pnpm add vite-plugin-javascript-obfuscator
Imports
- obfuscatorPlugin
import { obfuscatorPlugin } from 'vite-plugin-javascript-obfuscator';import obfuscatorPlugin from 'vite-plugin-javascript-obfuscator';
- defineConfig
import { defineConfig } from 'vite';
Quickstart
import { defineConfig } from 'vite';
import obfuscatorPlugin from 'vite-plugin-javascript-obfuscator';
export default defineConfig({
plugins: [
obfuscatorPlugin({
// Apply obfuscation only during the build process
apply: 'build',
// Configure files to include (e.g., specific paths or regex)
include: ['src/**/*.js', 'src/**/*.jsx', 'src/**/*.ts', 'src/**/*.tsx'],
// Basic javascript-obfuscator options for robust protection
options: {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
debugProtection: true,
debugProtectionInterval: true,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: true,
simplify: true,
splitStrings: true,
stringArray: true,
stringArrayCallsTransform: true,
stringArrayCallsTransformThreshold: 1,
stringArrayEncoding: ['base64'],
stringArrayIndexShift: true,
stringArrayRotate: true,
stringArrayShuffle: true,
stringArrayWrappersCount: 5,
stringArrayWrappersType: 'function',
stringArrayThreshold: 1,
transformObjectKeys: true,
unicodeEscapeSequence: true
}
})
]
});