diff --git a/package.json b/package.json index b56c54c..539e0d4 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "scripts": { "typecheck": "bun --filter '*' typecheck", "test": "bun test", - "build": "bun --filter '*' build" + "build": "bun --filter '*' build", + "postinstall": "bun --filter '@loreai/gateway' build" }, "devDependencies": { "@types/bun": "^1.2.0", diff --git a/packages/gateway/package.json b/packages/gateway/package.json index 324e935..681f9d8 100644 --- a/packages/gateway/package.json +++ b/packages/gateway/package.json @@ -23,8 +23,7 @@ "build": "bun run script/build.ts", "bundle": "bun run script/bundle.ts", "build:binary": "bun run script/build.ts --binary", - "start": "bun run src/index.ts", - "prepare": "node -e \"var f=require('fs'),p='dist/index.bun.js';if(!f.existsSync(p)){f.mkdirSync('dist',{recursive:true});f.writeFileSync(p,'export * from \\\"../src/index.ts\\\";\\n')}\"" + "start": "bun run src/index.ts" }, "dependencies": { "p-limit": "7" diff --git a/packages/gateway/script/build.ts b/packages/gateway/script/build.ts index dc0c8dd..9f2f6e1 100644 --- a/packages/gateway/script/build.ts +++ b/packages/gateway/script/build.ts @@ -89,10 +89,32 @@ const { values: flags } = parseArgs({ // --------------------------------------------------------------------------- async function buildLibrary() { - // No-op: the npm package is built by `bun run bundle` (script/bundle.ts) - // which produces the self-contained CJS bundle (dist/index.cjs + dist/bin.cjs). - // This function exists only so `bun run build` (workspace-wide) doesn't fail. - console.log("⏭ @loreai/gateway: use `bun run bundle` for npm build"); + // Create lightweight dev shims so workspace consumers can resolve + // the "bun" export condition without running the full `bun run bundle`. + // Real bundle builds (bundle.ts) wipe dist/ first, so these shims + // never interfere with production artifacts. + mkdirSync(distDir, { recursive: true }); + + const shims: Array<[string, string]> = [ + ["index.bun.js", 'export * from "../src/index.ts";\n'], + ["embedding-worker.js", 'export * from "../../core/src/embedding-worker.ts";\n'], + ]; + + for (const [filename, content] of shims) { + const filePath = join(distDir, filename); + if (existsSync(filePath)) { + // Don't overwrite real bundle output (minified, large files). + const existing = readFileSync(filePath, "utf8"); + if (!existing.startsWith("export *")) { + console.log(` ${filename}: skipped (real bundle exists)`); + continue; + } + } + writeFileSync(filePath, content); + console.log(` ${filename}: dev shim created`); + } + + console.log("✓ @loreai/gateway: dev shims ready (use `bun run bundle` for npm build)"); } // --------------------------------------------------------------------------- diff --git a/packages/gateway/test/bundle-exports.test.ts b/packages/gateway/test/bundle-exports.test.ts index 12942e8..976411d 100644 --- a/packages/gateway/test/bundle-exports.test.ts +++ b/packages/gateway/test/bundle-exports.test.ts @@ -18,7 +18,9 @@ import { fileURLToPath } from "node:url"; const packageDir = join(fileURLToPath(import.meta.url), "..", ".."); const distDir = join(packageDir, "dist"); const pkgJson = JSON.parse(readFileSync(join(packageDir, "package.json"), "utf8")); -const hasBundle = existsSync(join(distDir, "index.cjs")) && existsSync(join(distDir, "index.bun.js")); +const hasBunBundle = existsSync(join(distDir, "index.bun.js")) && + !readFileSync(join(distDir, "index.bun.js"), "utf8").startsWith("export *"); +const hasBundle = existsSync(join(distDir, "index.cjs")) && hasBunBundle; describe.skipIf(!hasBundle)("bundle exports", () => { // -------------------------------------------------------------------------