| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | import * as esbuild from "esbuild"; |
| 4 | import { chmodSync, writeFileSync, copyFileSync } from "fs"; |
| 5 | import { dirname, resolve } from "path"; |
| 6 | import { fileURLToPath } from "url"; |
| 7 | |
| 8 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | |
| 10 | // List of packages to mark as external (ONLY native modules that cannot be bundled) |
| 11 | // Note: Everything else will be bundled to create a self-contained CLI |
| 12 | // Users should not need to install any additional dependencies |
| 13 | const external = [ |
| 14 | "@sentry/profiling-node", // Contains native profiler bindings (optional) |
| 15 | "fsevents", // macOS native file watcher (optional dependency) |
| 16 | "./xhr-sync-worker.js", // JSDOM worker file that needs to be copied separately |
| 17 | ]; |
| 18 | |
| 19 | console.log("Building CLI with esbuild..."); |
| 20 | |
| 21 | // Plugin to handle optional react-devtools-core |
| 22 | const optionalDevtoolsPlugin = { |
| 23 | name: "optional-devtools", |
| 24 | setup(build) { |
| 25 | build.onResolve({ filter: /^react-devtools-core$/ }, () => { |
| 26 | // Return path to our stub instead of marking as external |
| 27 | return { path: resolve(__dirname, "stubs/react-devtools-core.js") }; |
| 28 | }); |
| 29 | }, |
| 30 | }; |
| 31 | |
| 32 | try { |
| 33 | const result = await esbuild.build({ |
| 34 | entryPoints: ["src/index.ts"], |
| 35 | bundle: true, |
| 36 | platform: "node", |
| 37 | target: "node18", |
| 38 | format: "esm", |
| 39 | outfile: "dist/index.js", |
| 40 | external, |
| 41 | sourcemap: true, |
| 42 | minify: true, // Minify for smaller bundle size |
| 43 | metafile: true, |
| 44 | plugins: [optionalDevtoolsPlugin], |
| 45 | |
| 46 | // Handle .js extensions in imports |
| 47 | resolveExtensions: [".ts", ".tsx", ".js", ".jsx", ".json"], |
| 48 | |
| 49 | // Handle TypeScript paths and local packages |
| 50 | alias: { |
| 51 | "@continuedev/config-yaml": resolve( |
| 52 | __dirname, |
| 53 | "../../packages/config-yaml/dist/index.js", |
| 54 | ), |
| 55 | "@continuedev/openai-adapters": resolve( |
| 56 | __dirname, |
| 57 | "../../packages/openai-adapters/dist/index.js", |
| 58 | ), |
| 59 | "@continuedev/config-types": resolve( |
| 60 | __dirname, |
| 61 | "../../packages/config-types/dist/index.js", |
| 62 | ), |
| 63 | core: resolve(__dirname, "../../core"), |
| 64 | "@continuedev/fetch": resolve( |
| 65 | __dirname, |
| 66 | "../../packages/fetch/dist/index.js", |
| 67 | ), |
| 68 | "@continuedev/llm-info": resolve( |
| 69 | __dirname, |
| 70 | "../../packages/llm-info/dist/index.js", |
| 71 | ), |
| 72 | "@continuedev/terminal-security": resolve( |
| 73 | __dirname, |
| 74 | "../../packages/terminal-security/dist/index.js", |
| 75 | ), |
| 76 | }, |
| 77 | |
| 78 | // Add banner to create require for CommonJS packages |
| 79 | banner: { |
| 80 | js: `import { createRequire } from 'module'; |
| 81 | const require = createRequire(import.meta.url);`, |
| 82 | }, |
| 83 | }); |
| 84 | |
| 85 | // Write metafile for analysis |
| 86 | writeFileSync("dist/meta.json", JSON.stringify(result.metafile, null, 2)); |
| 87 | |
| 88 | // Copy worker files needed by JSDOM |
| 89 | const workerSource = resolve(__dirname, "node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js"); |
| 90 | const workerDest = resolve(__dirname, "dist/xhr-sync-worker.js"); |
| 91 | try { |
| 92 | copyFileSync(workerSource, workerDest); |
| 93 | console.log("✓ Copied xhr-sync-worker.js"); |
| 94 | } catch (error) { |
| 95 | console.warn("Warning: Could not copy xhr-sync-worker.js:", error.message); |
| 96 | } |
| 97 | |
| 98 | // Create wrapper script with shebang |
| 99 | writeFileSync("dist/cn.js", "#!/usr/bin/env node\nimport('./index.js');"); |
| 100 | |
| 101 | // Make the wrapper script executable |
| 102 | chmodSync("dist/cn.js", 0o755); |
| 103 | |
| 104 | // Calculate bundle size |
| 105 | const bundleSize = result.metafile.outputs["dist/index.js"].bytes; |
| 106 | console.log( |
| 107 | `✓ Build complete! Bundle size: ${(bundleSize / 1024 / 1024).toFixed(2)} MB`, |
| 108 | ); |
| 109 | } catch (error) { |
| 110 | console.error("Build failed:", error); |
| 111 | process.exit(1); |
| 112 | } |