Skip to content

Commit a7e78f1

Browse files
committed
first commit
0 parents  commit a7e78f1

File tree

9 files changed

+336
-0
lines changed

9 files changed

+336
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist
3+
pnpm-lock.yaml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 elmeet
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# rollup-plugin-javascript-confuser
2+
3+
A Rollup Plugin for [js-confuser](https://npmjs.com/package/js-confuser). Forked from [vite-plugin-javascript-obfuscator](https://github.com/elmeet/vite-plugin-javascript-obfuscator).
4+
5+
## Installation
6+
7+
Install the package:
8+
9+
- npm `npm install --save-dev rollup-plugin-javascript-confuser`
10+
- yarn `yarn add --dev rollup-plugin-javascript-confuser`
11+
- pnpm `pnpm i rollup-plugin-javascript-confuser -D`
12+
13+
## Usage
14+
15+
### Example 1
16+
17+
rollup.config.mjs
18+
19+
```javascript
20+
import obfuscatorPlugin from "rollup-plugin-javascript-confuser";
21+
22+
export default {
23+
input: 'src/index.js',
24+
output: {
25+
dir: 'output',
26+
format: 'cjs'
27+
},
28+
plugins: [
29+
obfuscatorPlugin({
30+
options: {
31+
// your js-confuser options
32+
debugProtection: true,
33+
// ... [See more options](https://js-confuser.com/docs/options)
34+
},
35+
})
36+
]
37+
};
38+
```
39+
40+
### Example 2
41+
42+
vite.config.js
43+
44+
```javascript
45+
import obfuscatorPlugin from "rollup-plugin-javascript-confuser";
46+
47+
48+
export default {
49+
input: {
50+
a: 'foo.js',
51+
b: 'bar.js'
52+
}
53+
output: {
54+
dir: 'output',
55+
format: 'cjs'
56+
},
57+
plugins: [
58+
obfuscatorPlugin({
59+
include: ["src/path/to/file.js", "path/anyjs/**/*.js", /foo.js$/],
60+
exclude: [/node_modules/],
61+
debugger: true,
62+
options: {
63+
// your js-confuser options
64+
debugProtection: true,
65+
// ... [See more options](https://js-confuser.com/docs/options)
66+
},
67+
})
68+
]
69+
};
70+
```
71+
72+
### Params
73+
74+
| Name | Type | Default | Description |
75+
| :------------: | :-------------------------------: | :-----------------------------------: | :--------------------------------------------------------------------: |
76+
| **`include`** | `Array\|String\|RegExp\|Function` | `[/\.(jsx?\|tsx?\|cjs\|mjs)$/]` | Configure this option to include files |
77+
| **`exclude`** | `Array\|String\|RegExp\|Function` | `[/node_modules/, /\.nuxt/]` | Configure this option to exclude files |
78+
| **`options`** | `Object` | javascript-obfuscator default options | [See more options](https://js-confuser.com/docs/options) |
79+
| **`debugger`** | `Boolean` | `false` | Used for debugging, Print out the path of matching or excluding files. |

package.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "rollup-plugin-javascript-confuser",
3+
"version": "1.0.0",
4+
"description": "A Rollup Plugin for js-confuser",
5+
"main": "dist/index.cjs.js",
6+
"module": "dist/index.es.js",
7+
"types": "dist/src",
8+
"scripts": {
9+
"prepublish": "rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript --configImportAttributesKey with",
10+
"prebuild": "rm -rf dist/*"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/EXPress-016/rollup-plugin-javascript-confuser.git"
15+
},
16+
"keywords": [
17+
"rollup",
18+
"vite",
19+
"obfuscator",
20+
"obfuscation",
21+
"uglify",
22+
"crush",
23+
"code protection",
24+
"javascript obfuscator",
25+
"js obfuscator",
26+
"js confuser",
27+
"javascript confuser"
28+
],
29+
"author": "EXPress-016",
30+
"license": "MIT",
31+
"bugs": {
32+
"url": "https://github.com/EXPress-016/rollup-plugin-javascript-confuser/issues"
33+
},
34+
"homepage": "https://github.com/EXPress-016/rollup-plugin-javascript-confuser#readme",
35+
"dependencies": {
36+
"anymatch": "^3.1.3"
37+
},
38+
"peerDependencies": {
39+
"js-confuser": "^2.0.0"
40+
},
41+
"devDependencies": {
42+
"@rollup/plugin-node-resolve": "^16.0.1",
43+
"@rollup/plugin-typescript": "^12.1.4",
44+
"@biomejs/biome": "^2.0.6",
45+
"@types/node": "^22.16.0",
46+
"rollup": "^4.44.2",
47+
"tslib": "^2.8.1",
48+
"typescript": "^5.8.3"
49+
},
50+
"files": ["dist"],
51+
"packageManager": "pnpm@10.11.0"
52+
}

rollup.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { RollupOptions } from "rollup";
2+
import nodeResolve from "@rollup/plugin-node-resolve";
3+
import typescript from "@rollup/plugin-typescript";
4+
import pkg from "./package.json" with { type: "json" };
5+
const external = Object.keys({ ...pkg.dependencies, ...pkg.peerDependencies });
6+
7+
const options: RollupOptions = {
8+
input: "src/index.ts",
9+
10+
output: [
11+
{
12+
file: pkg.main,
13+
format: "cjs",
14+
sourcemap: true,
15+
},
16+
{
17+
file: pkg.module,
18+
format: "es",
19+
sourcemap: true,
20+
},
21+
],
22+
external: external,
23+
24+
plugins: [nodeResolve(), typescript()],
25+
};
26+
27+
export default options;

src/index.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import type { Plugin } from "rollup";
2+
import Obfuscator, { type ObfuscateOptions } from "js-confuser";
3+
import anymatch, { type Matcher } from "anymatch";
4+
import {
5+
defaultExcludeMatcher,
6+
defaultIncludeMatcher,
7+
handleMatcher,
8+
} from "./matcher";
9+
10+
export type { ObfuscateOptions };
11+
12+
export type Config = {
13+
/**
14+
* (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)$/]```
15+
* [See more](https://github.com/micromatch/anymatch)
16+
*/
17+
include?: Matcher;
18+
/**
19+
* (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/]```
20+
* [See more](https://github.com/micromatch/anymatch)
21+
*/
22+
exclude?: Matcher;
23+
/**
24+
* Your js-confuser options
25+
* [See more options](https://js-confuser.com/docs/options)
26+
*/
27+
options?: ObfuscateOptions;
28+
/**
29+
* Used for debugging, Print out the path of matching or excluding files
30+
*/
31+
debugger?: boolean;
32+
};
33+
34+
const { obfuscate } = Obfuscator;
35+
36+
export default function obfuscatorPlugin(obOptions?: Config) {
37+
let { include, exclude, options }: Config = obOptions || {};
38+
39+
const consoleLog = obOptions?.debugger ? console.log.bind(console) : () => {};
40+
41+
options = options || {
42+
target: "browser",
43+
preset: "low",
44+
};
45+
46+
const includeMatcher = include
47+
? handleMatcher(include)
48+
: defaultIncludeMatcher;
49+
50+
const excludeMatcher = exclude
51+
? handleMatcher(exclude)
52+
: defaultExcludeMatcher;
53+
54+
return {
55+
name: "rollup-plugin-javascript-confuser",
56+
enforce: "post",
57+
/*
58+
async transform(src: string, id: string) {
59+
if (anymatch(excludeMatcher, id, { dot: true })) {
60+
consoleLog("[::plugin-javascript-confuser]::exclude", id);
61+
return;
62+
}
63+
64+
if (anymatch(includeMatcher, id)) {
65+
consoleLog("[::plugin-javascript-confuser]::include matched", id);
66+
67+
const obfuscationResult = await obfuscate(src, options);
68+
69+
const result = { code: obfuscationResult.code } as {
70+
code: string;
71+
};
72+
73+
return result;
74+
}
75+
76+
consoleLog("[::plugin-javascript-confuser]::not matched", id);
77+
},
78+
*/
79+
async generateBundle(bundleOptions, bundleObj) {
80+
for (const bundleKey of Object.keys(bundleObj)) {
81+
const bundle = bundleObj[bundleKey];
82+
if (bundle.type !== "chunk") {
83+
consoleLog(
84+
"[::plugin-javascript-confuser]::not chunk",
85+
bundle.fileName,
86+
);
87+
continue;
88+
}
89+
90+
const id = bundle.facadeModuleId || bundle.preliminaryFileName;
91+
92+
if (anymatch(excludeMatcher, id, { dot: true })) {
93+
consoleLog("[::plugin-javascript-confuser]::exclude", id);
94+
continue;
95+
}
96+
97+
if (anymatch(includeMatcher, id)) {
98+
consoleLog("[::plugin-javascript-confuser]::include matched", id);
99+
100+
const obfuscationResult = await obfuscate(bundle.code, options);
101+
102+
const result = { code: obfuscationResult.code } as {
103+
code: string;
104+
};
105+
106+
bundle.code = result.code;
107+
108+
continue;
109+
}
110+
111+
consoleLog("[::plugin-javascript-confuser]::not matched", id);
112+
}
113+
},
114+
} as Plugin;
115+
}

src/matcher.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Matcher } from "anymatch";
2+
import anymatch from "anymatch";
3+
import { resolve } from "node:path";
4+
5+
export const defaultIncludeMatcher = [/\.(jsx?|tsx?|cjs|mjs)$/];
6+
export const defaultExcludeMatcher = [/node_modules/, /\.nuxt/];
7+
8+
export function handleMatcher(matcher: Matcher) {
9+
const matcherA = Array.isArray(matcher) ? matcher : [matcher];
10+
return matcherA.map((matcher) => {
11+
if (typeof matcher !== 'string') return matcher
12+
13+
return resolve('.', matcher).replace(/\\/g, "/")
14+
})
15+
16+
// return matcherA.map((matcher: Matcher): Matcher => {
17+
// if (typeof matcher !== "string") {
18+
// return matcher;
19+
// }
20+
// return resolve(".", matcher).replace(/\\/g, "/");
21+
// });
22+
23+
}
24+

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"include": ["src/**/*.ts", "rollup.config.ts"],
3+
"compilerOptions": {
4+
"strict": false,
5+
"target": "ESNext",
6+
"module": "ESNext",
7+
"declaration": true,
8+
"declarationDir": "./dist",
9+
"skipLibCheck": true,
10+
"esModuleInterop": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"moduleResolution": "node",
13+
"resolveJsonModule": true
14+
}
15+
}

0 commit comments

Comments
 (0)