@@ -11,21 +11,21 @@ import {
1111 BuilderConfiguration ,
1212 BuilderContext ,
1313} from '@angular-devkit/architect' ;
14+ import { LoggingCallback , WebpackBuilder } from '@angular-devkit/build-webpack' ;
1415import { Path , getSystemPath , normalize , resolve , virtualFs } from '@angular-devkit/core' ;
1516import * as fs from 'fs' ;
16- import { Observable , concat , of } from 'rxjs' ;
17+ import { Observable , concat , of , throwError } from 'rxjs' ;
1718import { concatMap , last , tap } from 'rxjs/operators' ;
1819import * as ts from 'typescript' ; // tslint:disable-line:no-implicit-dependencies
19- import * as webpack from 'webpack' ;
2020import { WebpackConfigOptions } from '../angular-cli-files/models/build-options' ;
2121import {
2222 getAotConfig ,
2323 getBrowserConfig ,
2424 getCommonConfig ,
2525 getNonAotConfig ,
26+ getStatsConfig ,
2627 getStylesConfig ,
2728} from '../angular-cli-files/models/webpack-configs' ;
28- import { getWebpackStatsConfig } from '../angular-cli-files/models/webpack-configs/utils' ;
2929import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig' ;
3030import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module' ;
3131import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker' ;
@@ -58,6 +58,7 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
5858 const root = this . context . workspace . root ;
5959 const projectRoot = resolve ( root , builderConfig . root ) ;
6060 const host = new virtualFs . AliasHost ( this . context . host as virtualFs . Host < fs . Stats > ) ;
61+ const webpackBuilder = new WebpackBuilder ( { ...this . context , host } ) ;
6162
6263 return of ( null ) . pipe (
6364 concatMap ( ( ) => options . deleteOutputPath
@@ -68,7 +69,7 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
6869 options . assets , host , root , projectRoot , builderConfig . sourceRoot ) ) ,
6970 // Replace the assets in options with the normalized version.
7071 tap ( ( assetPatternObjects => options . assets = assetPatternObjects ) ) ,
71- concatMap ( ( ) => new Observable ( obs => {
72+ concatMap ( ( ) => {
7273 // Ensure Build Optimizer is only used with AOT.
7374 if ( options . buildOptimizer && ! options . aot ) {
7475 throw new Error ( 'The `--build-optimizer` option cannot be used without `--aot`.' ) ;
@@ -79,80 +80,35 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
7980 webpackConfig = this . buildWebpackConfig ( root , projectRoot , host ,
8081 options as NormalizedBrowserBuilderSchema ) ;
8182 } catch ( e ) {
82- obs . error ( e ) ;
83-
84- return ;
83+ return throwError ( e ) ;
8584 }
86- const webpackCompiler = webpack ( webpackConfig ) ;
87- const statsConfig = getWebpackStatsConfig ( options . verbose ) ;
88-
89- const callback : webpack . compiler . CompilerCallback = ( err , stats ) => {
90- if ( err ) {
91- return obs . error ( err ) ;
92- }
93-
94- const json = stats . toJson ( statsConfig ) ;
95- if ( options . verbose ) {
96- this . context . logger . info ( stats . toString ( statsConfig ) ) ;
97- } else {
98- this . context . logger . info ( statsToString ( json , statsConfig ) ) ;
99- }
100-
101- if ( stats . hasWarnings ( ) ) {
102- this . context . logger . warn ( statsWarningsToString ( json , statsConfig ) ) ;
103- }
104- if ( stats . hasErrors ( ) ) {
105- this . context . logger . error ( statsErrorsToString ( json , statsConfig ) ) ;
106- }
107-
108- if ( options . watch ) {
109- obs . next ( { success : ! stats . hasErrors ( ) } ) ;
110-
111- // Never complete on watch mode.
112- return ;
113- } else {
114- if ( builderConfig . options . serviceWorker ) {
115- augmentAppWithServiceWorker (
116- this . context . host ,
117- root ,
118- projectRoot ,
119- resolve ( root , normalize ( options . outputPath ) ) ,
120- options . baseHref || '/' ,
121- options . ngswConfigPath ,
122- ) . then (
123- ( ) => {
124- obs . next ( { success : ! stats . hasErrors ( ) } ) ;
125- obs . complete ( ) ;
126- } ,
127- ( err : Error ) => {
128- // We error out here because we're not in watch mode anyway (see above).
129- obs . error ( err ) ;
130- } ,
131- ) ;
132- } else {
133- obs . next ( { success : ! stats . hasErrors ( ) } ) ;
134- obs . complete ( ) ;
135- }
136- }
137- } ;
13885
139- try {
140- if ( options . watch ) {
141- const watching = webpackCompiler . watch ( { poll : options . poll } , callback ) ;
142-
143- // Teardown logic. Close the watcher when unsubscribed from.
144- return ( ) => watching . close ( ( ) => { } ) ;
145- } else {
146- webpackCompiler . run ( callback ) ;
147- }
148- } catch ( err ) {
149- if ( err ) {
150- this . context . logger . error (
151- '\nAn error occured during the build:\n' + ( ( err && err . stack ) || err ) ) ;
152- }
153- throw err ;
86+ return webpackBuilder . runWebpack ( webpackConfig , getBrowserLoggingCb ( options . verbose ) ) ;
87+ } ) ,
88+ concatMap ( buildEvent => {
89+ if ( buildEvent . success && ! options . watch && options . serviceWorker ) {
90+ return new Observable ( obs => {
91+ augmentAppWithServiceWorker (
92+ this . context . host ,
93+ root ,
94+ projectRoot ,
95+ resolve ( root , normalize ( options . outputPath ) ) ,
96+ options . baseHref || '/' ,
97+ options . ngswConfigPath ,
98+ ) . then (
99+ ( ) => {
100+ obs . next ( { success : true } ) ;
101+ obs . complete ( ) ;
102+ } ,
103+ ( err : Error ) => {
104+ obs . error ( err ) ;
105+ } ,
106+ ) ;
107+ } ) ;
108+ } else {
109+ return of ( buildEvent ) ;
154110 }
155- } ) ) ,
111+ } ) ,
156112 ) ;
157113 }
158114
@@ -185,6 +141,7 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
185141 getCommonConfig ( wco ) ,
186142 getBrowserConfig ( wco ) ,
187143 getStylesConfig ( wco ) ,
144+ getStatsConfig ( wco ) ,
188145 ] ;
189146
190147 if ( wco . buildOptions . main || wco . buildOptions . polyfills ) {
@@ -213,4 +170,22 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
213170 }
214171}
215172
173+ export const getBrowserLoggingCb = ( verbose : boolean ) : LoggingCallback =>
174+ ( stats , config , logger ) => {
175+ // config.stats contains our own stats settings, added during buildWebpackConfig().
176+ const json = stats . toJson ( config . stats ) ;
177+ if ( verbose ) {
178+ logger . info ( stats . toString ( config . stats ) ) ;
179+ } else {
180+ logger . info ( statsToString ( json , config . stats ) ) ;
181+ }
182+
183+ if ( stats . hasWarnings ( ) ) {
184+ logger . warn ( statsWarningsToString ( json , config . stats ) ) ;
185+ }
186+ if ( stats . hasErrors ( ) ) {
187+ logger . error ( statsErrorsToString ( json , config . stats ) ) ;
188+ }
189+ } ;
190+
216191export default BrowserBuilder ;
0 commit comments