Elffuss BraidHub

kikocis/continue public

b87cf24dd006 · 2.8 KiBRawPermalink
1#!/usr/bin/env node
2
3import * as esbuild from "esbuild";
4import { writeFileSync, chmodSync } from "fs";
5import { resolve, dirname } from "path";
6import { fileURLToPath } from "url";
7
8const __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
13const external = [
14 "@sentry/profiling-node", // Contains native profiler bindings (optional)
15 "fsevents", // macOS native file watcher (optional dependency)
16];
17
18console.log("Building CLI with esbuild...");
19
20// Plugin to handle optional react-devtools-core
21const 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
31try {
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(__dirname, "../../packages/fetch/dist/index.js"),
64 "@continuedev/llm-info": resolve(__dirname, "../../packages/llm-info/dist/index.js"),
65 },
66
67 // Add banner to create require for CommonJS packages
68 banner: {
69 js: `import { createRequire } from 'module';
70const require = createRequire(import.meta.url);`,
71 },
72 });
73
74 // Write metafile for analysis
75 writeFileSync("dist/meta.json", JSON.stringify(result.metafile, null, 2));
76
77 // Create wrapper script with shebang
78 writeFileSync("dist/cn.js", "#!/usr/bin/env node\nimport('./index.js');");
79
80 // Make the wrapper script executable
81 chmodSync("dist/cn.js", 0o755);
82
83 // Calculate bundle size
84 const bundleSize = result.metafile.outputs["dist/index.js"].bytes;
85 console.log(
86 `✓ Build complete! Bundle size: ${(bundleSize / 1024 / 1024).toFixed(2)} MB`,
87 );
88} catch (error) {
89 console.error("Build failed:", error);
90 process.exit(1);
91}