JavaScript obfuscator plugin for Vite environments
npm install vite-plugin-bundle-obfuscator
适用于Vite环境的JavaScript混淆器插件






📝 更新日志 · [🐛 报告问题][github-issues-link] · [✨ 请求功能][github-pr-link]
English | 中文

[npm-release-shield]: https://img.shields.io/npm/v/vite-plugin-bundle-obfuscator?style=flat&colorA=18181B&colorB=28CF8D&logo=npm
[npm-downloads-shield]: https://img.shields.io/npm/dt/vite-plugin-bundle-obfuscator?style=flat&colorA=18181B&colorB=28CF8D&logo=npm
[npm-release-link]: https://www.npmjs.com/package/vite-plugin-bundle-obfuscator
[github-releasedate-shield]: https://img.shields.io/github/release-date/z0ffy/vite-plugin-bundle-obfuscator?style=flat&colorA=18181B&colorB=a855f7
[github-releasedate-link]: https://github.com/z0ffy/vite-plugin-bundle-obfuscator/releases
[github-license-shield]: https://img.shields.io/github/license/z0ffy/vite-plugin-bundle-obfuscator?style=flat&colorA=18181B&colorB=white
[github-license-link]: https://github.com/z0ffy/vite-plugin-bundle-obfuscator/blob/main/LICENSE
[github-issues-link]: https://github.com/z0ffy/vite-plugin-bundle-obfuscator/issues
[github-pr-link]: https://github.com/z0ffy/vite-plugin-bundle-obfuscator/pulls
- ✅ ⚡ 支持Vite项目中的JavaScript混淆。
- ✅ 🚀 多线程支持,以获得更好的性能。
- ✅ ⚙️ 可定制的混淆器选项,以满足您的需求。
- ✅ 🛡️ 自动排除node_modules。
- ✅ 📦 支持node_modules拆分块。
- 如果遇到内存溢出,修改打包命令为"build": "cross-env NODE_OPTIONS=--max-old-space-size=8192 vite build",
max-old-space-size的值根据配置自行设置。
- 在设置node_modules分包时,请把准确的包名前置。例如:["vue-router", "vue"],"vue"可以同时匹配到vue以及vue-router。
✦ Vite - Vanilla
✦ Vite - Vue
✦ Vite - React
✦ Vite - PReact
✦ Vite - lit
✦ Vite - Svelte
✦ Vite - Solid
✦ Vite - Qwik
✦ ...
``bash使用npm
npm install vite-plugin-bundle-obfuscator -D
👨💻 使用
1. 使用您首选的软件包管理器安装插件。
2. 在
vite.config.js中注册插件。
3. 自定义混淆器配置或使用默认选项。示例:
`javascript
import vitePluginBundleObfuscator from 'vite-plugin-bundle-obfuscator';// 全部配置
const allObfuscatorConfig = {
excludes: [],
enable: true,
log: true,
autoExcludeNodeModules: false,
// autoExcludeNodeModules: { enable: true, manualChunks: ['vue'] }
threadPool: false,
// 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)
]
};
// 简化配置
const minimizeObfuscatorConfig = {
autoExcludeNodeModules: true,
// autoExcludeNodeModules: { enable: true, manualChunks: ['vue'] }
threadPool: true,
// threadPool: { enable: true, size: 4 }
};
export default {
plugins: [
vitePluginBundleObfuscator(minimizeObfuscatorConfig)
]
};
// 默认配置
export default {
plugins: [
vitePluginBundleObfuscator()
]
};
`🧵 Web Worker 支持
默认不会对
?worker / new Worker(new URL(...)) 生成的 worker bundle 进行混淆。启用 worker 混淆:
`js
import vitePluginBundleObfuscator from 'vite-plugin-bundle-obfuscator';export default {
plugins: [
vitePluginBundleObfuscator({
obfuscateWorker: true,
}),
],
};
`仅对 worker 额外排除某些文件:
`js
vitePluginBundleObfuscator({
obfuscateWorkerExcludes: [/comlink\\.worker.*\\.js$/],
});
`🚀 性能比较
拥有 7000+ modules 和 400+ bundles 在 4C 8G 机器上:
- ThreadPool Enabled : 🟩🟩🟩⬜⬜⬜⬜⬜⬜ (大约30秒)
- ThreadPool Disabled : 🟥🟥🟥🟥🟥🟥🟥🟥🟥 (大约90秒)
🛠️ 选项
| 属性 | 描述 | 类型 | 默认值 | 版本 |
|------------------------|----------------------------|-------------------------------------------------------------------------------------|-------------------------|--------------------------------------|
| threadPool | 线程池的配置。 | boolean \| ({ enable: true; size: number } \| { enable: false }) | false | v1.2.0 |
| apply | 仅将插件应用于服务或构建,或在特定条件下。 | 'serve' \| 'build' \| ((this: void, config: UserConfig, env: ConfigEnv) => boolean) | build | v1.1.0 |
| autoExcludeNodeModules | 启用自动排除node_modules。 | boolean \| ({ enable: true; manualChunks: string[] } \| { enable: false }) | false | v1.0.9(原本为boolean,在v1.3.0版本中扩展到当前类型) |
| obfuscateWorker | 启用或禁用 Web Worker 产物混淆。 | boolean \| { enable: boolean } | false | v1.10.0 |
| obfuscateWorkerExcludes | 仅对 worker 额外排除(最终:
excludes + obfuscateWorkerExcludes`)。 | (RegExp \| string)[] | [] | v1.10.0 |MIT License Copyright (c) 2024-present, Zoffy