vkashti / scripts / vercel-build.js
vercel-build.js
Raw
#!/usr/bin/env node

/**
 * This is a simplified build script for Vercel deployments
 * to bypass potential stack overflow issues in the regular build
 */

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

console.log('๐Ÿš€ Starting simplified Vercel build...');

// Apply critical fixes first
try {
  require('./fix-vercel-build.js');
  console.log('โœ… Applied critical build fixes');
} catch (err) {
  console.error('โš ๏ธ Warning: Failed to apply build fixes:', err.message);
  // Continue with the build even if fixes fail
}

// Create a temporary package.json to force simpler settings
function simplifyPackageJson() {
  const packageJsonPath = path.join(process.cwd(), 'package.json');
  
  if (!fs.existsSync(packageJsonPath)) {
    console.error('โŒ package.json not found!');
    return false;
  }
  
  // Backup the original package.json
  const backupPath = `${packageJsonPath}.original`;
  if (!fs.existsSync(backupPath)) {
    fs.copyFileSync(packageJsonPath, backupPath);
    console.log('๐Ÿ“ฆ Backed up original package.json');
  }
  
  try {
    // Read and parse the package.json
    const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
    
    // Keep a simplified version of the package
    const simplifiedPackage = {
      ...packageJson,
      overrides: {
        ...packageJson.overrides,
        'next': packageJson.dependencies.next, // Ensure exact version match
      },
      resolutions: {
        ...packageJson.resolutions,
      }
    };
    
    // Write simplified package.json
    fs.writeFileSync(packageJsonPath, JSON.stringify(simplifiedPackage, null, 2));
    console.log('๐Ÿ“ฆ Simplified package.json for build');
    return true;
  } catch (err) {
    console.error('โš ๏ธ Warning: Failed to simplify package.json:', err.message);
    return false;
  }
}

// Run the Next.js build with safer options
function runSafeBuild() {
  console.log('๐Ÿ—๏ธ Running simplified Next.js build...');
  
  try {
    // Set safer Node options - only memory limit
    process.env.NODE_OPTIONS = `--max-old-space-size=3072 ${process.env.NODE_OPTIONS || ''}`;
    
    // Run the actual build
    execSync('next build', { 
      stdio: 'inherit',
      env: {
        ...process.env,
        SKIP_POSTINSTALL: 'true',
        NEXT_DISABLE_SOURCEMAPS: 'true',
        NEXT_TELEMETRY_DISABLED: '1',
        NEXT_DISABLE_OPTIMIZATION: '1'
      }
    });
    
    return true;
  } catch (err) {
    console.error('โŒ Build failed:', err.message);
    return false;
  }
}

// Main build execution
(async function main() {
  try {
    // Apply all fixes and run build
    simplifyPackageJson();
    
    if (runSafeBuild()) {
      console.log('โœ… Simplified build completed successfully!');
      process.exit(0);
    } else {
      console.error('โŒ Simplified build failed!');
      process.exit(1);
    }
  } catch (err) {
    console.error('โŒ Critical build error:', err);
    process.exit(1);
  }
})();