From 7be977c58829104a5d448a1df94b6fbe2fba3d38 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Sun, 16 Nov 2025 04:25:42 +0000 Subject: [PATCH] Fix build-api.js with more robust ESM import fixing cgen-fc3007874e0a4c7da831f44b47542e66 --- build-api.js | 58 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/build-api.js b/build-api.js index ec3a50be..450c1c38 100644 --- a/build-api.js +++ b/build-api.js @@ -50,6 +50,7 @@ try { // Step 3: Fix ESM imports by adding .js extensions to relative imports console.log("Step 3: Fixing ESM module imports..."); let fixedCount = 0; +let importFixedCount = 0; function fixImportsInDir(dir) { const entries = fs.readdirSync(dir, { withFileTypes: true }); @@ -61,34 +62,69 @@ function fixImportsInDir(dir) { fixImportsInDir(fullPath); } else if (entry.name.endsWith(".js")) { let content = fs.readFileSync(fullPath, "utf-8"); + let originalContent = content; - // Fix relative imports for ESM: "../../_supabase" -> "../../_supabase.js" - let fixedContent = content.replace( - /from\s+"(\.\.?\/[^"]+)"/g, + // Pattern 1: import ... from "..." + content = content.replace( + /from\s+["'](\.[^"']+)["']/g, (match, importPath) => { - if (importPath.endsWith(".js")) return match; + if ( + importPath.endsWith(".js") || + importPath.endsWith(".json") || + importPath.includes("node_modules") + ) { + return match; + } + importFixedCount++; return `from "${importPath}.js"`; }, ); - fixedContent = fixedContent.replace( - /from\s+'(\.\.?\/[^']+)'/g, + // Pattern 2: import("...") + content = content.replace( + /import\s*\(\s*["'](\.[^"']+)["']\s*\)/g, (match, importPath) => { - if (importPath.endsWith(".js")) return match; - return `from '${importPath}.js'`; + if ( + importPath.endsWith(".js") || + importPath.endsWith(".json") || + importPath.includes("node_modules") + ) { + return match; + } + importFixedCount++; + return `import("${importPath}.js")`; }, ); - if (fixedContent !== content) { - fs.writeFileSync(fullPath, fixedContent); + // Pattern 3: require("...") + content = content.replace( + /require\s*\(\s*["'](\.[^"']+)["']\s*\)/g, + (match, importPath) => { + if ( + importPath.endsWith(".js") || + importPath.endsWith(".json") || + importPath.includes("node_modules") + ) { + return match; + } + importFixedCount++; + return `require("${importPath}.js")`; + }, + ); + + if (content !== originalContent) { + fs.writeFileSync(fullPath, content); fixedCount++; + console.log(` Fixed: ${path.relative(destApi, fullPath)}`); } } } } fixImportsInDir(destApi); -console.log(`✓ Fixed ESM imports in ${fixedCount} files`); +console.log( + `✓ Fixed ESM imports in ${fixedCount} files (${importFixedCount} total imports)`, +); // Step 4: Remove TypeScript files so Vercel doesn't recompile them console.log("Step 4: Removing TypeScript source files...");