99// TODO: cleanup this file, it's copied as is from Angular CLI.
1010
1111import * as path from 'path' ;
12- import * as fs from 'fs' ;
1312import * as glob from 'glob' ;
1413import * as webpack from 'webpack' ;
1514const webpackDevMiddleware = require ( 'webpack-dev-middleware' ) ;
1615
17- import { AssetPattern } from '../../browser/schema' ;
1816import { KarmaWebpackFailureCb } from './karma-webpack-failure-cb' ;
17+ import { statsErrorsToString } from '../utilities/stats' ;
18+ import { getWebpackStatsConfig } from '../models/webpack-configs/stats' ;
19+ import { createConsoleLogger } from '@angular-devkit/core/node' ;
20+ import { logging } from '@angular-devkit/core' ;
1921
2022/**
2123 * Enumerate needed (but not require/imported) dependencies from this file
@@ -62,7 +64,7 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
6264 )
6365 }
6466 const options = config . buildWebpack . options ;
65- const projectRoot = config . buildWebpack . projectRoot as string ;
67+ const logger : logging . Logger = config . buildWebpack . logger || createConsoleLogger ( ) ;
6668 successCb = config . buildWebpack . successCb ;
6769 failureCb = config . buildWebpack . failureCb ;
6870
@@ -93,7 +95,9 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
9395 // Add webpack config.
9496 const webpackConfig = config . buildWebpack . webpackConfig ;
9597 const webpackMiddlewareConfig = {
96- logLevel : 'error' , // Hide webpack output because its noisy.
98+ // Hide webpack output because its noisy.
99+ logLevel : 'error' ,
100+ stats : false ,
97101 watchOptions : { poll : options . poll } ,
98102 publicPath : '/_karma_webpack_/' ,
99103 } ;
@@ -147,9 +151,9 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
147151 try {
148152 compiler = webpack ( webpackConfig ) ;
149153 } catch ( e ) {
150- console . error ( e . stack || e ) ;
154+ logger . error ( e . stack || e )
151155 if ( e . details ) {
152- console . error ( e . details ) ;
156+ logger . error ( e . details )
153157 }
154158 throw e ;
155159 }
@@ -175,9 +179,17 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
175179 }
176180
177181 let lastCompilationHash : string | undefined ;
182+ const statsConfig = getWebpackStatsConfig ( ) ;
178183 compiler . hooks . done . tap ( 'karma' , ( stats : any ) => {
179- // Refresh karma only when there are no webpack errors, and if the compilation changed.
180- if ( stats . compilation . errors . length === 0 && stats . hash != lastCompilationHash ) {
184+ if ( stats . compilation . errors . length > 0 ) {
185+ const json = stats . toJson ( config . stats ) ;
186+ // Print compilation errors.
187+ logger . error ( statsErrorsToString ( json , statsConfig ) ) ;
188+ lastCompilationHash = undefined ;
189+ // Emit a failure build event if there are compilation errors.
190+ failureCb && failureCb ( ) ;
191+ } else if ( stats . hash != lastCompilationHash ) {
192+ // Refresh karma only when there are no webpack errors, and if the compilation changed.
181193 lastCompilationHash = stats . hash ;
182194 emitter . refreshFiles ( ) ;
183195 }
0 commit comments