From 27710507cb7cca8919a221ccc17e8a40aecfc8d7 Mon Sep 17 00:00:00 2001 From: Nayan Date: Fri, 7 Aug 2026 20:15:00 +0530 Subject: [PATCH] build(cli): bundle with tsup and vendor the overlay assets The published package has to run from a clean `npx` with no workspace around it, so the overlay bundle, its icons and its token stylesheet are vendored into dist at build time. That is also why every @airship/* dependency is a devDependency here. --- apps/cli/scripts/vendor-assets.mjs | 91 ++++++++++++++++++++++++++++++ apps/cli/tsup.config.ts | 35 ++++++++++++ apps/cli/vitest.config.ts | 6 ++ 3 files changed, 132 insertions(+) create mode 100644 apps/cli/scripts/vendor-assets.mjs create mode 100644 apps/cli/tsup.config.ts create mode 100644 apps/cli/vitest.config.ts diff --git a/apps/cli/scripts/vendor-assets.mjs b/apps/cli/scripts/vendor-assets.mjs new file mode 100644 index 0000000..924db13 --- /dev/null +++ b/apps/cli/scripts/vendor-assets.mjs @@ -0,0 +1,91 @@ +// Copies the runtime assets the server serves but cannot import into the CLI's +// own dist/, so the published tarball is self-contained. +// +// Why this exists: packages/server/src/proxy.ts serves three things it resolves +// at runtime rather than importing — the overlay IIFE, the bippy hook IIFE, and +// the editor's woff2 fonts. They are assets, so no bundler can inline them. +// +// In this monorepo they resolve fine through node_modules. In a published +// install they do not exist at all: tsup inlines @airship/server into the CLI +// bundle, so proxy.ts's import.meta.url ends up pointing at the installed CLI's +// dist/, where no @airship/* package is on disk. dist/vendor/ is the copy that +// makes that case work. See the fallback in proxy.ts. +// +// Sources are resolved through the packages' own export maps rather than +// hardcoded ../../packages/… paths, so a layout change there fails loudly here +// instead of silently shipping a stale asset. + +import { copyFileSync, existsSync, mkdirSync } from "node:fs"; +import { createRequire } from "node:module"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const require = createRequire(import.meta.url); +const distDir = fileURLToPath(new URL("../dist", import.meta.url)); +const vendorDir = join(distDir, "vendor"); +const fontsDir = join(vendorDir, "fonts"); + +const FONTS = [ + "inter-variable.woff2", + "jetbrains-mono-400.woff2", + "jetbrains-mono-700.woff2", +]; + +function fail(what, cause) { + console.error(`vendor-assets: could not resolve ${what}`); + console.error(` ${cause}`); + console.error(" Build the workspace first: make build"); + process.exit(1); +} + +function resolve(specifier, what) { + try { + return require.resolve(specifier); + } catch (error) { + fail(what, error.message); + } +} + +// Copies a file and, when present, the sourcemap sitting next to it. proxy.ts +// serves `${bundle}.map` derived from whatever path it resolved, so the map has +// to land beside its bundle here too. +function copyWithMap(from, toDir) { + const name = from.split("/").pop(); + copyFileSync(from, join(toDir, name)); + if (existsSync(`${from}.map`)) { + copyFileSync(`${from}.map`, join(toDir, `${name}.map`)); + } +} + +if (!existsSync(distDir)) { + fail( + "dist/", + "the CLI has not been built yet — tsup runs before this script" + ); +} + +mkdirSync(fontsDir, { recursive: true }); + +copyWithMap( + resolve("@airship/overlay/bundle", "the overlay bundle"), + vendorDir +); +copyWithMap(resolve("@airship/overlay/hook", "the bippy hook"), vendorDir); + +for (const font of FONTS) { + const from = resolve(`@airship/editor-tokens/fonts/${font}`, `font ${font}`); + copyFileSync(from, join(fontsDir, font)); +} + +// Belt and braces: a silently empty vendor/ is the exact failure this script +// exists to prevent, and it would only surface as a 404 in a published install. +const expected = ["overlay.global.js", "hook.global.js"]; +for (const name of expected) { + if (!existsSync(join(vendorDir, name))) { + fail(name, `it was not written to ${vendorDir}`); + } +} + +console.log( + `vendor-assets: ${expected.length} bundles + ${FONTS.length} fonts -> ${dirname(vendorDir)}/vendor` +); diff --git a/apps/cli/tsup.config.ts b/apps/cli/tsup.config.ts new file mode 100644 index 0000000..771caa6 --- /dev/null +++ b/apps/cli/tsup.config.ts @@ -0,0 +1,35 @@ +import { readFileSync } from "node:fs"; +import { defineConfig } from "tsup"; + +const pkg: { version: string } = JSON.parse( + readFileSync(new URL("package.json", import.meta.url), "utf8") +); + +export default defineConfig({ + banner: { js: "#!/usr/bin/env node" }, + clean: true, + // Baked in at build time rather than read from package.json at runtime: the + // bundle is a single file with no package.json beside it, and resolving one + // relative to import.meta.url would break the moment it is moved or linked. + define: { __AIRSHIP_VERSION__: JSON.stringify(pkg.version) }, + dts: false, + entry: { index: "src/index.ts" }, + format: ["esm"], + // Inline every workspace package so the published tarball declares no + // @airship/* runtime dependency — none of them is published, so a consumer + // resolving one would 404. They stay in devDependencies purely to build. + // + // Third-party deps stay external on purpose. The three agent SDKs have to: + // each resolves a per-platform binary package at runtime (the reason + // pnpm-workspace.yaml pins them all in minimumReleaseAgeExclude), and + // bundling severs that resolution. The rest are left external because a + // debuggable node_modules beats a marginally smaller bundle for a CLI. + // + // The overlay IIFEs and the editor fonts are NOT covered by this: they are + // assets, resolved at runtime rather than imported. scripts/vendor-assets.mjs + // copies them into dist/vendor/. See packages/server/src/proxy.ts. + noExternal: [/^@airship\//], + platform: "node", + sourcemap: true, + target: "node22", +}); diff --git a/apps/cli/vitest.config.ts b/apps/cli/vitest.config.ts new file mode 100644 index 0000000..7ae5a6e --- /dev/null +++ b/apps/cli/vitest.config.ts @@ -0,0 +1,6 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + define: { __AIRSHIP_VERSION__: JSON.stringify("0.0.0-test") }, + test: { include: ["src/**/*.test.ts"] }, +});