-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathesbuild-plugin-obfuscator.js
More file actions
106 lines (86 loc) · 3.22 KB
/
Copy pathesbuild-plugin-obfuscator.js
File metadata and controls
106 lines (86 loc) · 3.22 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import JavaScriptObfuscator from 'javascript-obfuscator';
import micromatch from 'micromatch';
import { promises as fs } from 'fs';
import { getBuildExtensions } from 'esbuild-extra';
export function ObfuscatorPlugin({
filter = [],
shouldObfuscateOutput = false,
shouldWriteOutputSourceMap = false,
shouldGenerateSourceMap = false,
ignoreRequireImports = true,
...options
} = {}) {
return {
name: 'obfuscator',
async setup(build) {
const { onTransform } = getBuildExtensions(build, 'obfuscator');
options = {
ignoreRequireImports,
...options,
};
if (shouldObfuscateOutput) {
build.initialOptions.write = false;
build.onEnd(async (result) => {
let tasks = [];
for (const output of result.outputFiles) {
const filePath = output.path;
const originalCode = output.text;
// Obfuscate the code using javascript-obfuscator with conditional sourcemap generation
const obfuscatorOptions = {
...options,
sourceMap: shouldWriteOutputSourceMap
};
if (shouldWriteOutputSourceMap) {
obfuscatorOptions.sourceMapFileName = filePath + '.map';
}
const obfuscationResult = JavaScriptObfuscator.obfuscate(originalCode, obfuscatorOptions);
const obfuscatedCode = obfuscationResult.getObfuscatedCode();
// Write the obfuscated code to the file
tasks.push(fs.writeFile(filePath, obfuscatedCode));
if (shouldWriteOutputSourceMap) {
const sourceMap = obfuscationResult.getSourceMap();
// Write the sourcemap file
tasks.push(fs.writeFile(filePath + '.map', sourceMap));
}
}
await Promise.all(tasks);
});
} else {
onTransform({
loaders: ['js'],
namespace: 'file'
}, async (args) => {
// Use micromatch to check if the input file matches any patterns in the filter array
const shouldObfuscate = micromatch.isMatch(args.path, filter);
// If the input file does not match the filter, skip obfuscation
if (!shouldObfuscate) {
return { code: args.code };
}
// Obfuscate the JavaScript code with conditional sourcemap generation
const obfuscatorOptions = {
...options,
sourceMap: shouldGenerateSourceMap
};
if (shouldGenerateSourceMap) {
obfuscatorOptions.sourceMapFileName = args.path + '.map';
}
const obfuscationResult = JavaScriptObfuscator.obfuscate(args.code, obfuscatorOptions);
const obfuscatedCode = obfuscationResult.getObfuscatedCode();
if (shouldGenerateSourceMap) {
let sourceMap = obfuscationResult.getSourceMap();
sourceMap = JSON.parse(sourceMap);
sourceMap.sources = [];
return {
code: obfuscatedCode,
map: sourceMap
};
} else {
return {
code: obfuscatedCode
};
}
});
}
},
};
}