-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
75 lines (64 loc) · 2.82 KB
/
build.js
File metadata and controls
75 lines (64 loc) · 2.82 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
const { execSync } = require('child_process');
const JavaScriptObfuscator = require('javascript-obfuscator');
const fs = require('fs');
console.log('Compilando Previewhtml por separado (sin ofuscar)...');
// Paso 1: compilar Previewhtml.ts → JS legible aparte
execSync('npx tsc src/webview/Previewhtml.ts --outDir out_preview --module commonjs --target es2020 --esModuleInterop --skipLibCheck');
const previewCode = fs.readFileSync('out_preview/webview/Previewhtml.js', 'utf8');
console.log('Iniciando empaquetado con esbuild (sin Previewhtml)...');
// Paso 2: bundle todo el src EXCEPTO Previewhtml
// Previewhtml ya está compilado; lo reemplazamos con un stub vacío temporal
const previewSrcPath = 'src/webview/Previewhtml.ts';
const previewBackup = fs.readFileSync(previewSrcPath, 'utf8');
// Stub temporal: exporta las mismas funciones pero vacías (para que esbuild no falle)
// AJUSTA los nombres de exports según tu archivo real
const stub = `
export function getPreviewHtml(...args: any[]): string { return '__PREVIEW_PLACEHOLDER__'; }
export function getWebviewContent(...args: any[]): string { return '__PREVIEW_PLACEHOLDER__'; }
`;
fs.writeFileSync(previewSrcPath, stub);
try {
execSync('npx esbuild ./src/extension.ts --bundle --outfile=out/extension.js --external:vscode --format=cjs --minify --platform=node');
} finally {
// Restaurar Previewhtml.ts original siempre, aunque falle
fs.writeFileSync(previewSrcPath, previewBackup);
}
console.log('Esbuild completado. Iniciando ofuscación...');
// Paso 3: ofuscar el bundle (que tiene stubs de Previewhtml)
const code = fs.readFileSync('out/extension.js', 'utf8');
const result = JavaScriptObfuscator.obfuscate(code, {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.4,
deadCodeInjection: true,
deadCodeInjectionThreshold: 0.3,
debugProtection: false,
selfDefending: false,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
renameGlobals: false,
transformObjectKeys: false,
stringArray: true,
stringArrayEncoding: ['base64'],
stringArrayRotate: true,
rotateStringArray: true,
stringArrayShuffle: true,
shuffleStringArray: true,
stringArrayIndexShift: true,
stringArrayCallsTransform: true,
stringArrayCallsTransformThreshold: 0.75,
stringArrayWrappersCount: 5,
stringArrayWrappersType: 'function',
splitStrings: true,
splitStringsChunkLength: 4,
unicodeEscapeSequence: true,
});
// Paso 4: reinyectar el código real de Previewhtml (sin ofuscar)
let finalCode = result.getObfuscatedCode();
finalCode = finalCode.replace(
/"__PREVIEW_PLACEHOLDER__"/g,
// Envuelve el código real como IIFE que retorna el módulo
`(function(){ ${previewCode}; return module.exports; })()`
);
fs.writeFileSync('out/extension.js', finalCode);
console.log('¡Build completo! Previewhtml sin ofuscar, resto ofuscado.');