Use esbuild to properly bundle API functions for Vercel
cgen-3f921d54f4e54eed8476fce763cc895a
This commit is contained in:
parent
3ba129df07
commit
9190dfcb68
1 changed files with 58 additions and 17 deletions
75
build-api.js
75
build-api.js
|
|
@ -2,34 +2,75 @@
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
|
import * as esbuild from "esbuild";
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
console.log("Preparing API routes for Vercel...");
|
console.log("Building API routes for Vercel with esbuild...");
|
||||||
|
|
||||||
const srcApi = path.resolve(__dirname, "api");
|
const srcApi = path.resolve(__dirname, "api");
|
||||||
const destApi = path.resolve(__dirname, "..", "api");
|
const destApi = path.resolve(__dirname, "..", "api");
|
||||||
|
|
||||||
function copyDir(src, dest) {
|
// Ensure destination exists
|
||||||
if (fs.existsSync(dest)) {
|
if (fs.existsSync(destApi)) {
|
||||||
fs.rmSync(dest, { recursive: true, force: true });
|
fs.rmSync(destApi, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
fs.mkdirSync(dest, { recursive: true });
|
fs.mkdirSync(destApi, { recursive: true });
|
||||||
|
|
||||||
const entries = fs.readdirSync(src, { withFileTypes: true });
|
// Find all TypeScript files in src/api
|
||||||
|
const tsFiles = [];
|
||||||
|
|
||||||
|
function findTsFiles(dir, prefix = "") {
|
||||||
|
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
const srcPath = path.join(src, entry.name);
|
const fullPath = path.join(dir, entry.name);
|
||||||
const destPath = path.join(dest, entry.name);
|
|
||||||
|
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
copyDir(srcPath, destPath);
|
findTsFiles(fullPath, prefix + entry.name + "/");
|
||||||
} else {
|
} else if (entry.name.endsWith(".ts") && !entry.name.endsWith(".d.ts")) {
|
||||||
fs.copyFileSync(srcPath, destPath);
|
tsFiles.push(fullPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Copying TypeScript API files from ${srcApi}...`);
|
findTsFiles(srcApi);
|
||||||
copyDir(srcApi, destApi);
|
|
||||||
console.log(`✓ API files copied to ${destApi}`);
|
if (tsFiles.length === 0) {
|
||||||
console.log("Vercel will compile TypeScript automatically.");
|
console.log("No TypeScript files found");
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Found ${tsFiles.length} TypeScript files`);
|
||||||
|
|
||||||
|
// Build each file separately to preserve structure
|
||||||
|
async function buildAll() {
|
||||||
|
for (const tsFile of tsFiles) {
|
||||||
|
const relativePath = path.relative(srcApi, tsFile);
|
||||||
|
const outFile = path.join(destApi, relativePath.replace(/\.ts$/, ".js"));
|
||||||
|
const outDir = path.dirname(outFile);
|
||||||
|
|
||||||
|
if (!fs.existsSync(outDir)) {
|
||||||
|
fs.mkdirSync(outDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await esbuild.build({
|
||||||
|
entryPoints: [tsFile],
|
||||||
|
outfile: outFile,
|
||||||
|
platform: "node",
|
||||||
|
target: "es2020",
|
||||||
|
format: "esm",
|
||||||
|
external: ["@supabase/supabase-js", "nodemailer", "stripe", "ethers"],
|
||||||
|
bundle: false,
|
||||||
|
sourcemap: false,
|
||||||
|
logLevel: "silent",
|
||||||
|
});
|
||||||
|
console.log(`✓ ${relativePath}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`✗ Failed to build ${relativePath}:`, error.message);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await buildAll();
|
||||||
|
console.log(`\n✓ API build complete! ${tsFiles.length} files compiled.`);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue