| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | import * as esbuild from "esbuild"; |
| 4 | import { chmodSync, writeFileSync } 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 | ]; |
| 17 | |
| 18 | console.log("Building CLI with esbuild..."); |
| 19 | |
| 20 | // Plugin to handle optional react-devtools-core |
| 21 | const optionalDevtoolsPlugin = { |
| 22 | name: "optional-devtools", |
| 23 | setup(build) { |
| 24 | build.onResolve({ filter: /^react-devtools-core$/ }, () => { |
| 25 | // Return path to our stub instead of marking as external |
| 26 | return { path: resolve(__dirname, "stubs/react-devtools-core.js") }; |
| 27 | }); |
| 28 | }, |
| 29 | }; |
| 30 | |
| 31 | try { |
| 32 | const result = await esbuild.build({ |
| 33 | entryPoints: ["src/index.ts"], |
| 34 | bundle: true, |
| 35 | platform: "node", |
| 36 | target: "node18", |
| 37 | format: "esm", |
| 38 | outfile: "dist/index.js", |
| 39 | external, |
| 40 | sourcemap: true, |
| 41 | minify: true, // Minify for smaller bundle size |
| 42 | metafile: true, |
| 43 | plugins: [optionalDevtoolsPlugin], |
| 44 | |
| 45 | // Handle .js extensions in imports |
| 46 | resolveExtensions: [".ts", ".tsx", ".js", ".jsx", ".json"], |
| 47 | |
| 48 | // Handle TypeScript paths and local packages |
| 49 | alias: { |
| 50 | "@continuedev/config-yaml": resolve( |
| 51 | __dirname, |
| 52 | "../../packages/config-yaml/dist/index.js", |
| 53 | ), |
| 54 | "@continuedev/openai-adapters": resolve( |
| 55 | __dirname, |
| 56 | "../../packages/openai-adapters/dist/index.js", |
| 57 | ), |
| 58 | "@continuedev/config-types": resolve( |
| 59 | __dirname, |
| 60 | "../../packages/config-types/dist/index.js", |
| 61 | ), |
| 62 | core: resolve(__dirname, "../../core"), |
| 63 | "@continuedev/fetch": resolve( |
| 64 | __dirname, |
| 65 | "../../packages/fetch/dist/index.js", |
| 66 | ), |
| 67 | "@continuedev/llm-info": resolve( |
| 68 | __dirname, |
| 69 | "../../packages/llm-info/dist/index.js", |
| 70 | ), |
| 71 | "@continuedev/terminal-security": resolve( |
| 72 | __dirname, |
| 73 | "../../packages/terminal-security/dist/index.js", |
| 74 | ), |
| 75 | }, |
| 76 | |
| 77 | // Add banner to create require for CommonJS packages |
| 78 | banner: { |
| 79 | js: `import { createRequire } from 'module'; |
| 80 | const require = createRequire(import.meta.url);`, |
| 81 | }, |
| 82 | }); |
| 83 | |
| 84 | // Write metafile for analysis |
| 85 | writeFileSync("dist/meta.json", JSON.stringify(result.metafile, null, 2)); |
| 86 | |
| 87 | // Create wrapper script with shebang |
| 88 | writeFileSync("dist/cn.js", "#!/usr/bin/env node\nimport('./index.js');"); |
| 89 | |
| 90 | // Make the wrapper script executable |
| 91 | chmodSync("dist/cn.js", 0o755); |
| 92 | |
| 93 | // Calculate bundle size |
| 94 | const bundleSize = result.metafile.outputs["dist/index.js"].bytes; |
| 95 | console.log( |
| 96 | `✓ Build complete! Bundle size: ${(bundleSize / 1024 / 1024).toFixed(2)} MB`, |
| 97 | ); |
| 98 | } catch (error) { |
| 99 | console.error("Build failed:", error); |
| 100 | process.exit(1); |
| 101 | } |