From 1cab82b6bf18133c36c3eca36901d27fbcbed71c Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sat, 17 Feb 2024 22:27:40 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20smaller=20binary,=20multiple=20p?= =?UTF-8?q?latforms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/vendor/package-lock.json | 2 +- core/vendor/package.json | 2 +- pkg/README.md | 7 +++++++ pkg/build.js | 26 +++++++++++++++++++++----- pkg/package-lock.json | 2 +- pkg/package.json | 2 +- pkg/pkgJson/darwin-arm64/package.json | 23 +++++++++++++++++++++++ pkg/pkgJson/darwin-x64/package.json | 23 +++++++++++++++++++++++ pkg/pkgJson/linux-arm64/package.json | 23 +++++++++++++++++++++++ pkg/pkgJson/linux-x64/package.json | 23 +++++++++++++++++++++++ pkg/pkgJson/win32-arm64/package.json | 23 +++++++++++++++++++++++ pkg/pkgJson/win32-x64/package.json | 23 +++++++++++++++++++++++ 12 files changed, 170 insertions(+), 9 deletions(-) create mode 100644 pkg/README.md create mode 100644 pkg/pkgJson/darwin-arm64/package.json create mode 100644 pkg/pkgJson/darwin-x64/package.json create mode 100644 pkg/pkgJson/linux-arm64/package.json create mode 100644 pkg/pkgJson/linux-x64/package.json create mode 100644 pkg/pkgJson/win32-arm64/package.json create mode 100644 pkg/pkgJson/win32-x64/package.json diff --git a/core/vendor/package-lock.json b/core/vendor/package-lock.json index 64225d017..3f8229af6 100644 --- a/core/vendor/package-lock.json +++ b/core/vendor/package-lock.json @@ -7,7 +7,7 @@ "": { "name": "vendor", "version": "1.0.0", - "license": "ISC", + "license": "Apache-2.0", "dependencies": { "@xenova/transformers": "^2.14.0" } diff --git a/core/vendor/package.json b/core/vendor/package.json index a7838e858..07cd4c0d6 100644 --- a/core/vendor/package.json +++ b/core/vendor/package.json @@ -7,7 +7,7 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", - "license": "ISC", + "license": "Apache-2.0", "dependencies": { "@xenova/transformers": "^2.14.0" } diff --git a/pkg/README.md b/pkg/README.md new file mode 100644 index 000000000..a7b25757f --- /dev/null +++ b/pkg/README.md @@ -0,0 +1,7 @@ +# Continue Core Binary + +The purpose of this folder is to package Typescript code in a way that can be run from any IDE or platform. We first bundle with `esbuild` and then package into binaries with `pkg`. + +The `pkgJson/package.json` contains instructions for building with pkg, and needs to be in a separte folder because there is no CLI flag for the assets option (it must be in a package.json), and pkg doesn't recognize any name other than package.json, but if we use the same package.json with dependencies in it, pkg will automatically include these, significantly increasing the binary size. + +The build process is otherwise defined entirely in `build.js`. diff --git a/pkg/build.js b/pkg/build.js index 6b9b38c97..a076f803a 100644 --- a/pkg/build.js +++ b/pkg/build.js @@ -21,16 +21,17 @@ for (let i = 2; i < process.argv.length; i++) { const DYNAMIC_IMPORTS = [ "esbuild", "@esbuild", - "@lancedb", + // "@lancedb", "posthog-node", "@octokit", ]; fs.mkdirSync("out/node_modules", { recursive: true }); + fs.mkdirSync("bin/node_modules", { recursive: true }); await Promise.all( DYNAMIC_IMPORTS.map( (mod) => - new Promise((resolve, reject) => + new Promise((resolve, reject) => { ncp( `node_modules/${mod}`, `out/node_modules/${mod}`, @@ -42,8 +43,20 @@ for (let i = 2; i < process.argv.length; i++) { resolve(); } } - ) - ) + ); + ncp( + `node_modules/${mod}`, + `bin/node_modules/${mod}`, + function (error) { + if (error) { + console.error(`[error] Error copying ${mod}`, error); + reject(error); + } else { + resolve(); + } + } + ); + }) ) ); console.log(`[info] Copied ${DYNAMIC_IMPORTS.join(", ")}`); @@ -74,6 +87,9 @@ for (let i = 2; i < process.argv.length; i++) { } console.log("[info] Building binary with pkg..."); - execSync("npx pkg ."); + execSync(`npx pkg pkgJson/darwin-arm64`); + // execSync( + // `npx pkg out/index.js --target node18-darwin-arm64 --no-bytecode --public-packages "*" --public -o bin/pkg` + // ); console.log("[info] Done!"); })(); diff --git a/pkg/package-lock.json b/pkg/package-lock.json index b5a97a7c0..d0b5376ea 100644 --- a/pkg/package-lock.json +++ b/pkg/package-lock.json @@ -7,7 +7,7 @@ "": { "name": "pkg", "version": "1.0.0", - "license": "ISC", + "license": "Apache-2.0", "dependencies": { "@octokit/rest": "^20.0.2", "commander": "^12.0.0", diff --git a/pkg/package.json b/pkg/package.json index 3ea139dcd..0429c35a7 100644 --- a/pkg/package.json +++ b/pkg/package.json @@ -27,7 +27,7 @@ "esbuild": "node build.js --esbuild-only" }, "author": "", - "license": "ISC", + "license": "Apache-2.0", "devDependencies": { "@types/follow-redirects": "^1.14.4", "@types/uuid": "^9.0.8", diff --git a/pkg/pkgJson/darwin-arm64/package.json b/pkg/pkgJson/darwin-arm64/package.json new file mode 100644 index 000000000..3ac12e67a --- /dev/null +++ b/pkg/pkgJson/darwin-arm64/package.json @@ -0,0 +1,23 @@ +{ + "name": "pkg", + "version": "1.0.0", + "description": "", + "bin": "../../out/index.js", + "pkg": { + "scripts": [ + "node_modules/axios/**/*" + ], + "assets": [ + "../../../core/node_modules/sqlite3/**/*", + "../../node_modules/@lancedb/vectordb-darwin-arm64/index.node", + "../../out/tree-sitter.wasm", + "../../out/tree-sitter-wasms/*" + ], + "targets": [ + "node18-macos-arm64" + ], + "outputPath": "bin" + }, + "author": "", + "license": "Apache-2.0" +} diff --git a/pkg/pkgJson/darwin-x64/package.json b/pkg/pkgJson/darwin-x64/package.json new file mode 100644 index 000000000..aa98b9b17 --- /dev/null +++ b/pkg/pkgJson/darwin-x64/package.json @@ -0,0 +1,23 @@ +{ + "name": "pkg", + "version": "1.0.0", + "description": "", + "bin": "../../out/index.js", + "pkg": { + "scripts": [ + "node_modules/axios/**/*" + ], + "assets": [ + "../../../core/node_modules/sqlite3/**/*", + "../../node_modules/@lancedb/**/*", + "../../out/tree-sitter.wasm", + "../../out/tree-sitter-wasms/*" + ], + "targets": [ + "node18-macos-x64" + ], + "outputPath": "bin" + }, + "author": "", + "license": "Apache-2.0" +} diff --git a/pkg/pkgJson/linux-arm64/package.json b/pkg/pkgJson/linux-arm64/package.json new file mode 100644 index 000000000..f1d4c5f67 --- /dev/null +++ b/pkg/pkgJson/linux-arm64/package.json @@ -0,0 +1,23 @@ +{ + "name": "pkg", + "version": "1.0.0", + "description": "", + "bin": "../../out/index.js", + "pkg": { + "scripts": [ + "node_modules/axios/**/*" + ], + "assets": [ + "../../../core/node_modules/sqlite3/**/*", + "../../node_modules/@lancedb/**/*", + "../../out/tree-sitter.wasm", + "../../out/tree-sitter-wasms/*" + ], + "targets": [ + "node18-linux-arm64" + ], + "outputPath": "bin" + }, + "author": "", + "license": "Apache-2.0" +} diff --git a/pkg/pkgJson/linux-x64/package.json b/pkg/pkgJson/linux-x64/package.json new file mode 100644 index 000000000..62f99ebd0 --- /dev/null +++ b/pkg/pkgJson/linux-x64/package.json @@ -0,0 +1,23 @@ +{ + "name": "pkg", + "version": "1.0.0", + "description": "", + "bin": "../../out/index.js", + "pkg": { + "scripts": [ + "node_modules/axios/**/*" + ], + "assets": [ + "../../../core/node_modules/sqlite3/**/*", + "../../node_modules/@lancedb/**/*", + "../../out/tree-sitter.wasm", + "../../out/tree-sitter-wasms/*" + ], + "targets": [ + "node18-linux-x64" + ], + "outputPath": "bin" + }, + "author": "", + "license": "Apache-2.0" +} diff --git a/pkg/pkgJson/win32-arm64/package.json b/pkg/pkgJson/win32-arm64/package.json new file mode 100644 index 000000000..c7b16e035 --- /dev/null +++ b/pkg/pkgJson/win32-arm64/package.json @@ -0,0 +1,23 @@ +{ + "name": "pkg", + "version": "1.0.0", + "description": "", + "bin": "../../out/index.js", + "pkg": { + "scripts": [ + "node_modules/axios/**/*" + ], + "assets": [ + "../../../core/node_modules/sqlite3/**/*", + "../../node_modules/@lancedb/**/*", + "../../out/tree-sitter.wasm", + "../../out/tree-sitter-wasms/*" + ], + "targets": [ + "node18-win-arm64" + ], + "outputPath": "bin" + }, + "author": "", + "license": "Apache-2.0" +} diff --git a/pkg/pkgJson/win32-x64/package.json b/pkg/pkgJson/win32-x64/package.json new file mode 100644 index 000000000..32ac6e583 --- /dev/null +++ b/pkg/pkgJson/win32-x64/package.json @@ -0,0 +1,23 @@ +{ + "name": "pkg", + "version": "1.0.0", + "description": "", + "bin": "../../out/index.js", + "pkg": { + "scripts": [ + "node_modules/axios/**/*" + ], + "assets": [ + "../../../core/node_modules/sqlite3/**/*", + "../../node_modules/@lancedb/**/*", + "../../out/tree-sitter.wasm", + "../../out/tree-sitter-wasms/*" + ], + "targets": [ + "node18-win-x64" + ], + "outputPath": "bin" + }, + "author": "", + "license": "Apache-2.0" +}