vite-plugin-bundle-obfuscator
Version:
JavaScript obfuscator plugin for Vite environments
208 lines (165 loc) β’ 10.1 kB
Markdown
<div align="center">
<img height="160" src="https://obfuscator.io/logo.png" alt="vite-plugin-bundle-obfuscator logo" />
JavaScript `obfuscator` plugin for `Vite` environments
[](https://github.com/vitejs/awesome-vite)
[](https://www.npmjs.com/package/vite-plugin-bundle-obfuscator)
[](https://www.npmjs.com/package/vite-plugin-bundle-obfuscator)
[](https://github.com/z0ffy/vite-plugin-bundle-obfuscator/releases)
[](https://codecov.io/gh/z0ffy/vite-plugin-bundle-obfuscator)
[](https://github.com/z0ffy/vite-plugin-bundle-obfuscator/blob/main/LICENSE)
[π Changelog](./CHANGELOG.md) Β· [π Report Bug][github-issues-link] Β· [β¨ Request Feature][github-pr-link]
<p align="center">
<strong>English</strong> | <a href="./README.zh-CN.md">δΈζ</a>
</p>

</div>
<!-- Badge Links -->
[]: https://img.shields.io/npm/v/vite-plugin-bundle-obfuscator?style=flat&colorA=18181B&colorB=28CF8D&logo=npm
[]: https://img.shields.io/npm/dt/vite-plugin-bundle-obfuscator?style=flat&colorA=18181B&colorB=28CF8D&logo=npm
[]: https://www.npmjs.com/package/vite-plugin-bundle-obfuscator
[]: https://img.shields.io/github/release-date/z0ffy/vite-plugin-bundle-obfuscator?style=flat&colorA=18181B&colorB=a855f7
[]: https://github.com/z0ffy/vite-plugin-bundle-obfuscator/releases
[]: https://img.shields.io/github/license/z0ffy/vite-plugin-bundle-obfuscator?style=flat&colorA=18181B&colorB=white
[]: https://github.com/z0ffy/vite-plugin-bundle-obfuscator/blob/main/LICENSE
[]: https://github.com/z0ffy/vite-plugin-bundle-obfuscator/issues
[]: https://github.com/z0ffy/vite-plugin-bundle-obfuscator/pulls
- β
β‘ Supports `JavaScript obfuscation` in `Vite` projects.
- β
π Multi-threading support for better performance.
- β
βοΈ Customizable obfuscator options to fit your needs.
- β
π‘οΈ Auto-excludes `node_modules`.
- β
π¦ Support the `node_modules` split chunk.
- If a memory overflow occurs, modify the packaging command to
`"build": "cross-env NODE_OPTIONS=--max-old-space-size=8192 vite build"`, where `max-old-space-size` is set according
to the configuration.
- When setting up `node_modules` split chunk, please place the accurate package name at the front. For
example: ["vue-router", "vue"], "vue" can match both "vue" and "vue-router" at the same time.
β¦ [Vite - Vanilla](https://stackblitz.com/edit/vitejs-vite-zsytij?file=vite.config.js)
β¦ [Vite - Vue](https://stackblitz.com/edit/vitejs-vite-ywho91?file=vite.config.js)
β¦ [Vite - React](https://stackblitz.com/edit/vitejs-vite-wyeur4?file=vite.config.js)
β¦ [Vite - PReact](https://stackblitz.com/edit/vitejs-vite-oujmks?file=vite.config.js)
β¦ [Vite - lit](https://stackblitz.com/edit/vitejs-vite-ru4gws?file=vite.config.js)
β¦ [Vite - Svelte](https://stackblitz.com/edit/vitejs-vite-fthdtu?file=vite.config.js)
β¦ [Vite - Solid](https://stackblitz.com/edit/vitejs-vite-dcx3eh?file=vite.config.js)
β¦ [Vite - Qwik](https://stackblitz.com/edit/vitejs-vite-i2bjvq?file=vite.config.js)
β¦ ...
```bash
npm install vite-plugin-bundle-obfuscator -D
pnpm add vite-plugin-bundle-obfuscator -D
yarn add vite-plugin-bundle-obfuscator -D
```
1. Install the plugin using your preferred package manager.
2. Register the plugin in `vite.config.js`
3. Customize the obfuscator configuration or use the default options.
Example:
```javascript
import vitePluginBundleObfuscator from 'vite-plugin-bundle-obfuscator';
// All configurations
const allObfuscatorConfig = {
excludes: [],
enable: true,
log: true,
autoExcludeNodeModules: true,
// autoExcludeNodeModules: { enable: true, manualChunks: ['vue'] }
threadPool: true,
// threadPool: { enable: true, size: 4 }
options: {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
deadCodeInjection: false,
debugProtection: false,
debugProtectionInterval: 0,
disableConsoleOutput: false,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: false,
renameGlobals: false,
selfDefending: true,
simplify: true,
splitStrings: false,
ignoreImports: true,
stringArray: true,
stringArrayCallsTransform: true,
stringArrayCallsTransformThreshold: 0.5,
stringArrayEncoding: [],
stringArrayIndexShift: true,
stringArrayRotate: true,
stringArrayShuffle: true,
stringArrayWrappersCount: 1,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersParametersMaxCount: 2,
stringArrayWrappersType: 'variable',
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false,
}
};
export default {
plugins: [
vitePluginBundleObfuscator(allObfuscatorConfig)
]
};
// Simplified configurations
const minimizeObfuscatorConfig = {
autoExcludeNodeModules: true,
// autoExcludeNodeModules: { enable: true, manualChunks: ['vue'] }
threadPool: true,
// threadPool: { enable: true, size: 4 }
};
export default {
plugins: [
vitePluginBundleObfuscator(minimizeObfuscatorConfig)
]
};
// Default configurations
export default {
plugins: [
vitePluginBundleObfuscator()
]
};
```
By default, worker bundles generated from `?worker` / `new Worker(new URL(...))` are **not** obfuscated.
Enable worker obfuscation:
```js
import vitePluginBundleObfuscator from 'vite-plugin-bundle-obfuscator';
export default {
plugins: [
vitePluginBundleObfuscator({
obfuscateWorker: true,
}),
],
};
```
Exclude extra files only for workers:
```js
vitePluginBundleObfuscator({
obfuscateWorkerExcludes: [/comlink\\.worker.*\\.js$/],
});
```
With **7000+ modules** and **400+ bundles** on a **4C 8G** machine:
- **ThreadPool Enabled** : π©π©π©β¬β¬β¬β¬β¬β¬ (About 30 seconds)
- **ThreadPool Disabled** : π₯π₯π₯π₯π₯π₯π₯π₯π₯ (About 90 seconds)
| Property Name | Description | Type | Default | Version |
|------------------------|-------------------------------------------------------------------------|-------------------------------------------------------------------------------------|-------------------------|-----------------------------------------------------------------|
| threadPool | Configuration for the thread pool. | boolean \| ({ enable: true; size: number } \| { enable: false }) | false | v1.2.0 |
| apply | Apply the plugin only for serve or build, or on certain conditions. | 'serve' \| 'build' \| ((this: void, config: UserConfig, env: ConfigEnv) => boolean) | build | v1.1.0 |
| autoExcludeNodeModules | Enable auto exclude node_modules. | boolean \| ({ enable: true; manualChunks: string[] } \| { enable: false }) | false | v1.0.9 (originally boolean, extended to current type in v1.3.0) |
| obfuscateWorker | Enable or disable obfuscation for Web Worker bundles. | boolean \| { enable: boolean } | false | v1.10.0 |
| obfuscateWorkerExcludes | Additional excludes only for workers (final: `excludes + obfuscateWorkerExcludes`). | (RegExp \| string)[] | [] | v1.10.0 |
| log | Show or hide log output. | boolean | true | v1.0.4 |
| enable | Enable or disable the obfuscator. | boolean | true | v1.0.1 |
| excludes | Bundle names to be excluded. Starting from v1.0.8, RegExp is supported. | (RegExp \| string)[] | [] | v1.0.0 |
| options | Options for the JavaScript obfuscator. | ObfuscatorOptions | defaultObfuscatorConfig | v1.0.0 |
## π License
[MIT](https://opensource.org/licenses/MIT) License Copyright (c) 2024-present, Zoffy