@@ -11,10 +11,12 @@ import {
11
11
BuilderConfiguration ,
12
12
BuilderContext ,
13
13
} from '@angular-devkit/architect' ;
14
- import { Path , getSystemPath , join , normalize , virtualFs } from '@angular-devkit/core' ;
14
+ import { Path , getSystemPath , join , normalize , resolve , virtualFs } from '@angular-devkit/core' ;
15
15
import { Observable , forkJoin , from , merge , of , throwError } from 'rxjs' ;
16
16
import { concatMap , map , switchMap } from 'rxjs/operators' ;
17
17
import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module' ;
18
+ import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker' ;
19
+ import { BrowserBuilderSchema } from '../browser/schema' ;
18
20
import { BuildWebpackServerSchema } from '../server/schema' ;
19
21
import { BuildWebpackAppShellSchema } from './schema' ;
20
22
@@ -30,7 +32,9 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
30
32
let success = true ;
31
33
const subscription = merge (
32
34
this . build ( options . serverTarget , { } ) ,
33
- this . build ( options . browserTarget , { watch : false } ) ,
35
+ // Never run the browser target in watch mode.
36
+ // If service worker is needed, it will be added in this.renderUniversal();
37
+ this . build ( options . browserTarget , { watch : false , serviceWorker : false } ) ,
34
38
) . subscribe ( ( event : BuildEvent ) => {
35
39
// TODO: once we support a better build event, add support for merging two event streams
36
40
// together.
@@ -109,25 +113,30 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
109
113
} ) ;
110
114
}
111
115
112
- getBrowserIndexOutputPath ( options : BuildWebpackAppShellSchema ) {
116
+ getBrowserBuilderConfig ( options : BuildWebpackAppShellSchema ) {
113
117
const architect = this . context . architect ;
114
118
const [ project , target , configuration ] = options . browserTarget . split ( ':' ) ;
115
- const builderConfig = architect . getBuilderConfiguration < BuildWebpackServerSchema > ( {
119
+ const builderConfig = architect . getBuilderConfiguration < BrowserBuilderSchema > ( {
116
120
project,
117
121
target,
118
122
configuration,
119
123
} ) ;
120
124
121
125
return architect . getBuilderDescription ( builderConfig ) . pipe (
122
126
concatMap ( description => architect . validateBuilderOptions ( builderConfig , description ) ) ,
123
- map ( config => join ( normalize ( config . options . outputPath ) , 'index.html' ) ) ,
124
127
) ;
125
128
}
126
129
127
130
renderUniversal ( options : BuildWebpackAppShellSchema ) : Observable < BuildEvent > {
131
+ let browserOptions : BrowserBuilderSchema ;
132
+ let projectRoot : Path ;
133
+
128
134
return forkJoin (
129
- this . getBrowserIndexOutputPath ( options ) . pipe (
130
- switchMap ( browserIndexOutputPath => {
135
+ this . getBrowserBuilderConfig ( options ) . pipe (
136
+ switchMap ( config => {
137
+ browserOptions = config . options ;
138
+ projectRoot = resolve ( this . context . workspace . root , config . root ) ;
139
+ const browserIndexOutputPath = join ( normalize ( browserOptions . outputPath ) , 'index.html' ) ;
131
140
const path = join ( this . context . workspace . root , browserIndexOutputPath ) ;
132
141
133
142
return this . context . host . read ( path ) . pipe (
@@ -151,7 +160,7 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
151
160
getSystemPath ( serverBundlePath ) ,
152
161
) . AppServerModuleNgFactory ;
153
162
const indexHtml = virtualFs . fileBufferToString ( indexContent ) ;
154
- const outputPath = join ( root , options . outputIndexPath || browserIndexOutputPath ) ;
163
+ const outputIndexPath = join ( root , options . outputIndexPath || browserIndexOutputPath ) ;
155
164
156
165
// Render to HTML and overwrite the client index file.
157
166
return from (
@@ -161,9 +170,21 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
161
170
} )
162
171
. then ( ( html : string ) => {
163
172
return this . context . host
164
- . write ( outputPath , virtualFs . stringToFileBuffer ( html ) )
173
+ . write ( outputIndexPath , virtualFs . stringToFileBuffer ( html ) )
165
174
. toPromise ( ) ;
166
175
} )
176
+ . then ( ( ) => {
177
+ if ( browserOptions . serviceWorker ) {
178
+ return augmentAppWithServiceWorker (
179
+ this . context . host ,
180
+ root ,
181
+ projectRoot ,
182
+ join ( root , browserOptions . outputPath ) ,
183
+ browserOptions . baseHref || '/' ,
184
+ browserOptions . ngswConfigPath ,
185
+ ) ;
186
+ }
187
+ } )
167
188
. then ( ( ) => ( { success : true } ) ) ,
168
189
) ;
169
190
} ) ,
0 commit comments