[
  {
    "path": ".gitignore",
    "content": "node_modules\ndist"
  },
  {
    "path": ".prettierignore",
    "content": "README.md"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2022 elmeet\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# vite-plugin-javascript-obfuscator\n\nA Vite Plugin for [javascript-obfuscator](https://www.npmjs.com/package/javascript-obfuscator)\n\n## Installation\n\nInstall the package:\n\n- npm `npm install --save-dev vite-plugin-javascript-obfuscator`\n- yarn `yarn add --dev vite-plugin-javascript-obfuscator`\n- pnpm `pnpm i vite-plugin-javascript-obfuscator -D`\n\n## Usage\n\n### Example 1\n\nvite.config.js\n\n```javascript\nimport obfuscatorPlugin from \"vite-plugin-javascript-obfuscator\";\n\nexport default defineConfig({\n  plugins: [\n    obfuscatorPlugin({\n      options: {\n        // your javascript-obfuscator options\n        debugProtection: true,\n        // ...  [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator)\n      },\n    }),\n  ],\n});\n```\n\n### Example 2\n\nvite.config.js\n\n```javascript\nimport obfuscatorPlugin from \"vite-plugin-javascript-obfuscator\";\n\nexport default defineConfig({\n  plugins: [\n    obfuscatorPlugin({\n      include: [\"src/path/to/file.js\", \"path/anyjs/**/*.js\", /foo.js$/],\n      exclude: [/node_modules/],\n      apply: \"build\",\n      debugger: true,\n      options: {\n        // your javascript-obfuscator options\n        debugProtection: true,\n        // ...  [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator)\n      },\n    }),\n  ],\n});\n```\n\n### Params\n\n|    Name    | Type | Default | Description   |\n| :------------: | :----: | :-----: | :----------------------------------------------------------------------------------------------------: | \n| **`include`**  |  `Array\\|String\\|RegExp\\|Function` | `[/\\.(jsx?\\|tsx?\\|cjs\\|mjs)$/]` | Configure this option to include files |\n| **`exclude`**  |  `Array\\|String\\|RegExp\\|Function` | `[/node_modules/, /\\.nuxt/]`| Configure this option to exclude files |\n| **`options`**  |  `Object` | javascript-obfuscator default options | [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator) |\n| **`apply`**   | `'serve' \\| 'build'` | both serve and build. | By default plugins are invoked for both serve and build. In cases where a plugin needs to be conditionally applied only during serve or build, use the apply property to only invoke them during `vite build` or `vite serve`  |\n| **`debugger`** | `Boolean` | `false` | Used for debugging, Print out the path of matching or excluding files. |\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"vite-plugin-javascript-obfuscator\",\n  \"version\": \"3.1.0\",\n  \"description\": \"A Vite Plugin for javascript-obfuscator\",\n  \"main\": \"dist/index.cjs.js\",\n  \"module\": \"dist/index.es.js\",\n  \"types\": \"dist/index.d.ts\",\n  \"scripts\": {\n    \"build\": \"rollup -c rollup.config.mjs\",\n    \"prebuild\": \"rm -rf dist/*\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/elmeet/vite-plugin-javascript-obfuscator.git\"\n  },\n  \"keywords\": [\n    \"vite\",\n    \"obfuscator\",\n    \"obfuscation\",\n    \"uglify\",\n    \"crush\",\n    \"code protection\",\n    \"javascript obfuscator\",\n    \"js obfuscator\"\n  ],\n  \"author\": \"elmeet@163.com\",\n  \"license\": \"MIT\",\n  \"bugs\": {\n    \"url\": \"https://github.com/elmeet/vite-plugin-javascript-obfuscator/issues\"\n  },\n  \"homepage\": \"https://github.com/elmeet/vite-plugin-javascript-obfuscator#readme\",\n  \"dependencies\": {\n    \"anymatch\": \"~3.1.3\",\n    \"javascript-obfuscator\": \"^4.1.0\"\n  },\n  \"devDependencies\": {\n    \"@rollup/plugin-node-resolve\": \"~15.1.0\",\n    \"@rollup/plugin-typescript\": \"~11.1.1\",\n    \"@types/node\": \"~20.3.1\",\n    \"rollup\": \"~3.25.2\",\n    \"typescript\": \"~5.1.3\"\n  },\n  \"files\": [\n    \"dist\"\n  ]\n}\n"
  },
  {
    "path": "rollup.config.mjs",
    "content": "import resolve from \"@rollup/plugin-node-resolve\";\nimport typescript from \"@rollup/plugin-typescript\";\nimport pkg from \"./package.json\" assert { type: \"json\" };\nconst external = Object.keys(pkg.dependencies);\n\nexport default {\n  input: \"src/index.ts\",\n  output: [\n    {\n      file: pkg.main,\n      format: \"cjs\",\n    },\n    {\n      file: pkg.module,\n      format: \"es\",\n    },\n  ],\n  external: external,\n  sourcemap: true,\n  plugins: [resolve(), typescript()],\n};\n"
  },
  {
    "path": "src/index.ts",
    "content": "import { obfuscate, ObfuscatorOptions } from \"javascript-obfuscator\";\nimport anymatch, { Matcher } from \"anymatch\";\nimport { resolve } from \"path\";\n\nconst defaultIncludeMatcher = [/\\.(jsx?|tsx?|cjs|mjs)$/];\nconst defaultExcludeMatcher = [/node_modules/, /\\.nuxt/];\n\ntype Options = {\n  /**\n   * (Array|String|RegExp|Function) String to be directly matched, string with glob patterns, regular expression test, function that takes the testString as an argument and returns a truthy value if it should be matched. default: ```[/\\.(jsx?|tsx?|cjs|mjs)$/]```\n   * [See more](https://github.com/micromatch/anymatch)\n   */\n  include?: Matcher;\n  /**\n   *  (Array|String|RegExp|Function) String to be directly matched, string with glob patterns, regular expression test, function that takes the testString as an argument and returns a truthy value if it should be matched. default: ```[/node_modules/, /\\.nuxt/]```\n   * [See more](https://github.com/micromatch/anymatch)\n   */\n  exclude?: Matcher;\n  /**\n   * Your javascript-obfuscator options\n   * [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator)\n   */\n  options?: ObfuscatorOptions;\n  /**\n   * Used for debugging, Print out the path of matching or excluding files\n   */\n  debugger?: boolean;\n  /**\n   * By default plugins are invoked for both serve and build. In cases where a plugin needs to be conditionally applied only during serve or build\n   * https://vitejs.dev/guide/api-plugin.html\n   */\n  apply?: \"serve\" | \"build\" | ((this: void, config: any, env: any) => boolean);\n};\n\ntype UnArray<T> = T extends any[] ? never : T;\n\ntype AnymatchPattern = UnArray<Matcher>;\n\nfunction handleMatcher(matcher: Matcher): Matcher {\n  matcher = matcher instanceof Array ? matcher : [matcher];\n  return matcher.map((matcher: AnymatchPattern): AnymatchPattern => {\n    if (typeof matcher !== \"string\") {\n      return matcher;\n    }\n    return resolve(\".\", matcher).replace(/\\\\/g, \"/\");\n  });\n}\n\nexport default function obfuscatorPlugin(obOptions?: Options) {\n  let { include, exclude, options }: Options = obOptions || {};\n\n  const consoleLog = obOptions?.debugger ? console.log.bind(console) : () => {};\n\n  options = options || {};\n\n  const includeMatcher = include\n    ? handleMatcher(include)\n    : defaultIncludeMatcher;\n\n  const excludeMatcher = exclude\n    ? handleMatcher(exclude)\n    : defaultExcludeMatcher;\n\n  return {\n    name: \"vite-plugin-javascript-obfuscator\",\n    enforce: \"post\" as \"post\",\n    apply: obOptions?.apply || (() => true),\n    transform(src: string, id: string) {\n      if (anymatch(excludeMatcher, id, { dot: true })) {\n        consoleLog(\"[::plugin-javascript-obfuscator]::exclude\", id);\n        return;\n      }\n\n      if (anymatch(includeMatcher, id)) {\n        consoleLog(\"[::plugin-javascript-obfuscator]::include matched\", id);\n\n        const obfuscationResult = obfuscate(src, options);\n\n        const result = { code: obfuscationResult.getObfuscatedCode() } as {\n          map: string;\n          code: string;\n        };\n\n        if (options?.sourceMap && options?.sourceMapMode !== \"inline\") {\n          result.map = obfuscationResult.getSourceMap();\n        }\n        return result;\n      }\n\n      consoleLog(`[::plugin-javascript-obfuscator]::not matched`, id);\n    },\n  };\n}\n"
  },
  {
    "path": "tsconfig.json",
    "content": "{\n  \"include\": [\"src/**/*.ts\"],\n  \"compilerOptions\": {\n    \"strict\": true,\n    \"target\": \"es2016\",\n    \"module\": \"ES6\",\n    \"declaration\": true,\n    \"declarationDir\": \"./dist\",\n    \"skipLibCheck\": true,\n    \"esModuleInterop\": true,\n    \"forceConsistentCasingInFileNames\": true,\n    \"moduleResolution\": \"node\"\n  }\n}\n"
  }
]