llm-agent-plugin/esbuild.config.mjs
2025-11-25 23:07:38 +03:00

110 lines
3.3 KiB
JavaScript

import esbuild from 'esbuild'
import process from 'process'
import builtins from 'builtin-modules'
import fs from 'fs'
import path from 'path'
import { ObfuscatorPlugin } from 'esbuild-plugin-obfuscator' // Импорт плагина
const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`
const arg = process.argv[2] || ''
const prod = arg === 'production'
const obfuscate = arg === 'dev-obfuscate'
const outputFolder = '../llm-agent-plugin-build';
console.log(arg)
console.log(obfuscate)
const renameCssPlugin = () => ({
name: 'rename-css',
setup (build) {
build.onEnd(() => {
const outDir = path.dirname(build.initialOptions.outfile || 'main.js')
const mainCss = path.join(outDir, 'main.css')
const stylesCss = path.join(outDir, 'styles.css')
if (fs.existsSync(mainCss)) {
fs.renameSync(mainCss, stylesCss)
console.log(`Renamed ${mainCss} to ${stylesCss}`)
}
})
}
})
const copyManifestPlugin = () => ({
name: 'copy-manifest',
setup(build) {
build.onEnd(() => {
const outDir = path.dirname(build.initialOptions.outfile || 'main.js'); // Целевая директория сборки
const manifestSourcePath = path.join(process.cwd(), 'src', 'json', 'manifest.json'); // Исходный путь к manifest.json
const manifestDestPath = path.join(outDir, 'manifest.json'); // Целевой путь в папке сборки
if (fs.existsSync(manifestSourcePath)) {
fs.copyFileSync(manifestSourcePath, manifestDestPath);
console.log(`Copied manifest.json from ${manifestSourcePath} to ${manifestDestPath}`);
} else {
console.warn(`Warning: manifest.json not found at ${manifestSourcePath}, skipping copy.`);
}
});
},
});
const context = await esbuild.context({
banner: {
js: banner
},
entryPoints: ['main.ts'],
bundle: true,
external: [
'obsidian',
'electron',
'@codemirror/autocomplete',
'@codemirror/collab',
'@codemirror/commands',
'@codemirror/language',
'@codemirror/lint',
'@codemirror/search',
'@codemirror/state',
'@codemirror/view',
'@lezer/common',
'@lezer/highlight',
'@lezer/lr',
...builtins
],
format: 'cjs',
target: 'es2019',
logLevel: 'info',
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: path.join(outputFolder, 'main.js'),
minify: prod,
loader: {
'.css': 'css'
},
plugins: [
renameCssPlugin(),
copyManifestPlugin(),
...(prod || obfuscate ? [ObfuscatorPlugin({
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
debugProtection: true,
debugProtectionInterval: true,
disableConsoleOutput: true,
// Можно добавить filter, чтобы обфусцировать только нужные файлы, например ['**/*.js']
})] : [])
]
})
if (prod) {
await context.rebuild()
process.exit(0)
} else {
await context.watch()
}