1
1
import * as path from 'path' ;
2
2
import * as fs from 'fs' ;
3
3
import * as glob from 'glob' ;
4
+ import * as webpack from 'webpack' ;
5
+ const webpackDevMiddleware = require ( 'webpack-dev-middleware' ) ;
4
6
5
7
import { Pattern } from './glob-copy-webpack-plugin' ;
6
8
import { WebpackTestConfig , WebpackTestOptions } from '../models/webpack-test-config' ;
@@ -39,7 +41,7 @@ function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
39
41
}
40
42
}
41
43
42
- const init : any = ( config : any ) => {
44
+ const init : any = ( config : any , emitter : any , customFileHandlers : any ) => {
43
45
const appConfig = getAppFromConfig ( config . angularCli . app ) ;
44
46
const appRoot = path . join ( config . basePath , appConfig . root ) ;
45
47
const testConfig : WebpackTestOptions = Object . assign ( {
@@ -88,6 +90,8 @@ const init: any = (config: any) => {
88
90
const webpackConfig = new WebpackTestConfig ( testConfig , appConfig ) . buildConfig ( ) ;
89
91
const webpackMiddlewareConfig = {
90
92
noInfo : true , // Hide webpack output because its noisy.
93
+ watchOptions : { poll : testConfig . poll } ,
94
+ publicPath : '/_karma_webpack_/' ,
91
95
stats : { // Also prevent chunk and module display output, cleaner look. Only emit errors.
92
96
assets : false ,
93
97
colors : true ,
@@ -97,9 +101,6 @@ const init: any = (config: any) => {
97
101
chunks : false ,
98
102
chunkModules : false
99
103
} ,
100
- watchOptions : {
101
- poll : testConfig . poll
102
- }
103
104
} ;
104
105
105
106
// If Karma is being ran in single run mode, throw errors.
@@ -120,15 +121,54 @@ const init: any = (config: any) => {
120
121
config . customDebugFile = `${ __dirname } /karma-debug.html` ;
121
122
122
123
config . files . forEach ( ( file : any ) => {
123
- if ( file . pattern === `./${ appConfig . root } /${ appConfig . test } ` ) {
124
+ if ( file . pattern === `./${ appConfig . root } /${ appConfig . test } ` ) {
124
125
file . watched = false ;
125
126
file . served = false ;
126
127
file . included = false ;
127
128
}
128
129
} ) ;
130
+
131
+
132
+
133
+ // code from karma-webpack
134
+
135
+ // The webpack tier owns the watch behavior so we want to force it in the config
136
+ webpackConfig . watch = true ;
137
+ webpackConfig . output . path = '/_karma_webpack_/' ;
138
+ webpackConfig . output . publicPath = '/_karma_webpack_/' ;
139
+
140
+ let compiler : any ;
141
+ try {
142
+ compiler = webpack ( webpackConfig ) ;
143
+ } catch ( e ) {
144
+ console . error ( e . stack || e ) ;
145
+ if ( e . details ) {
146
+ console . error ( e . details ) ;
147
+ }
148
+ throw e ;
149
+ }
150
+
151
+ compiler . plugin ( 'done' , ( ) => emitter . refreshFiles ( ) ) ;
152
+
153
+ const middleware = new webpackDevMiddleware ( compiler , webpackMiddlewareConfig ) ;
154
+
155
+ customFileHandlers . push ( {
156
+ urlRegex : / ^ \/ _ k a r m a _ w e b p a c k _ \/ .* / ,
157
+ handler : function handler ( req : any , res : any ) {
158
+ middleware ( req , res , function ( ) {
159
+ res . statusCode = 404 ;
160
+ res . end ( 'Not found' ) ;
161
+ } ) ;
162
+ }
163
+ } ) ;
164
+
165
+ emitter . on ( 'exit' , ( done : any ) => {
166
+ middleware . close ( ) ;
167
+ done ( ) ;
168
+ } ) ;
129
169
} ;
130
170
131
- init . $inject = [ 'config' ] ;
171
+ init . $inject = [ 'config' , 'emitter' , 'customFileHandlers' ] ;
132
172
133
173
// Dummy preprocessor, just to keep karma from showing a warning.
134
174
const preprocessor : any = ( ) => ( content : any , _file : string , done : any ) => done ( null , content ) ;
@@ -138,4 +178,5 @@ preprocessor.$inject = [];
138
178
module . exports = Object . assign ( {
139
179
'framework:@angular/cli' : [ 'factory' , init ] ,
140
180
'preprocessor:@angular/cli' : [ 'factory' , preprocessor ]
141
- } , require ( './karma-webpack-custom' ) , require ( 'karma-sourcemap-loader' ) ) ;
181
+ // }, require('./karma-webpack-custom'), require('karma-sourcemap-loader'));
182
+ } ) ;
0 commit comments