@ismailnakkar/vite-obfuscator

中文文档
5.1.0发布于 2026/11/01MITTypeScriptES Module

Vite plugin that obfuscates the shipped JS island, with guards that fail the build on silent corruption.

原始使用文档
文档目录8 节
  1. Install
  2. Use
  3. VM markers
  4. Build failures
  5. Output
  6. Testing
  7. Upgrading
  8. From 4.x

@ismailnakkar/vite-obfuscator

Vite plugin that obfuscates the chunks of chosen entries with javascript-obfuscator, optionally running marked functions through its Pro VM, and fails the build when protected code would ship readable or a VM marker would be lost or moved.

Install

npm i -D @ismailnakkar/vite-obfuscator

Vite 8 and javascript-obfuscator 5.8.1+ are peers. The VM needs an obfuscator.io Pro API token.

Use

import obfuscator from '@ismailnakkar/vite-obfuscator';
import {defineConfig} from 'vite';

export default defineConfig({
    plugins: [
        process.env.OBFUSCATE === 'true' && obfuscator({
            include: ['src/widget/index.js', 'src/widget/embed.js'],
            islandRoots: ['src/widget'],
            apiToken: process.env.OBFUSCATOR_API_TOKEN ?? '',
            vmObfuscation: true,
        }),
    ],
});

Gate it as above to skip it; Vite ignores a falsy plugin.

A chunk is obfuscated when at least one entry reaches it and every entry that reaches it is in include. An islandRoots module bundled only into readable chunks fails the build.

When several HTML pages load the same script, Vite drops each page's own chunk, so the pages match no entry. Add that script to build.rolldownOptions.input and to include.

OptionDefault
includerequiredEntry source paths as written in vite.config, not entry names, no ./. Matched as a whole-segment suffix: 'app.js' matches every app.js entry.
islandRootsrequiredDirectories of the code to protect, relative to Vite's root.
vmObfuscationfalseRuns marked functions through the Pro VM. The build fails if apiToken is empty.
apiToken''Pro API token. Ignored without vmObfuscation.
vmObfuscationThreshold1VM only. Clamped to 0–1.
timeoutupstream'sVM only. Pro API request timeout in ms.
versionlatestVM only. The obfuscator version the Pro API runs.
rehashtrueRenames each obfuscated chunk after a hash of its new bytes. false for a script served at a fixed URL.
maxGzipBytes0Fails the build when an obfuscated chunk gzips to more bytes than this. 0 is off; each chunk's raw and gzip sizes are logged either way.
optionsPreset'high-obfuscation'javascript-obfuscator preset, under the plugin's own settings.
overrides{}javascript-obfuscator options, merged last.

high-obfuscation turns on debugProtection (a 4000 ms debugger loop) and disableConsoleOutput. Turn them off with overrides: {debugProtection: false, disableConsoleOutput: false}.

VM markers

Mark a function with a legal comment on the line above it:

/*! javascript-obfuscator:vm */
export function detect() { … }

A marker may lead a function declaration, a const/let/var holding a function or arrow function, an export of either, a method, or an object property or class field holding one. These are the positions the plugin accepts, not ones verified against the Pro VM. Marked modules may sit outside islandRoots.

The plugin sets build.rolldownOptions.output.comments to {legal: true} so markers survive minification; a later plugin must not override it. The build can still move a marker:

  • oxc (the default minifier) moves one on a const/let/var;
  • terser inlines a marked function called once;
  • Rolldown, minified or not, inlines a single-use object and moves its property markers.

Mark function declarations, which oxc and Rolldown leave in place. With terser, or to keep markers on const/let/var, set build.minify: false and minify the chunks the plugin does not obfuscate in a plugin of your own.

The build fails when a marker is lost between source and chunk, leads no function of its own, or marks a function exported where declared (export function f, export const f =) that was tree-shaken. Not detected: a marker that lands on an unmarked function, whether a minifier moved it or a dropped marked function not exported where declared left it behind. Delete marked functions you stop using.

Build failures

Every check fails the build before anything is written, with a message that names the cause. Two need more:

  • … bundled but NOT obfuscated: add the importing entry to include, or move the module out of islandRoots.
  • … is in an import cycle between obfuscated chunks: break the cycle.

Output

Importers, every emitted .html, and the build.manifest, build.ssrManifest and build.chunkImportMap files are rewritten to the rehashed names. An obfuscated chunk's sourcemap is dropped with a warning: its sourcesContent would ship the readable code.

The plugin runs in a post-order generateBundle, so a normal-order one of your own sees readable chunks.

Vite empties outDir before this runs: with emptyOutDir on, an in-place build serves no assets for the whole pass (minutes with the VM) and leaves the directory empty after a failure. Build into another directory and swap it in, or set build.emptyOutDir: false and prune stale assets yourself.

Testing

The plugin runs only in vite build, so app tests need no fake or setup. This package's suite: npm test.

Upgrading

From 4.x

5.0 ignores these options (TypeScript reports them as excess properties); remove them:

  • enable: a leftover enable: false now obfuscates. Gate the call instead (see Use).
  • vmTargetFunctionsMode, vmTargetFunctions, vmExcludeFunctions: mark functions with /*! javascript-obfuscator:vm */, now the only mode. overrides still reaches upstream's root mode, unguarded, and only a chunk with a marker reaches the VM.
  • mustContain: check chunk content in a normal-order generateBundle of your own.

版本与发布记录

@ismailnakkar/vite-obfuscator 共发布 10 个版本,首次发布于 2026/09/20;过去 12 个月发布了 10 个版本。

版本发布日期距今
5.1.0latest2026/11/012 天前
5.0.02026/10/2014 天前
4.0.12026/10/1915 天前
4.0.02026/10/1915 天前
3.0.12026/10/1816 天前
3.0.02026/10/1717 天前

关于 @ismailnakkar/vite-obfuscator 的常见问题

@ismailnakkar/vite-obfuscator 是什么?

Vite插件,它混淆了发货的JS岛,带有在静默损坏时构建失败的守卫。

如何安装 @ismailnakkar/vite-obfuscator?

在项目目录运行 npm install @ismailnakkar/vite-obfuscator,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vite(^8.0.0)、javascript-obfuscator(^5.8.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ismailnakkar/vite-obfuscator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ismailnakkar/vite-obfuscator 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ismailnakkar/vite-obfuscator 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^20.19.0 || >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ismailnakkar/vite-obfuscator 可以商用吗?

@ismailnakkar/vite-obfuscator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ismailnakkar/vite-obfuscator 还在维护吗?

最新版本 5.1.0 发布于 2026/11/01(2 天前);至今共发布 10 个版本,其中过去 12 个月发布了 10 个;首次发布于 2026/09/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。