Repository: elmeet/vite-plugin-javascript-obfuscator Branch: main Commit: b9bce9a4a32b Files: 8 Total size: 8.4 KB Directory structure: gitextract_xy2tdofo/ ├── .gitignore ├── .prettierignore ├── LICENSE ├── README.md ├── package.json ├── rollup.config.mjs ├── src/ │ └── index.ts └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules dist ================================================ FILE: .prettierignore ================================================ README.md ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2022 elmeet Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # vite-plugin-javascript-obfuscator A Vite Plugin for [javascript-obfuscator](https://www.npmjs.com/package/javascript-obfuscator) ## Installation Install the package: - npm `npm install --save-dev vite-plugin-javascript-obfuscator` - yarn `yarn add --dev vite-plugin-javascript-obfuscator` - pnpm `pnpm i vite-plugin-javascript-obfuscator -D` ## Usage ### Example 1 vite.config.js ```javascript import obfuscatorPlugin from "vite-plugin-javascript-obfuscator"; export default defineConfig({ plugins: [ obfuscatorPlugin({ options: { // your javascript-obfuscator options debugProtection: true, // ... [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator) }, }), ], }); ``` ### Example 2 vite.config.js ```javascript import obfuscatorPlugin from "vite-plugin-javascript-obfuscator"; export default defineConfig({ plugins: [ obfuscatorPlugin({ include: ["src/path/to/file.js", "path/anyjs/**/*.js", /foo.js$/], exclude: [/node_modules/], apply: "build", debugger: true, options: { // your javascript-obfuscator options debugProtection: true, // ... [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator) }, }), ], }); ``` ### Params | Name | Type | Default | Description | | :------------: | :----: | :-----: | :----------------------------------------------------------------------------------------------------: | | **`include`** | `Array\|String\|RegExp\|Function` | `[/\.(jsx?\|tsx?\|cjs\|mjs)$/]` | Configure this option to include files | | **`exclude`** | `Array\|String\|RegExp\|Function` | `[/node_modules/, /\.nuxt/]`| Configure this option to exclude files | | **`options`** | `Object` | javascript-obfuscator default options | [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator) | | **`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` | | **`debugger`** | `Boolean` | `false` | Used for debugging, Print out the path of matching or excluding files. | ================================================ FILE: package.json ================================================ { "name": "vite-plugin-javascript-obfuscator", "version": "3.1.0", "description": "A Vite Plugin for javascript-obfuscator", "main": "dist/index.cjs.js", "module": "dist/index.es.js", "types": "dist/index.d.ts", "scripts": { "build": "rollup -c rollup.config.mjs", "prebuild": "rm -rf dist/*" }, "repository": { "type": "git", "url": "git+https://github.com/elmeet/vite-plugin-javascript-obfuscator.git" }, "keywords": [ "vite", "obfuscator", "obfuscation", "uglify", "crush", "code protection", "javascript obfuscator", "js obfuscator" ], "author": "elmeet@163.com", "license": "MIT", "bugs": { "url": "https://github.com/elmeet/vite-plugin-javascript-obfuscator/issues" }, "homepage": "https://github.com/elmeet/vite-plugin-javascript-obfuscator#readme", "dependencies": { "anymatch": "~3.1.3", "javascript-obfuscator": "^4.1.0" }, "devDependencies": { "@rollup/plugin-node-resolve": "~15.1.0", "@rollup/plugin-typescript": "~11.1.1", "@types/node": "~20.3.1", "rollup": "~3.25.2", "typescript": "~5.1.3" }, "files": [ "dist" ] } ================================================ FILE: rollup.config.mjs ================================================ import resolve from "@rollup/plugin-node-resolve"; import typescript from "@rollup/plugin-typescript"; import pkg from "./package.json" assert { type: "json" }; const external = Object.keys(pkg.dependencies); export default { input: "src/index.ts", output: [ { file: pkg.main, format: "cjs", }, { file: pkg.module, format: "es", }, ], external: external, sourcemap: true, plugins: [resolve(), typescript()], }; ================================================ FILE: src/index.ts ================================================ import { obfuscate, ObfuscatorOptions } from "javascript-obfuscator"; import anymatch, { Matcher } from "anymatch"; import { resolve } from "path"; const defaultIncludeMatcher = [/\.(jsx?|tsx?|cjs|mjs)$/]; const defaultExcludeMatcher = [/node_modules/, /\.nuxt/]; type Options = { /** * (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)$/]``` * [See more](https://github.com/micromatch/anymatch) */ include?: Matcher; /** * (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/]``` * [See more](https://github.com/micromatch/anymatch) */ exclude?: Matcher; /** * Your javascript-obfuscator options * [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator) */ options?: ObfuscatorOptions; /** * Used for debugging, Print out the path of matching or excluding files */ debugger?: boolean; /** * 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 * https://vitejs.dev/guide/api-plugin.html */ apply?: "serve" | "build" | ((this: void, config: any, env: any) => boolean); }; type UnArray = T extends any[] ? never : T; type AnymatchPattern = UnArray; function handleMatcher(matcher: Matcher): Matcher { matcher = matcher instanceof Array ? matcher : [matcher]; return matcher.map((matcher: AnymatchPattern): AnymatchPattern => { if (typeof matcher !== "string") { return matcher; } return resolve(".", matcher).replace(/\\/g, "/"); }); } export default function obfuscatorPlugin(obOptions?: Options) { let { include, exclude, options }: Options = obOptions || {}; const consoleLog = obOptions?.debugger ? console.log.bind(console) : () => {}; options = options || {}; const includeMatcher = include ? handleMatcher(include) : defaultIncludeMatcher; const excludeMatcher = exclude ? handleMatcher(exclude) : defaultExcludeMatcher; return { name: "vite-plugin-javascript-obfuscator", enforce: "post" as "post", apply: obOptions?.apply || (() => true), transform(src: string, id: string) { if (anymatch(excludeMatcher, id, { dot: true })) { consoleLog("[::plugin-javascript-obfuscator]::exclude", id); return; } if (anymatch(includeMatcher, id)) { consoleLog("[::plugin-javascript-obfuscator]::include matched", id); const obfuscationResult = obfuscate(src, options); const result = { code: obfuscationResult.getObfuscatedCode() } as { map: string; code: string; }; if (options?.sourceMap && options?.sourceMapMode !== "inline") { result.map = obfuscationResult.getSourceMap(); } return result; } consoleLog(`[::plugin-javascript-obfuscator]::not matched`, id); }, }; } ================================================ FILE: tsconfig.json ================================================ { "include": ["src/**/*.ts"], "compilerOptions": { "strict": true, "target": "es2016", "module": "ES6", "declaration": true, "declarationDir": "./dist", "skipLibCheck": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "moduleResolution": "node" } }