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.
This commit is contained in:
@@ -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`
|
||||
);
|
||||
@@ -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",
|
||||
});
|
||||
@@ -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"] },
|
||||
});
|
||||
Reference in New Issue
Block a user