-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
82 lines (73 loc) · 2.42 KB
/
vite.config.ts
File metadata and controls
82 lines (73 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { defineConfig, type Plugin } from "vite";
import { builtinModules } from "module";
import { readFileSync, writeFileSync, readdirSync } from "fs";
import { join } from "path";
import JavaScriptObfuscator from "javascript-obfuscator";
const nodeExternal = builtinModules.flatMap((m) => [m, `node:${m}`]);
const npmExternal = ["commander", "dotenv", "node-fetch", "nodemailer", "ora"];
function obfuscatorPlugin(): Plugin {
return {
name: "obfuscator",
closeBundle() {
const distDir = join(__dirname, "dist");
const getJsFiles = (dir: string): string[] =>
readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
const p = join(dir, e.name);
return e.isDirectory()
? getJsFiles(p)
: e.name.endsWith(".js")
? [p]
: [];
});
for (const file of getJsFiles(distDir)) {
const src = readFileSync(file, "utf8");
// Strip shebang before obfuscating so the obfuscator doesn't choke on it
const shebangMatch = src.match(/^(#!.+\n)/);
const shebang = shebangMatch ? shebangMatch[1] : "";
const code = shebang ? src.slice(shebang.length) : src;
const obfuscated = JavaScriptObfuscator.obfuscate(code, {
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
disableConsoleOutput: false,
identifierNamesGenerator: "hexadecimal",
renameGlobals: false,
rotateStringArray: true,
selfDefending: false,
shuffleStringArray: true,
splitStrings: false,
stringArray: true,
stringArrayEncoding: ["base64"],
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false,
}).getObfuscatedCode();
writeFileSync(file, shebang + obfuscated, "utf8");
console.log(
` obfuscated: ${file.replace(distDir + "\\", "").replace(distDir + "/", "")}`,
);
}
},
};
}
export default defineConfig({
build: {
target: "node18",
lib: {
entry: "src/cli.ts",
formats: ["cjs"],
fileName: () => "cli.js",
},
rollupOptions: {
external: [...nodeExternal, ...npmExternal],
output: {
inlineDynamicImports: true,
},
},
outDir: "dist",
minify: false,
sourcemap: false,
emptyOutDir: true,
},
plugins: [obfuscatorPlugin()],
});