@@ -8,6 +8,14 @@ import { Pattern } from './glob-copy-webpack-plugin';
88import { WebpackTestConfig , WebpackTestOptions } from '../models/webpack-test-config' ;
99import { KarmaWebpackThrowError } from './karma-webpack-throw-error' ;
1010
11+ /**
12+ * Enumerate needed (but not require/imported) dependencies from this file
13+ * to let the dependency validator know they are used.
14+ *
15+ * require('karma-source-map-support')
16+ */
17+
18+
1119const getAppFromConfig = require ( '../utilities/app-utils' ) . getAppFromConfig ;
1220
1321let blocked : any [ ] = [ ] ;
@@ -54,6 +62,22 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
5462 progress : true ,
5563 } , config . angularCli ) ;
5664
65+ if ( testConfig . sourcemaps ) {
66+ // Add a reporter that fixes sourcemap urls.
67+ config . reporters . unshift ( '@angular/cli' ) ;
68+
69+ // Code taken from https://github.com/tschaub/karma-source-map-support.
70+ // We can't use it directly because we need to add it conditionally in this file, and karma
71+ // frameworks cannot be added dynamically.
72+ const smsPath = path . dirname ( require . resolve ( 'source-map-support' ) ) ;
73+ const ksmsPath = path . dirname ( require . resolve ( 'karma-source-map-support' ) ) ;
74+
75+ addKarmaFiles ( config . files , [
76+ { pattern : path . join ( smsPath , 'browser-source-map-support.js' ) , watched : false } ,
77+ { pattern : path . join ( ksmsPath , 'client.js' ) , watched : false }
78+ ] , true ) ;
79+ }
80+
5781 // Add assets. This logic is mimics the one present in GlobCopyWebpackPlugin.
5882 if ( appConfig . assets ) {
5983 config . proxies = config . proxies || { } ;
@@ -227,9 +251,25 @@ function requestBlocker() {
227251 } ;
228252}
229253
230- // Also export karma-webpack and karma-sourcemap-loader.
254+ // Strip the server address and webpack scheme (webpack://) from error log.
255+ const initSourcemapReporter : any = function ( baseReporterDecorator : any ) {
256+ baseReporterDecorator ( this ) ;
257+ const urlRegexp = / \( h t t p : \/ \/ l o c a l h o s t : \d + \/ _ k a r m a _ w e b p a c k _ \/ w e b p a c k : \/ / gi;
258+
259+ this . onSpecComplete = function ( _browser : any , result : any ) {
260+ if ( ! result . success && result . log . length > 0 ) {
261+ result . log . forEach ( ( log : string , idx : number ) => {
262+ result . log [ idx ] = log . replace ( urlRegexp , '' ) ;
263+ } ) ;
264+ }
265+ } ;
266+ } ;
267+
268+ initSourcemapReporter . $inject = [ 'baseReporterDecorator' ] ;
269+
231270module . exports = Object . assign ( {
232271 'framework:@angular/cli' : [ 'factory' , init ] ,
233272 'preprocessor:@angular/cli' : [ 'factory' , preprocessor ] ,
273+ 'reporter:@angular/cli' : [ 'type' , initSourcemapReporter ] ,
234274 'middleware:angularCliBlocker' : [ 'factory' , requestBlocker ]
235275} ) ;
0 commit comments