@@ -15,13 +15,13 @@ import { JsonObject, experimental, join, normalize, resolve, schema } from '@ang
1515import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
1616import * as fs from 'fs' ;
1717import * as path from 'path' ;
18+ import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig' ;
1819import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker' ;
1920import { BrowserBuilderOutput } from '../browser' ;
2021import { Schema as BrowserBuilderSchema } from '../browser/schema' ;
2122import { ServerBuilderOutput } from '../server' ;
2223import { Schema as BuildWebpackAppShellSchema } from './schema' ;
2324
24-
2525async function _renderUniversal (
2626 options : BuildWebpackAppShellSchema ,
2727 context : BuilderContext ,
@@ -31,40 +31,48 @@ async function _renderUniversal(
3131 const browserIndexOutputPath = path . join ( browserResult . outputPath || '' , 'index.html' ) ;
3232 const indexHtml = fs . readFileSync ( browserIndexOutputPath , 'utf8' ) ;
3333 const serverBundlePath = await _getServerModuleBundlePath ( options , context , serverResult ) ;
34-
3534 const root = context . workspaceRoot ;
3635
36+ // Get browser target options.
37+ const browserTarget = targetFromTargetString ( options . browserTarget ) ;
38+ const rawBrowserOptions = await context . getTargetOptions ( browserTarget ) ;
39+ const browserBuilderName = await context . getBuilderNameForTarget ( browserTarget ) ;
40+ const browserOptions = await context . validateOptions < JsonObject & BrowserBuilderSchema > (
41+ rawBrowserOptions ,
42+ browserBuilderName ,
43+ ) ;
44+
45+ // Determine if browser app was compiled using Ivy.
46+ const { options : compilerOptions } = readTsconfig ( browserOptions . tsConfig , context . workspaceRoot ) ;
47+ const ivy = compilerOptions . enableIvy ;
48+
3749 // Initialize zone.js
3850 const zonePackage = require . resolve ( 'zone.js' , { paths : [ root ] } ) ;
3951 await import ( zonePackage ) ;
4052
4153 // Load platform server module renderer
4254 const platformServerPackage = require . resolve ( '@angular/platform-server' , { paths : [ root ] } ) ;
43- const renderModuleFactory = await import ( platformServerPackage )
55+ const renderOpts = {
56+ document : indexHtml ,
57+ url : options . route ,
58+ } ;
59+
60+ // Render app to HTML using Ivy or VE
61+ const html = await import ( platformServerPackage )
4462 // tslint:disable-next-line:no-implicit-dependencies
45- . then ( ( m : typeof import ( '@angular/platform-server' ) ) => m . renderModuleFactory ) ;
63+ . then ( ( m : typeof import ( '@angular/platform-server' ) ) =>
64+ ivy
65+ ? m . renderModule ( require ( serverBundlePath ) . AppServerModule , renderOpts )
66+ : m . renderModuleFactory ( require ( serverBundlePath ) . AppServerModuleNgFactory , renderOpts ) ,
67+ ) ;
4668
47- const AppServerModuleNgFactory = require ( serverBundlePath ) . AppServerModuleNgFactory ;
69+ // Overwrite the client index file.
4870 const outputIndexPath = options . outputIndexPath
4971 ? path . join ( root , options . outputIndexPath )
5072 : browserIndexOutputPath ;
5173
52- // Render to HTML and overwrite the client index file.
53- const html = await renderModuleFactory ( AppServerModuleNgFactory , {
54- document : indexHtml ,
55- url : options . route ,
56- } ) ;
57-
5874 fs . writeFileSync ( outputIndexPath , html ) ;
5975
60- const browserTarget = targetFromTargetString ( options . browserTarget ) ;
61- const rawBrowserOptions = await context . getTargetOptions ( browserTarget ) ;
62- const browserBuilderName = await context . getBuilderNameForTarget ( browserTarget ) ;
63- const browserOptions = await context . validateOptions < JsonObject & BrowserBuilderSchema > (
64- rawBrowserOptions ,
65- browserBuilderName ,
66- ) ;
67-
6876 if ( browserOptions . serviceWorker ) {
6977 const host = new NodeJsSyncHost ( ) ;
7078 // Create workspace.
@@ -81,10 +89,7 @@ async function _renderUniversal(
8189 if ( ! projectName ) {
8290 throw new Error ( 'Must either have a target from the context or a default project.' ) ;
8391 }
84- const projectRoot = resolve (
85- workspace . root ,
86- normalize ( workspace . getProject ( projectName ) . root ) ,
87- ) ;
92+ const projectRoot = resolve ( workspace . root , normalize ( workspace . getProject ( projectName ) . root ) ) ;
8893
8994 await augmentAppWithServiceWorker (
9095 host ,
@@ -99,7 +104,6 @@ async function _renderUniversal(
99104 return browserResult ;
100105}
101106
102-
103107async function _getServerModuleBundlePath (
104108 options : BuildWebpackAppShellSchema ,
105109 context : BuilderContext ,
@@ -121,7 +125,6 @@ async function _getServerModuleBundlePath(
121125 }
122126}
123127
124-
125128async function _appShellBuilder (
126129 options : JsonObject & BuildWebpackAppShellSchema ,
127130 context : BuilderContext ,
@@ -139,7 +142,7 @@ async function _appShellBuilder(
139142
140143 try {
141144 const [ browserResult , serverResult ] = await Promise . all ( [
142- browserTargetRun . result as { } as BrowserBuilderOutput ,
145+ ( browserTargetRun . result as { } ) as BrowserBuilderOutput ,
143146 serverTargetRun . result ,
144147 ] ) ;
145148
@@ -154,12 +157,8 @@ async function _appShellBuilder(
154157 return { success : false , error : err . message } ;
155158 } finally {
156159 // Just be good citizens and stop those jobs.
157- await Promise . all ( [
158- browserTargetRun . stop ( ) ,
159- serverTargetRun . stop ( ) ,
160- ] ) ;
160+ await Promise . all ( [ browserTargetRun . stop ( ) , serverTargetRun . stop ( ) ] ) ;
161161 }
162162}
163163
164-
165164export default createBuilder ( _appShellBuilder ) ;
0 commit comments