77 */
88
99import { BuilderContext , BuilderOutput , createBuilder , targetFromTargetString } from '@angular-devkit/architect' ;
10+ import { BrowserBuilderOptions } from '@angular-devkit/build-angular' ;
1011import { fork } from 'child_process' ;
1112import * as fs from 'fs' ;
1213import * as path from 'path' ;
1314
1415import { PrerenderBuilderOptions , PrerenderBuilderOutput } from './models' ;
15- import { getRoutes , shardArray } from './utils' ;
16+ import { getIndexOutputFile , getRoutes , shardArray } from './utils' ;
1617
1718type BuildBuilderOutput = BuilderOutput & {
1819 baseOutputPath : string ;
@@ -67,13 +68,15 @@ async function _parallelRenderRoutes(
6768 context : BuilderContext ,
6869 indexHtml : string ,
6970 outputPath : string ,
71+ indexFile : string ,
7072 serverBundlePath : string ,
7173 ) : Promise < void > {
7274 const workerFile = path . join ( __dirname , 'render.js' ) ;
7375 const childProcesses = shardedRoutes . map ( routes =>
7476 new Promise ( ( resolve , reject ) => {
7577 fork ( workerFile , [
7678 indexHtml ,
79+ indexFile ,
7780 serverBundlePath ,
7881 outputPath ,
7982 ...routes ,
@@ -103,12 +106,15 @@ async function _renderUniversal(
103106 context : BuilderContext ,
104107 browserResult : BuildBuilderOutput ,
105108 serverResult : BuildBuilderOutput ,
109+ browserOptions : BrowserBuilderOptions ,
106110 numProcesses ?: number ,
107111) : Promise < BuildBuilderOutput > {
112+ // Users can specify a different base html file e.g. "src/home.html"
113+ const indexFile = getIndexOutputFile ( browserOptions ) ;
108114 // We need to render the routes for each locale from the browser output.
109115 for ( const outputPath of browserResult . outputPaths ) {
110- const browserIndexOutputPath = path . join ( outputPath , 'index.html' ) ;
111- const indexHtml = fs . readFileSync ( browserIndexOutputPath , 'utf8' ) ;
116+ const browserIndexInputPath = path . join ( outputPath , indexFile ) ;
117+ const indexHtml = fs . readFileSync ( browserIndexInputPath , 'utf8' ) ;
112118
113119 const { baseOutputPath = '' } = serverResult ;
114120 const localeDirectory = path . relative ( browserResult . baseOutputPath , outputPath ) ;
@@ -125,6 +131,7 @@ async function _renderUniversal(
125131 context ,
126132 indexHtml ,
127133 outputPath ,
134+ indexFile ,
128135 serverBundlePath ,
129136 ) ;
130137 }
@@ -152,7 +159,18 @@ export async function execute(
152159 return { success, error } as BuilderOutput ;
153160 }
154161
155- return _renderUniversal ( routes , context , browserResult , serverResult , options . numProcesses ) ;
162+ const browserTarget = targetFromTargetString ( options . browserTarget ) ;
163+ const browserOptions =
164+ await context . getTargetOptions ( browserTarget ) as unknown as BrowserBuilderOptions ;
165+
166+ return _renderUniversal (
167+ routes ,
168+ context ,
169+ browserResult ,
170+ serverResult ,
171+ browserOptions ,
172+ options . numProcesses ,
173+ ) ;
156174}
157175
158176export default createBuilder ( execute ) ;
0 commit comments