import * as esbuild from "esbuild"; import * as fs from "node:fs"; import * as path from "node:path"; import ObfuscatorPlugin from "esbuild-obfuscator-plugin"; const watch = process.argv.includes("--watch"); function removeExtension(name) { const fileNames = name.split("."); fileNames.pop(); return fileNames.join("."); } const entriesPath = path.join(process.cwd(), "src", "client", "entries"); const tsFile = fs .readdirSync(entriesPath, { withFileTypes: true, }) .filter((e) => e.isFile() && e.name.endsWith(".ts")); const configs = tsFile.map((e) => { const config = { entryPoints: [path.join(entriesPath, e.name)], bundle: true, sourcemap: false, minify: true, platform: "browser", outfile: path.join( process.cwd(), "public", "assets", "js", `${removeExtension(e.name)}.js`, ), }; if (e.name === "detailed-analytics.ts") { config.plugins = [ ObfuscatorPlugin({ "optionsPreset": "high-obfuscation", "compact": true, "simplify": true, "identifierNamesGenerator": "hexadecimal", "renameGlobals": false, "stringArray": true, "stringArrayCallsTransform": true, "stringArrayEncoding": ["base64", "rc4"], "stringArrayThreshold": 1, "stringArrayRotate": true, "stringArrayShuffle": true, "stringArrayIndexShift": true, "stringArrayWrappersCount": 5, "stringArrayWrappersChainedCalls": true, "stringArrayWrappersParametersMaxCount": 5, "stringArrayWrappersType": "function", "controlFlowFlattening": true, "controlFlowFlatteningThreshold": 1, "deadCodeInjection": true, "deadCodeInjectionThreshold": 0.4, "numbersToExpressions": true, "transformObjectKeys": true, "unicodeEscapeSequence": false, "selfDefending": true, "debugProtection": true, "debugProtectionInterval": 4000, "disableConsoleOutput": true, "log": false, "sourceMap": false, }), ]; } return config; }); if (watch) { const contexts = await Promise.all( configs.map((config) => esbuild.context(config)), ); await Promise.all(contexts.map((context) => context.watch())); console.log("[esbuild] watching client bundles for changes..."); } else { await Promise.all(configs.map((config) => esbuild.build(config))); console.log("[esbuild] client bundles built."); }