diff --git a/package.json b/package.json index 6a2d0faa..c679519e 100644 --- a/package.json +++ b/package.json @@ -44,12 +44,12 @@ "examples/*" ], "scripts": { - "postinstall": "node scripts/setup-bun.mjs || echo 'setup-bun.mjs failed or not available'", "start": "npm run examples:dev", "generate:schemas": "tsx scripts/generate-schemas.ts && prettier --write \"src/generated/**/*\"", "sync:snippets": "bun scripts/sync-snippets.ts", "build": "npm run generate:schemas && npm run sync:snippets && node scripts/run-bun.mjs build.bun.ts && node scripts/link-self.mjs", - "prepack": "npm run build", + "prepack": "npm run build && node scripts/strip-optional-deps.mjs", + "postpack": "node scripts/restore-package-json.mjs", "build:all": "npm run examples:build", "test": "bun test src", "test:e2e": "playwright test", diff --git a/scripts/restore-package-json.mjs b/scripts/restore-package-json.mjs new file mode 100644 index 00000000..8f2bb36d --- /dev/null +++ b/scripts/restore-package-json.mjs @@ -0,0 +1,14 @@ +#!/usr/bin/env node +// Restores the original package.json after packing (saved by strip-optional-deps.mjs). + +import { copyFileSync, unlinkSync } from "fs"; +import { join, dirname } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const pkgPath = join(__dirname, "..", "package.json"); + +copyFileSync(pkgPath + ".bak", pkgPath); +unlinkSync(pkgPath + ".bak"); + +console.log("[restore-package-json] Restored original package.json"); diff --git a/scripts/strip-optional-deps.mjs b/scripts/strip-optional-deps.mjs new file mode 100644 index 00000000..f10a2672 --- /dev/null +++ b/scripts/strip-optional-deps.mjs @@ -0,0 +1,32 @@ +#!/usr/bin/env node +// Strips dev-only platform binaries (@oven/bun-*, @rollup/rollup-*) from +// optionalDependencies before packing. Consumers don't need these. +// Original is saved to package.json.bak and restored by restore-package-json.mjs (postpack). + +import { readFileSync, writeFileSync, copyFileSync } from "fs"; +import { join, dirname } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const pkgPath = join(__dirname, "..", "package.json"); + +copyFileSync(pkgPath, pkgPath + ".bak"); + +const DEV_ONLY_PREFIXES = ["@oven/bun-", "@rollup/rollup-"]; + +const pkg = JSON.parse(readFileSync(pkgPath, "utf8")); +const optional = pkg.optionalDependencies; +if (optional) { + const removed = []; + for (const name of Object.keys(optional)) { + if (DEV_ONLY_PREFIXES.some((p) => name.startsWith(p))) { + delete optional[name]; + removed.push(name); + } + } + if (Object.keys(optional).length === 0) delete pkg.optionalDependencies; + writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n"); + console.log( + `[strip-optional-deps] Removed ${removed.length} dev-only packages from optionalDependencies`, + ); +}