9
9
// TODO: cleanup this file, it's copied as is from Angular CLI.
10
10
11
11
import * as path from 'path' ;
12
- import * as fs from 'fs' ;
13
12
import * as glob from 'glob' ;
14
13
import * as webpack from 'webpack' ;
15
14
const webpackDevMiddleware = require ( 'webpack-dev-middleware' ) ;
16
15
17
- import { AssetPattern } from '../../browser/schema' ;
18
16
import { 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' ;
19
21
20
22
/**
21
23
* Enumerate needed (but not require/imported) dependencies from this file
@@ -62,7 +64,7 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
62
64
)
63
65
}
64
66
const options = config . buildWebpack . options ;
65
- const projectRoot = config . buildWebpack . projectRoot as string ;
67
+ const logger : logging . Logger = config . buildWebpack . logger || createConsoleLogger ( ) ;
66
68
successCb = config . buildWebpack . successCb ;
67
69
failureCb = config . buildWebpack . failureCb ;
68
70
@@ -93,7 +95,9 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
93
95
// Add webpack config.
94
96
const webpackConfig = config . buildWebpack . webpackConfig ;
95
97
const webpackMiddlewareConfig = {
96
- logLevel : 'error' , // Hide webpack output because its noisy.
98
+ // Hide webpack output because its noisy.
99
+ logLevel : 'error' ,
100
+ stats : false ,
97
101
watchOptions : { poll : options . poll } ,
98
102
publicPath : '/_karma_webpack_/' ,
99
103
} ;
@@ -147,9 +151,9 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
147
151
try {
148
152
compiler = webpack ( webpackConfig ) ;
149
153
} catch ( e ) {
150
- console . error ( e . stack || e ) ;
154
+ logger . error ( e . stack || e )
151
155
if ( e . details ) {
152
- console . error ( e . details ) ;
156
+ logger . error ( e . details )
153
157
}
154
158
throw e ;
155
159
}
@@ -175,9 +179,17 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
175
179
}
176
180
177
181
let lastCompilationHash : string | undefined ;
182
+ const statsConfig = getWebpackStatsConfig ( ) ;
178
183
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.
181
193
lastCompilationHash = stats . hash ;
182
194
emitter . refreshFiles ( ) ;
183
195
}
0 commit comments