1+ const path = require ( 'node:path' ) ;
2+ const fs = require ( 'node:fs/promises' ) ;
3+
4+ // Copy /myPath.html to /myPath/index.html
5+ // This ensures both URL patterns work: /myPath and /myPath/
6+ async function generateIndexHtmlFiles ( outDir ) {
7+ console . log ( 'Post-build: Creating index.html files from hoisted pages...' ) ;
8+
9+ // Walk through all directories recursively
10+ async function * walkDirs ( dir ) {
11+ const dirents = await fs . readdir ( dir , { withFileTypes : true } ) ;
12+ for ( const dirent of dirents ) {
13+ const res = path . resolve ( dir , dirent . name ) ;
14+ if ( dirent . isDirectory ( ) ) {
15+ yield res ;
16+ yield * walkDirs ( res ) ;
17+ }
18+ }
19+ }
20+
21+ const processedFiles = [ ] ;
22+
23+ for await ( const dirPath of walkDirs ( outDir ) ) {
24+ // Check if there's already an index.html in this directory
25+ const indexPath = path . join ( dirPath , 'index.html' ) ;
26+ try {
27+ await fs . stat ( indexPath ) ;
28+ // index.html exists, skip this directory
29+ continue ;
30+ } catch {
31+ // No index.html, continue checking
32+ }
33+
34+ // Check if there's a sibling HTML file with the same name as the directory
35+ const dirName = path . basename ( dirPath ) ;
36+ const siblingHtmlPath = path . join ( path . dirname ( dirPath ) , `${ dirName } .html` ) ;
37+
38+ try {
39+ await fs . stat ( siblingHtmlPath ) ;
40+ // Sibling HTML file exists, copy it as index.html
41+ await fs . copyFile ( siblingHtmlPath , indexPath ) ;
42+ processedFiles . push ( `${ dirName } .html → ${ dirName } /index.html` ) ;
43+ } catch {
44+ // No sibling HTML file, skip
45+ }
46+ }
47+
48+ if ( processedFiles . length > 0 ) {
49+ console . log ( `Post-build: Created ${ processedFiles . length } index.html files` ) ;
50+ // Uncomment to see details:
51+ // processedFiles.forEach(f => console.log(` - ${f}`));
52+ } else {
53+ console . log ( 'Post-build: No index.html files needed' ) ;
54+ }
55+ }
56+
57+ // Run the post-processing
58+ const buildDir = path . join ( __dirname , '..' , 'build' ) ;
59+ generateIndexHtmlFiles ( buildDir ) . catch ( console . error ) ;
0 commit comments