-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
63 lines (52 loc) · 1.87 KB
/
build.mjs
File metadata and controls
63 lines (52 loc) · 1.87 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
import * as esbuild from 'esbuild';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import JavaScriptObfuscator from 'javascript-obfuscator';
if (!existsSync('dist')) {
mkdirSync('dist');
}
const isWatch = process.argv.includes('--watch');
const skipObfuscate = process.argv.includes('--no-obfuscate');
const buildOptions = {
entryPoints: ['js/app.js', 'js/admin.js'],
bundle: true,
outdir: 'dist',
entryNames: '[name].bundle',
format: 'iife',
minify: !isWatch,
sourcemap: isWatch,
target: ['es2020'],
logLevel: 'info'
};
if (isWatch) {
const ctx = await esbuild.context(buildOptions);
await ctx.watch();
console.log('Watching for changes...');
} else {
await esbuild.build(buildOptions);
console.log('✓ Bundle created');
if (!skipObfuscate) {
console.log('⏳ Obfuscating...');
const files = ['dist/app.bundle.js', 'dist/admin.bundle.js'];
for (const file of files) {
if (!existsSync(file)) continue;
const code = readFileSync(file, 'utf8');
const obfuscated = JavaScriptObfuscator.obfuscate(code, {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.5,
deadCodeInjection: false,
debugProtection: false,
identifierNamesGenerator: 'hexadecimal',
renameGlobals: false,
stringArray: true,
stringArrayThreshold: 0.5,
transformObjectKeys: true,
unicodeEscapeSequence: false
});
writeFileSync(file, obfuscated.getObfuscatedCode());
}
console.log('✓ Build complete: dist/*.bundle.js (minified + obfuscated)');
} else {
console.log('✓ Build complete: dist/*.bundle.js (minified only)');
}
}