|
| 1 | +import * as esbuild from "esbuild"; |
| 2 | +import { copyFileSync, mkdirSync } from "fs"; |
| 3 | +import { dirname, join } from "path"; |
| 4 | +import { fileURLToPath } from "url"; |
| 5 | + |
| 6 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 7 | +const isWatch = process.argv.includes("--watch"); |
| 8 | +const distDir = join(__dirname, "dist"); |
| 9 | + |
| 10 | +// Ensure dist/ directory exists |
| 11 | +mkdirSync(distDir, { recursive: true }); |
| 12 | + |
| 13 | +// Copy static files |
| 14 | +console.log("Copying static files..."); |
| 15 | +copyFileSync(join(__dirname, "src/index.html"), join(distDir, "index.html")); |
| 16 | +copyFileSync( |
| 17 | + join(__dirname, "../pkg/ggsql_wasm_bg.wasm"), |
| 18 | + join(distDir, "ggsql_wasm_bg.wasm"), |
| 19 | +); |
| 20 | +copyFileSync( |
| 21 | + join(__dirname, "node_modules/vscode-oniguruma/release/onig.wasm"), |
| 22 | + join(distDir, "onig.wasm"), |
| 23 | +); |
| 24 | +copyFileSync( |
| 25 | + join(__dirname, "../../ggsql-vscode/syntaxes/ggsql.tmLanguage.json"), |
| 26 | + join(distDir, "ggsql.tmLanguage.json"), |
| 27 | +); |
| 28 | + |
| 29 | +// Build Monaco editor web worker |
| 30 | +console.log("Building Monaco editor worker..."); |
| 31 | +await esbuild.build({ |
| 32 | + entryPoints: [ |
| 33 | + join( |
| 34 | + __dirname, |
| 35 | + "node_modules/monaco-editor/esm/vs/editor/editor.worker.js", |
| 36 | + ), |
| 37 | + ], |
| 38 | + bundle: true, |
| 39 | + outfile: join(distDir, "editor.worker.js"), |
| 40 | + format: "iife", |
| 41 | +}); |
| 42 | + |
| 43 | +// Shared build options |
| 44 | +const sharedOptions = { |
| 45 | + bundle: true, |
| 46 | + format: "esm", |
| 47 | + platform: "browser", |
| 48 | + target: "es2020", |
| 49 | + sourcemap: true, |
| 50 | + nodePaths: [join(__dirname, "node_modules")], |
| 51 | + loader: { |
| 52 | + ".ttf": "file", |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +// Build playground bundle |
| 57 | +const playgroundOptions = { |
| 58 | + ...sharedOptions, |
| 59 | + entryPoints: [join(__dirname, "src/main.ts")], |
| 60 | + outfile: join(distDir, "bundle.js"), |
| 61 | +}; |
| 62 | + |
| 63 | +// Build quarto integration bundle |
| 64 | +const quartoOptions = { |
| 65 | + ...sharedOptions, |
| 66 | + entryPoints: [join(__dirname, "src/quarto/main.ts")], |
| 67 | + outfile: join(distDir, "quarto.js"), |
| 68 | + loader: { |
| 69 | + ...sharedOptions.loader, |
| 70 | + ".css": "css", |
| 71 | + }, |
| 72 | +}; |
| 73 | + |
| 74 | +if (isWatch) { |
| 75 | + console.log("Starting watch mode..."); |
| 76 | + const playgroundCtx = await esbuild.context(playgroundOptions); |
| 77 | + const quartoCtx = await esbuild.context(quartoOptions); |
| 78 | + await Promise.all([playgroundCtx.watch(), quartoCtx.watch()]); |
| 79 | + console.log("Watching for changes..."); |
| 80 | +} else { |
| 81 | + console.log("Building bundles..."); |
| 82 | + await Promise.all([ |
| 83 | + esbuild.build(playgroundOptions), |
| 84 | + esbuild.build(quartoOptions), |
| 85 | + ]); |
| 86 | + console.log("Build complete!"); |
| 87 | +} |
0 commit comments