@@ -15,13 +15,13 @@ import { JsonObject, experimental, join, normalize, resolve, schema } from '@ang
15
15
import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
16
16
import * as fs from 'fs' ;
17
17
import * as path from 'path' ;
18
+ import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig' ;
18
19
import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker' ;
19
20
import { BrowserBuilderOutput } from '../browser' ;
20
21
import { Schema as BrowserBuilderSchema } from '../browser/schema' ;
21
22
import { ServerBuilderOutput } from '../server' ;
22
23
import { Schema as BuildWebpackAppShellSchema } from './schema' ;
23
24
24
-
25
25
async function _renderUniversal (
26
26
options : BuildWebpackAppShellSchema ,
27
27
context : BuilderContext ,
@@ -31,40 +31,48 @@ async function _renderUniversal(
31
31
const browserIndexOutputPath = path . join ( browserResult . outputPath || '' , 'index.html' ) ;
32
32
const indexHtml = fs . readFileSync ( browserIndexOutputPath , 'utf8' ) ;
33
33
const serverBundlePath = await _getServerModuleBundlePath ( options , context , serverResult ) ;
34
-
35
34
const root = context . workspaceRoot ;
36
35
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
+
37
49
// Initialize zone.js
38
50
const zonePackage = require . resolve ( 'zone.js' , { paths : [ root ] } ) ;
39
51
await import ( zonePackage ) ;
40
52
41
53
// Load platform server module renderer
42
54
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 )
44
62
// 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
+ ) ;
46
68
47
- const AppServerModuleNgFactory = require ( serverBundlePath ) . AppServerModuleNgFactory ;
69
+ // Overwrite the client index file.
48
70
const outputIndexPath = options . outputIndexPath
49
71
? path . join ( root , options . outputIndexPath )
50
72
: browserIndexOutputPath ;
51
73
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
-
58
74
fs . writeFileSync ( outputIndexPath , html ) ;
59
75
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
-
68
76
if ( browserOptions . serviceWorker ) {
69
77
const host = new NodeJsSyncHost ( ) ;
70
78
// Create workspace.
@@ -81,10 +89,7 @@ async function _renderUniversal(
81
89
if ( ! projectName ) {
82
90
throw new Error ( 'Must either have a target from the context or a default project.' ) ;
83
91
}
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 ) ) ;
88
93
89
94
await augmentAppWithServiceWorker (
90
95
host ,
@@ -99,7 +104,6 @@ async function _renderUniversal(
99
104
return browserResult ;
100
105
}
101
106
102
-
103
107
async function _getServerModuleBundlePath (
104
108
options : BuildWebpackAppShellSchema ,
105
109
context : BuilderContext ,
@@ -121,7 +125,6 @@ async function _getServerModuleBundlePath(
121
125
}
122
126
}
123
127
124
-
125
128
async function _appShellBuilder (
126
129
options : JsonObject & BuildWebpackAppShellSchema ,
127
130
context : BuilderContext ,
@@ -139,7 +142,7 @@ async function _appShellBuilder(
139
142
140
143
try {
141
144
const [ browserResult , serverResult ] = await Promise . all ( [
142
- browserTargetRun . result as { } as BrowserBuilderOutput ,
145
+ ( browserTargetRun . result as { } ) as BrowserBuilderOutput ,
143
146
serverTargetRun . result ,
144
147
] ) ;
145
148
@@ -154,12 +157,8 @@ async function _appShellBuilder(
154
157
return { success : false , error : err . message } ;
155
158
} finally {
156
159
// 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 ( ) ] ) ;
161
161
}
162
162
}
163
163
164
-
165
164
export default createBuilder ( _appShellBuilder ) ;
0 commit comments