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
8 changes: 8 additions & 0 deletions apps/minting-service/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"projectType": "application",
"tags": ["scope:service", "type:app"],
"targets": {
"build": {
"executor": "nx:run-commands",
"outputs": ["{projectRoot}/.vercel/output"],
"options": {
"command": "node scripts/build.mjs",
"cwd": "apps/minting-service"
}
},
"lint": { "executor": "@nx/eslint:lint" },
"test": {
"executor": "@nx/vitest:test",
Expand Down
95 changes: 95 additions & 0 deletions apps/minting-service/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
/**
* Builds the minting-service API functions using Vercel Build Output API v3.
*
* - Bundles each `api/*.ts` into a self-contained CommonJS module via esbuild,
* inlining workspace deps (@cacheplane/*) and npm deps alike so no install
* step is required inside each function directory.
* - Writes to `.vercel/output/functions/api/<name>.func/` with the companion
* `.vc-config.json` Vercel's Node runtime expects.
* - Writes `.vercel/output/config.json` at the top level so Vercel picks up
* the Build Output API layout instead of scanning `api/` for TS sources.
*
* Run via `nx build minting-service`, which sets `cwd` to this app.
*/
import { build } from 'esbuild';
import { mkdir, readdir, rm, writeFile } from 'node:fs/promises';
import { basename, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const appRoot = resolve(fileURLToPath(import.meta.url), '..', '..');
const apiDir = join(appRoot, 'api');
const outputRoot = join(appRoot, '.vercel', 'output');
const functionsRoot = join(outputRoot, 'functions', 'api');

async function listEntries() {
const files = await readdir(apiDir);
return files
.filter((f) => f.endsWith('.ts') && !f.endsWith('.spec.ts') && !f.endsWith('.d.ts'))
.map((f) => join(apiDir, f));
}

async function buildEntry(entry) {
const name = basename(entry, '.ts');
const funcDir = join(functionsRoot, `${name}.func`);
await mkdir(funcDir, { recursive: true });

await build({
entryPoints: [entry],
outfile: join(funcDir, 'index.js'),
bundle: true,
platform: 'node',
target: 'node20',
format: 'cjs',
// Lets esbuild resolve `@cacheplane/*` via tsconfig paths and
// follows `extends` up to the workspace tsconfig.base.json.
tsconfig: join(appRoot, 'tsconfig.app.json'),
// @vercel/node provides these as ambient in the runtime environment.
external: ['@vercel/node'],
sourcemap: 'inline',
logLevel: 'info',
});

const vcConfig = {
runtime: 'nodejs20.x',
handler: 'index.js',
launcherType: 'Nodejs',
shouldAddHelpers: true,
maxDuration: 10,
};
await writeFile(join(funcDir, '.vc-config.json'), JSON.stringify(vcConfig, null, 2));

// The app's package.json declares `"type": "module"`, which would make Node
// load `index.js` as ESM. esbuild emits CJS, so drop a colocated package.json
// that pins `commonjs` inside each function directory.
await writeFile(
join(funcDir, 'package.json'),
JSON.stringify({ type: 'commonjs' }, null, 2),
);
}

async function main() {
await rm(outputRoot, { recursive: true, force: true });
await mkdir(functionsRoot, { recursive: true });

const entries = await listEntries();
if (entries.length === 0) {
throw new Error(`no api entries found in ${apiDir}`);
}

await Promise.all(entries.map(buildEntry));

// Minimal Build Output API v3 config — no custom routes needed;
// Vercel maps `functions/api/<name>.func/` to `/api/<name>` by convention.
await writeFile(
join(outputRoot, 'config.json'),
JSON.stringify({ version: 3 }, null, 2),
);

console.log(`\nbuilt ${entries.length} function(s) to ${outputRoot}`);
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
10 changes: 2 additions & 8 deletions apps/minting-service/vercel.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
{
"installCommand": "cd ../.. && npm ci",
"buildCommand": "cd ../.. && npx tsc --noEmit -p apps/minting-service/tsconfig.app.json",
"framework": null,
"functions": {
"api/*.ts": {
"runtime": "nodejs20.x",
"maxDuration": 10
}
}
"buildCommand": "cd ../.. && npx nx build minting-service",
"framework": null
}
Loading