@@ -11,10 +11,12 @@ import {
1111 BuilderConfiguration ,
1212 BuilderContext ,
1313} 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' ;
1515import { Observable , forkJoin , from , merge , of , throwError } from 'rxjs' ;
1616import { concatMap , map , switchMap } from 'rxjs/operators' ;
1717import { 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' ;
1820import { BuildWebpackServerSchema } from '../server/schema' ;
1921import { BuildWebpackAppShellSchema } from './schema' ;
2022
@@ -30,7 +32,9 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
3032 let success = true ;
3133 const subscription = merge (
3234 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 } ) ,
3438 ) . subscribe ( ( event : BuildEvent ) => {
3539 // TODO: once we support a better build event, add support for merging two event streams
3640 // together.
@@ -109,25 +113,30 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
109113 } ) ;
110114 }
111115
112- getBrowserIndexOutputPath ( options : BuildWebpackAppShellSchema ) {
116+ getBrowserBuilderConfig ( options : BuildWebpackAppShellSchema ) {
113117 const architect = this . context . architect ;
114118 const [ project , target , configuration ] = options . browserTarget . split ( ':' ) ;
115- const builderConfig = architect . getBuilderConfiguration < BuildWebpackServerSchema > ( {
119+ const builderConfig = architect . getBuilderConfiguration < BrowserBuilderSchema > ( {
116120 project,
117121 target,
118122 configuration,
119123 } ) ;
120124
121125 return architect . getBuilderDescription ( builderConfig ) . pipe (
122126 concatMap ( description => architect . validateBuilderOptions ( builderConfig , description ) ) ,
123- map ( config => join ( normalize ( config . options . outputPath ) , 'index.html' ) ) ,
124127 ) ;
125128 }
126129
127130 renderUniversal ( options : BuildWebpackAppShellSchema ) : Observable < BuildEvent > {
131+ let browserOptions : BrowserBuilderSchema ;
132+ let projectRoot : Path ;
133+
128134 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' ) ;
131140 const path = join ( this . context . workspace . root , browserIndexOutputPath ) ;
132141
133142 return this . context . host . read ( path ) . pipe (
@@ -151,7 +160,7 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
151160 getSystemPath ( serverBundlePath ) ,
152161 ) . AppServerModuleNgFactory ;
153162 const indexHtml = virtualFs . fileBufferToString ( indexContent ) ;
154- const outputPath = join ( root , options . outputIndexPath || browserIndexOutputPath ) ;
163+ const outputIndexPath = join ( root , options . outputIndexPath || browserIndexOutputPath ) ;
155164
156165 // Render to HTML and overwrite the client index file.
157166 return from (
@@ -161,9 +170,21 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
161170 } )
162171 . then ( ( html : string ) => {
163172 return this . context . host
164- . write ( outputPath , virtualFs . stringToFileBuffer ( html ) )
173+ . write ( outputIndexPath , virtualFs . stringToFileBuffer ( html ) )
165174 . toPromise ( ) ;
166175 } )
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+ } )
167188 . then ( ( ) => ( { success : true } ) ) ,
168189 ) ;
169190 } ) ,
0 commit comments