/** * Build script for WP Agentic Writer sidebar * Compiles index.jsx to dist/sidebar.js */ const esbuild = require("esbuild"); const path = require("path"); const fs = require("fs"); const srcPath = path.join(__dirname, "..", "assets", "js", "src", "index.jsx"); const distPath = path.join( __dirname, "..", "assets", "js", "dist", "sidebar.js", ); // Ensure dist directory exists const distDir = path.dirname(distPath); if (!fs.existsSync(distDir)) { fs.mkdirSync(distDir, { recursive: true }); } const isWatch = process.argv.includes("--watch"); const buildOptions = { entryPoints: [srcPath], bundle: true, outfile: distPath, // The source file is already a self-invoking IIFE: (function(wp){...})(window.wp). // Use anonymous IIFE wrapping WITHOUT a globalName so we never assign to (and // thereby clobber) the global `wp` that WordPress provides. format: "iife", target: ["es2019"], minify: !isWatch, sourcemap: isWatch, // JSX settings - WordPress uses createElement jsx: "transform", jsxFactory: "wp.element.createElement", jsxFragment: "wp.element.Fragment", // WordPress globals define: { "process.env.NODE_ENV": isWatch ? '"development"' : '"production"', }, // External dependencies (loaded by WordPress) external: [], logLevel: "info", }; async function build() { if (isWatch) { const ctx = await esbuild.context(buildOptions); await ctx.watch(); console.log("šŸ‘€ Watching for changes..."); // Handle cleanup on exit process.on("SIGINT", async () => { console.log("\nāœ– Stopping watcher..."); await ctx.dispose(); process.exit(0); }); } else { const result = await esbuild.build(buildOptions); console.log("āœ… Build complete!"); } } build().catch((err) => { console.error("āŒ Build failed:", err); process.exit(1); });