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