Simplified API build script - just copy TypeScript files for Vercel
cgen-cee3a334016640208074be832a53d3b9
This commit is contained in:
parent
9f8cc8f0f7
commit
ba06dd40ac
1 changed files with 3 additions and 66 deletions
69
build-api.js
69
build-api.js
|
|
@ -2,12 +2,11 @@
|
|||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { execSync } from "child_process";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// First, copy API files to root
|
||||
console.log("Copying API files...");
|
||||
// Copy API files from code/api to /api at root
|
||||
console.log("Copying API files for Vercel...");
|
||||
const srcApi = path.resolve(__dirname, "api");
|
||||
const destApi = path.resolve(__dirname, "..", "api");
|
||||
|
||||
|
|
@ -32,66 +31,4 @@ function copyDir(src, dest) {
|
|||
|
||||
copyDir(srcApi, destApi);
|
||||
console.log(`✓ API files copied to ${destApi}`);
|
||||
|
||||
// Now transpile TypeScript files to JavaScript using esbuild
|
||||
console.log("Transpiling API routes to JavaScript...");
|
||||
try {
|
||||
// Use esbuild to transpile all .ts files in the API directory
|
||||
execSync(
|
||||
`npx esbuild "${destApi}/**/*.ts" --platform=node --target=es2020 --format=esm --outdir="${destApi}" --allow-overwrite`,
|
||||
{
|
||||
cwd: __dirname,
|
||||
stdio: "inherit",
|
||||
env: { ...process.env, SKIP_TSC: "true" },
|
||||
},
|
||||
);
|
||||
console.log("✓ API routes transpiled successfully");
|
||||
} catch (error) {
|
||||
console.error("Error transpiling API routes:", error.message);
|
||||
}
|
||||
|
||||
// Fix ESM imports by adding .js extensions to relative imports
|
||||
console.log("Fixing ESM imports...");
|
||||
function fixImportsInDir(dir) {
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
fixImportsInDir(fullPath);
|
||||
} else if (entry.name.endsWith(".js")) {
|
||||
let content = fs.readFileSync(fullPath, "utf-8");
|
||||
|
||||
// Fix: import x from "../../_supabase" -> import x from "../../_supabase.js"
|
||||
// Handle both double and single quotes
|
||||
let fixedContent = content.replace(
|
||||
/from\s+"(\.\.?\/[^"]+)"/g,
|
||||
(match, importPath) => {
|
||||
if (importPath.endsWith(".js")) {
|
||||
return match;
|
||||
}
|
||||
return `from "${importPath}.js"`;
|
||||
},
|
||||
);
|
||||
|
||||
fixedContent = fixedContent.replace(
|
||||
/from\s+'(\.\.?\/[^']+)'/g,
|
||||
(match, importPath) => {
|
||||
if (importPath.endsWith(".js")) {
|
||||
return match;
|
||||
}
|
||||
return `from '${importPath}.js'`;
|
||||
},
|
||||
);
|
||||
|
||||
if (fixedContent !== content) {
|
||||
fs.writeFileSync(fullPath, fixedContent);
|
||||
console.log(`✓ Fixed imports in ${path.relative(destApi, fullPath)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fixImportsInDir(destApi);
|
||||
console.log("✓ ESM imports fixed");
|
||||
console.log("✓ Vercel will auto-detect and compile TypeScript files");
|
||||
|
|
|
|||
Loading…
Reference in a new issue