Skip to content
Open
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
45 changes: 40 additions & 5 deletions src/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
/* eslint-disable no-console */

import { build } from 'esbuild';
import { build, BuildOptions } from 'esbuild';
import { chmod, mkdir, rm } from 'fs/promises';

import { GlobalIdentifierName, globalIdentifiers } from './globalIdentifiers';
import { isVariant, variants } from './variants';

const dev = process.argv.includes('--dev');
const variant = process.argv.includes('--variant')
? process.argv[process.argv.indexOf('--variant') + 1]
: 'default';

if (!isVariant(variant)) {
throw new Error(`Invalid variant: ${variant}. Expected one of: ${variants.join(', ')}`);
}

const globalValues: Record<GlobalIdentifierName, string> = {
BUILD_VARIANT: variant,
};

(async () => {
await rm('./build', { recursive: true, force: true });

console.log('🏗️ Building...');
const result = await build({
console.log(`🏗️ Building ${variant} variant...`);
const buildOptions: BuildOptions = {
entryPoints: ['./src/index.ts'],
bundle: true,
platform: 'node',
Expand All @@ -22,7 +36,28 @@ const dev = process.argv.includes('--dev');
'empty-import-meta': 'silent',
},
outfile: './build/index.js',
});
// https://esbuild.github.io/api/#define
define: {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design Q: do we want BUILD_VARIANT to be runtime-readable outside the variant entry points at all?

The entry-point swap already gives us a clean build boundary, and exposing import.meta.env.BUILD_VARIANT more broadly seems like it could invite shared modules to grow variant conditionals over time

Keep variant selection constrained to the entry/build layer unless a concrete use case shows up?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout. I tried to get stuff working without using define at all, but it got kinda messy. Let me give it another shot.

// 'import.meta.env.BUILD_VARIANT': JSON.stringify(env.BUILD_VARIANT),
...globalIdentifiers.reduce<Record<`import.meta.env.${string}`, string>>(
(acc, { name, defaultValue }) => {
acc[`import.meta.env.${name}`] = JSON.stringify(globalValues[name] ?? defaultValue);
return acc;
},
{},
),
},
// must be last so that the action can override previous build options
...globalIdentifiers.reduce((acc, { name, defaultValue, action }) => {
return { ...acc, ...action(globalValues[name] ?? defaultValue) };
}, {}),
};

if (!buildOptions.outfile) {
throw new Error('outfile build option must be specified');
}

const result = await build(buildOptions);

for (const error of result.errors) {
console.log(`❌ ${error.text}`);
Expand Down Expand Up @@ -53,5 +88,5 @@ const dev = process.argv.includes('--dev');
console.log(`⚠️ ${warning.text}`);
}

await chmod('./build/index.js', '755');
await chmod(buildOptions.outfile, '755');
})();
1 change: 0 additions & 1 deletion src/scripts/createClaudeMcpBundleManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import packageJson from '../../package.json';
import { ProcessEnvEx } from '../../types/process-env.js';
import { webToolNames } from '../tools/web/toolName.js';

// @ts-expect-error - import.meta is not allowed in CommonJS output, but this file is built using esbuild as ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

Expand Down
32 changes: 32 additions & 0 deletions src/scripts/globalIdentifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { BuildOptions } from 'esbuild';

import { Variant } from './variants';

export type GlobalIdentifierName = 'BUILD_VARIANT';

type GlobalIdentifier = {
name: GlobalIdentifierName;
defaultValue: string;
action: (value: string) => BuildOptions | undefined;
};

export const globalIdentifiers: ReadonlyArray<GlobalIdentifier> = [
{
name: 'BUILD_VARIANT',
defaultValue: 'default' satisfies Variant,
action: (value: string): BuildOptions | undefined => {
if (value === 'default') {
return {
entryPoints: ['./src/index.ts'],
outfile: './build/index.js',
};
}

// variant "foo" has an entry point of src/index.foo.ts and an outfile of build/index.foo.js
return {
entryPoints: [`./src/index.${value}.ts`],
outfile: `./build/index.${value}.js`,
};
},
},
] as const;
5 changes: 5 additions & 0 deletions src/scripts/variants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const variants = ['default'] as const;
export type Variant = (typeof variants)[number];
export function isVariant(value: unknown): value is Variant {
return variants.some((variant) => variant === value);
}
2 changes: 1 addition & 1 deletion tests/eval/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
StreamedRunResult,
withTrace,
} from '@openai/agents';
import { OpenAI } from 'openai/client.js';
import OpenAI from 'openai';
import { Err, Ok, Result } from 'ts-results-es';
import z from 'zod';

Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"module": "ESNext",
"moduleResolution": "bundler",
"noEmit": true,
"outDir": "./build",
"rootDir": "./",
"strict": true,
Expand Down
5 changes: 5 additions & 0 deletions types/import-meta.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface ImportMeta {
readonly env: {
readonly BUILD_VARIANT: string;
};
}