Elffuss BraidHub

kikocis/continue public

2b414e1c0982 · 5.9 KiBRawPermalink
1const esbuild = require("esbuild");
2const fs = require("fs");
3const path = require("path");
4const ncp = require("ncp").ncp;
5const { rimrafSync } = require("rimraf");
6const { validateFilesPresent } = require("../scripts/util");
7const { ALL_TARGETS, TARGET_TO_LANCEDB } = require("./utils/targets");
8const { fork } = require("child_process");
9const {
10 installAndCopyNodeModules,
11} = require("../extensions/vscode/scripts/install-copy-nodemodule");
12const { bundleBinary } = require("./utils/bundle-binary");
13
14const bin = path.join(__dirname, "bin");
15const out = path.join(__dirname, "out");
16const build = path.join(__dirname, "build");
17
18function cleanSlate() {
19 // Clean slate
20 rimrafSync(bin);
21 rimrafSync(out);
22 rimrafSync(build);
23 rimrafSync(path.join(__dirname, "tmp"));
24 fs.mkdirSync(bin);
25 fs.mkdirSync(out);
26 fs.mkdirSync(build);
27}
28
29const esbuildOutputFile = "out/index.js";
30let targets = [...ALL_TARGETS];
31
32const assetBackups = [
33 "node_modules/win-ca/lib/crypt32-ia32.node.bak",
34 "node_modules/win-ca/lib/crypt32-x64.node.bak",
35];
36
37let esbuildOnly = false;
38for (let i = 2; i < process.argv.length; i++) {
39 if (process.argv[i] === "--esbuild-only") {
40 esbuildOnly = true;
41 }
42 if (process.argv[i - 1] === "--target") {
43 targets = [process.argv[i]];
44 }
45}
46
47// Bundles the extension into one file
48async function buildWithEsbuild() {
49 console.log("[info] Building with esbuild...");
50 await esbuild.build({
51 entryPoints: ["src/index.ts"],
52 bundle: true,
53 outfile: esbuildOutputFile,
54 external: [
55 "esbuild",
56 "./xhr-sync-worker.js",
57 "llamaTokenizerWorkerPool.mjs",
58 "tiktokenWorkerPool.mjs",
59 "vscode",
60 "./index.node",
61 ],
62 format: "cjs",
63 platform: "node",
64 sourcemap: true,
65 minify: !esbuildOnly,
66 treeShaking: true,
67 loader: {
68 // eslint-disable-next-line @typescript-eslint/naming-convention
69 ".node": "file",
70 },
71
72 // To allow import.meta.path for transformers.js
73 // https://github.com/evanw/esbuild/issues/1492#issuecomment-893144483
74 inject: ["./importMetaUrl.js"],
75 define: { "import.meta.url": "importMetaUrl" },
76 });
77}
78
79(async () => {
80 if (esbuildOnly) {
81 await buildWithEsbuild();
82 return;
83 }
84
85 cleanSlate();
86
87 // Informs of where to look for node_sqlite3.node https://www.npmjs.com/package/bindings#:~:text=The%20searching%20for,file%20is%20found
88 // This is only needed for our `pkg` command at build time
89 fs.writeFileSync(
90 "out/package.json",
91 JSON.stringify(
92 {
93 name: "binary",
94 version: "1.0.0",
95 author: "Continue Dev, Inc",
96 license: "Apache-2.0",
97 },
98 undefined,
99 2,
100 ),
101 );
102
103 const copyLanceDBPromises = [];
104 for (const target of targets) {
105 if (!TARGET_TO_LANCEDB[target]) {
106 continue;
107 }
108 console.log(`[info] Downloading for ${target}...`);
109 copyLanceDBPromises.push(
110 installAndCopyNodeModules(TARGET_TO_LANCEDB[target], "@lancedb"),
111 );
112 }
113 await Promise.all(copyLanceDBPromises).catch(() => {
114 console.error("[error] Failed to copy LanceDB");
115 process.exit(1);
116 });
117 console.log("[info] Copied all LanceDB");
118
119 // tree-sitter-wasm
120 const treeSitterWasmsDir = path.join(out, "tree-sitter-wasms");
121 fs.mkdirSync(treeSitterWasmsDir);
122 await new Promise((resolve, reject) => {
123 ncp(
124 path.join(
125 __dirname,
126 "..",
127 "core",
128 "node_modules",
129 "tree-sitter-wasms",
130 "out",
131 ),
132 treeSitterWasmsDir,
133 { dereference: true },
134 (error) => {
135 if (error) {
136 console.warn("[error] Error copying tree-sitter-wasm files", error);
137 reject(error);
138 } else {
139 resolve();
140 }
141 },
142 );
143 });
144
145 const filesToCopy = [
146 "../core/vendor/tree-sitter.wasm",
147 "../core/llm/llamaTokenizerWorkerPool.mjs",
148 "../core/llm/llamaTokenizer.mjs",
149 "../core/llm/tiktokenWorkerPool.mjs",
150 ];
151 for (const f of filesToCopy) {
152 fs.copyFileSync(
153 path.join(__dirname, f),
154 path.join(__dirname, "out", path.basename(f)),
155 );
156 console.log(`[info] Copied ${path.basename(f)}`);
157 }
158
159 console.log("[info] Cleaning up artifacts from previous builds...");
160
161 // delete asset backups generated by previous pkg invocations, if present
162 for (const assetPath of assetBackups) {
163 fs.rmSync(assetPath, { force: true });
164 }
165
166 await buildWithEsbuild();
167
168 // Copy over any worker files
169 fs.cpSync(
170 "../core/node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js",
171 "out/xhr-sync-worker.js",
172 );
173 fs.cpSync("../core/llm/tiktokenWorkerPool.mjs", "out/tiktokenWorkerPool.mjs");
174 fs.cpSync(
175 "../core/llm/llamaTokenizerWorkerPool.mjs",
176 "out/llamaTokenizerWorkerPool.mjs",
177 );
178
179 const buildBinaryPromises = [];
180 console.log("[info] Building binaries with pkg...");
181 for (const target of targets) {
182 buildBinaryPromises.push(bundleBinary(target));
183 }
184 await Promise.all(buildBinaryPromises).catch(() => {
185 console.error("[error] Failed to build binaries");
186 process.exit(1);
187 });
188 console.log("[info] All binaries built");
189
190 // Cleanup - this is needed when running locally
191 fs.rmSync("out/package.json");
192
193 const pathsToVerify = [];
194 for (const target of targets) {
195 const exe = target.startsWith("win") ? ".exe" : "";
196 const targetDir = `bin/${target}`;
197 pathsToVerify.push(
198 `${targetDir}/continue-binary${exe}`,
199 `${targetDir}/index.node`, // @lancedb
200 `${targetDir}/build/Release/node_sqlite3.node`,
201 `${targetDir}/rg${exe}`, // ripgrep binary
202 );
203 }
204
205 // Note that this doesn't verify they actually made it into the binary, just that they were in the expected folder before it was built
206 pathsToVerify.push("out/index.js");
207 pathsToVerify.push("out/llamaTokenizerWorkerPool.mjs");
208 pathsToVerify.push("out/tiktokenWorkerPool.mjs");
209 pathsToVerify.push("out/xhr-sync-worker.js");
210 pathsToVerify.push("out/tree-sitter.wasm");
211
212 validateFilesPresent(pathsToVerify);
213
214 console.log("[info] Done!");
215 process.exit(0);
216})();