import esbuild from 'esbuild';
import { config } from 'dotenv';
import { readFileSync, writeFileSync, mkdirSync, copyFileSync, existsSync } from 'fs';
// ─── Load Environment ────────────────────────────────────────────
const mode = process.env.NODE_ENV || 'development';
const envFile = `.env.${mode}`;
if (existsSync(envFile)) {
config({ path: envFile });
console.log(`Loaded environment from ${envFile}`);
} else {
config(); // fallback to .env
console.warn(`No ${envFile} found, using fallback .env if exists`);
}
// Removed hardcoded proxy URL - now handled dynamically in the plugin
// ─── Prepare Output Directory ────────────────────────────────────
const outDir = 'dist';
mkdirSync(outDir, { recursive: true });
// ─── Build UI (bundle ui.ts + inject into HTML template) ─────────
const html = readFileSync('src/ui.html', 'utf8');
const css = readFileSync('src/styles.css', 'utf8');
const uiBuild = await esbuild.build({
entryPoints: ['src/ui.ts'],
bundle: true,
minify: true,
write: false,
format: 'iife',
target: 'es6',
platform: 'browser',
});
const finalHtml = html
.replace('', ``)
.replace('', ``);
writeFileSync(`${outDir}/ui.html`, finalHtml);
// ─── Build Plugin Backend (code.ts) ──────────────────────────────
await esbuild.build({
entryPoints: ['src/code.ts'],
outfile: `${outDir}/code.js`,
bundle: true,
minify: true,
platform: 'browser',
target: 'es6',
});
// ─── Copy Manifest ───────────────────────────────────────────────
copyFileSync('src/manifest.json', `${outDir}/manifest.json`);
// ─── Done ────────────────────────────────────────────────────────
console.log(`Built for ${mode} - proxy URL now configured in plugin UI`);