1- // Custom build script to handle webpack issues
1+ // Custom build script to handle memory issues during build
22const { execSync } = require ( 'child_process' ) ;
33const fs = require ( 'fs' ) ;
44const path = require ( 'path' ) ;
55
66console . log ( 'Starting custom build process...' ) ;
77
8- // Ensure the .next directory is clean
9- try {
10- if ( fs . existsSync ( '.next' ) ) {
11- console . log ( 'Cleaning .next directory...' ) ;
12- try {
13- fs . rmSync ( '.next' , { recursive : true , force : true } ) ;
14- } catch ( e ) {
15- console . warn ( 'Could not remove .next directory, continuing anyway:' , e . message ) ;
8+ // Clean directories
9+ function cleanDirectories ( ) {
10+ console . log ( 'Cleaning build directories...' ) ;
11+
12+ const dirsToClean = [ '.next' , 'out' ] ;
13+
14+ for ( const dir of dirsToClean ) {
15+ if ( fs . existsSync ( dir ) ) {
16+ try {
17+ fs . rmSync ( dir , { recursive : true , force : true } ) ;
18+ console . log ( `Cleaned ${ dir } directory` ) ;
19+ } catch ( e ) {
20+ console . warn ( `Could not remove ${ dir } directory, continuing anyway:` , e . message ) ;
21+ }
1622 }
1723 }
24+ }
1825
19- if ( fs . existsSync ( 'out' ) ) {
20- console . log ( 'Cleaning out directory...' ) ;
21- try {
22- fs . rmSync ( 'out' , { recursive : true , force : true } ) ;
23- } catch ( e ) {
24- console . warn ( 'Could not remove out directory, continuing anyway:' , e . message ) ;
25- }
26+ // Set up environment
27+ function setupEnvironment ( ) {
28+ console . log ( 'Setting up build environment...' ) ;
29+
30+ // Set environment variables
31+ process . env . NODE_ENV = 'production' ;
32+ process . env . NEXT_TELEMETRY_DISABLED = '1' ;
33+
34+ // Create temporary .babelrc file for build optimization
35+ fs . writeFileSync ( '.babelrc' , JSON . stringify ( {
36+ presets : [ 'next/babel' ] ,
37+ plugins : [ ]
38+ } , null , 2 ) ) ;
39+
40+ if ( process . env . GITHUB_PAGES ) {
41+ console . log ( 'Building for GitHub Pages...' ) ;
2642 }
27- } catch ( err ) {
28- console . warn ( 'Warning: Could not clean directories:' , err . message ) ;
2943}
3044
31- // Create temporary .babelrc file for build optimization
32- console . log ( 'Creating temporary build files...' ) ;
33- fs . writeFileSync ( '.babelrc' , JSON . stringify ( {
34- presets : [ 'next/babel' ] ,
35- plugins : [ ]
36- } , null , 2 ) ) ;
45+ // Execute command with proper error handling
46+ function executeCommand ( command , options = { } ) {
47+ console . log ( `Executing: ${ command } ` ) ;
48+ try {
49+ execSync ( command , {
50+ stdio : 'inherit' ,
51+ env : process . env ,
52+ maxBuffer : 1024 * 1024 * 10 , // 10MB buffer
53+ ...options
54+ } ) ;
55+ return true ;
56+ } catch ( error ) {
57+ console . error ( `Command failed: ${ command } ` ) ;
58+ console . error ( error . message ) ;
59+ return false ;
60+ }
61+ }
62+
63+ // Main build function
64+ async function build ( ) {
65+ try {
66+ // Step 1: Clean up
67+ cleanDirectories ( ) ;
68+
69+ // Step 2: Setup environment
70+ setupEnvironment ( ) ;
3771
38- // Set environment variables
39- process . env . NODE_ENV = 'production' ;
40- process . env . NEXT_TELEMETRY_DISABLED = '1' ;
72+ // Step 3: Try the simplest build approach first
73+ console . log ( 'Attempting build with minimal configuration...' ) ;
74+ const success = executeCommand ( 'npx next build' ) ;
4175
42- // Memory optimization flags for Next.js
43- process . env . NEXT_MEMORY_OPTIMIZATION = 'true' ;
44- process . env . NEXT_MINIMIZE_USAGE = 'true' ;
76+ if ( success ) {
77+ console . log ( 'Build completed successfully!' ) ;
78+ } else {
79+ // Step 4: If that fails, try with explicit memory limits
80+ console . log ( 'First build attempt failed, trying with explicit memory limits...' ) ;
81+ const fallbackSuccess = executeCommand ( 'npx cross-env NODE_OPTIONS="--max-old-space-size=4096" next build' ) ;
4582
46- if ( process . env . GITHUB_PAGES ) {
47- console . log ( 'Building for GitHub Pages...' ) ;
83+ if ( fallbackSuccess ) {
84+ console . log ( 'Build with explicit memory limits completed successfully!' ) ;
85+ } else {
86+ // Step 5: If that also fails, try a more extreme approach
87+ console . log ( 'Second build attempt failed, trying with extreme memory optimization...' ) ;
88+
89+ // Create a temporary next.config.js with minimal settings
90+ const originalConfig = fs . existsSync ( 'next.config.mjs' ) ?
91+ fs . readFileSync ( 'next.config.mjs' , 'utf8' ) : '' ;
92+
93+ fs . writeFileSync ( 'next.config.mjs.backup' , originalConfig ) ;
94+
95+ const minimalConfig = `
96+ /** @type {import('next').NextConfig} */
97+ const nextConfig = {
98+ output: "export",
99+ images: { unoptimized: true },
100+ distDir: '.next',
101+ typescript: { ignoreBuildErrors: true },
102+ eslint: { ignoreDuringBuilds: true },
103+ basePath: process.env.GITHUB_PAGES ? "/lucasbecker-dev" : "",
104+ }
105+
106+ export default nextConfig
107+ ` ;
108+
109+ fs . writeFileSync ( 'next.config.mjs' , minimalConfig ) ;
110+
111+ const extremeSuccess = executeCommand ( 'npx next build' ) ;
112+
113+ // Restore original config
114+ fs . writeFileSync ( 'next.config.mjs' , originalConfig ) ;
115+ fs . unlinkSync ( 'next.config.mjs.backup' ) ;
116+
117+ if ( extremeSuccess ) {
118+ console . log ( 'Build with minimal configuration completed successfully!' ) ;
119+ } else {
120+ console . error ( 'All build attempts failed.' ) ;
121+ process . exit ( 1 ) ;
122+ }
123+ }
124+ }
125+ } catch ( error ) {
126+ console . error ( 'Build process failed:' , error . message ) ;
127+ process . exit ( 1 ) ;
128+ } finally {
129+ // Clean up temporary files
130+ console . log ( 'Cleaning up temporary files...' ) ;
131+ if ( fs . existsSync ( '.babelrc' ) ) {
132+ fs . unlinkSync ( '.babelrc' ) ;
133+ }
134+ }
48135}
49136
50- // Run the build command with error handling
51- try {
52- console . log ( 'Running Next.js build...' ) ;
53- // Note: NODE_OPTIONS is set in the GitHub workflow file
54- execSync ( 'npx next build' , {
55- stdio : 'inherit' ,
56- env : process . env ,
57- maxBuffer : 1024 * 1024 * 10 // Increase buffer size to 10MB
58- } ) ;
59- console . log ( 'Build completed successfully!' ) ;
60- } catch ( error ) {
61- console . error ( 'Build failed:' , error . message ) ;
137+ // Run the build
138+ build ( ) . catch ( err => {
139+ console . error ( 'Unhandled error during build:' , err ) ;
62140 process . exit ( 1 ) ;
63- } finally {
64- // Clean up temporary files
65- console . log ( 'Cleaning up temporary files...' ) ;
66- if ( fs . existsSync ( '.babelrc' ) ) {
67- fs . unlinkSync ( '.babelrc' ) ;
68- }
69- }
141+ } ) ;
0 commit comments