Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 2 additions & 104 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@opentelemetry/otlp-transformer": "0.213.0",
"@opentelemetry/resources": "^2.10.0",
"@opentelemetry/sdk-metrics": "^2.10.0",
"@smithy/core": "3.29.3",
"@smithy/core": "^3.33.3",
"@tanstack/react-query": "^5.101.2",
"cli-truncate": "^6.1.1",
"commander": "^15.0.0",
Expand Down
20 changes: 19 additions & 1 deletion scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { $ } from "bun";
import { existsSync } from "node:fs";
import { chmod } from "node:fs/promises";
import { join, resolve } from "node:path";
import { runWithExitCode } from "../src/runnable";

Expand Down Expand Up @@ -30,6 +31,8 @@ const BOOTSTRAP_TEMPLATE = ["lib", "api", "bootstrap", "bootstrap-template.yaml"
// traces unreadable and erase error names telemetry keys on.
const MINIFY = { whitespace: true, syntax: true, identifiers: false } as const;

const DEFINE = { "process.env.NODE_ENV": JSON.stringify("production") };

/** Absolute paths of every asset file. dot:true so hidden files (.prettierrc) are included. */
function discoverAssets(): string[] {
const files = [...new Bun.Glob("**/*").scanSync({ cwd: ASSETS_DIR, onlyFiles: true, dot: true })];
Expand Down Expand Up @@ -109,23 +112,37 @@ async function assertAssetsAreText(assets: string[]): Promise<void> {
}
}

/**
V8 only caches modules loaded after enableCompileCache(), so the bin is a loader in front of the bundle.
**/
const BIN_LOADER = `#!/usr/bin/env node
import module from "node:module";
module.enableCompileCache?.();
await import("./main.js");
`;

// Bun.build rejects with an AggregateError on failure (throw defaults to true),
// so build errors propagate to runWithExitCode like any other.
async function bundle(): Promise<void> {
await stageInspectorAssets();
await Bun.build({
entrypoints: [ENTRYPOINT],
outdir: DIST,
naming: { entry: "main.js" },
target: "node",
minify: MINIFY,
define: DEFINE,
external: EXTERNAL,
});
const bin = join(DIST, "index.js");
await Bun.write(bin, BIN_LOADER);
await chmod(bin, 0o755);

// Mirror assets beside the emitted module for resolveAssetsRoot().
const distAssets = join(DIST, "assets");
await $`rm -rf ${distAssets}`;
await $`cp -R ${ASSETS_DIR} ${distAssets}`;
console.log(`Bundled to ${join(DIST, "index.js")} with assets/`);
console.log(`Bundled to ${join(DIST, "main.js")} behind ${join(DIST, "index.js")} with assets/`);
}

async function compile(target: string): Promise<void> {
Expand All @@ -145,6 +162,7 @@ async function compile(target: string): Promise<void> {
entrypoints: [ENTRYPOINT, ...assets, template],
compile: { target: target as Bun.Build.CompileTarget, outfile },
minify: MINIFY,
define: DEFINE,
root: REPO_ROOT,
naming: { asset: ASSET_NAMING },
plugins: [assetLoaderPlugin()],
Expand Down
6 changes: 0 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#!/usr/bin/env node

// The shebang above is preserved by the bundler into dist/index.js, making the
// published `bin` directly executable by Node. It's ignored during development
// when the file is run via `bun run src/index.ts`.

import { homedir } from "os";
import { join } from "path";

Expand Down
Loading