7
7
*/
8
8
// tslint:disable
9
9
// TODO: cleanup this file, it's copied as is from Angular CLI.
10
-
10
+ import * as http from 'http' ;
11
11
import * as path from 'path' ;
12
12
import * as glob from 'glob' ;
13
13
import * as webpack from 'webpack' ;
@@ -29,6 +29,8 @@ import { normalizeSourceMaps } from '../../utils/index';
29
29
* require('karma-source-map-support')
30
30
*/
31
31
32
+ const KARMA_APPLICATION_PATH = '_karma_webpack_' ;
33
+
32
34
33
35
let blocked : any [ ] = [ ] ;
34
36
let isBlocked = false ;
@@ -121,7 +123,7 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
121
123
// Hide webpack output because its noisy.
122
124
logLevel : 'error' ,
123
125
stats : false ,
124
- publicPath : '/_karma_webpack_/' ,
126
+ publicPath : `/ ${ KARMA_APPLICATION_PATH } /` ,
125
127
} ;
126
128
127
129
const compilationErrorCb = ( error : string | undefined , errors : string [ ] ) => {
@@ -165,8 +167,8 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
165
167
} ) ;
166
168
}
167
169
// Files need to be served from a custom path for Karma.
168
- webpackConfig . output . path = '/_karma_webpack_/' ;
169
- webpackConfig . output . publicPath = '/_karma_webpack_/' ;
170
+ webpackConfig . output . path = `/ ${ KARMA_APPLICATION_PATH } /` ;
171
+ webpackConfig . output . publicPath = `/ ${ KARMA_APPLICATION_PATH } /` ;
170
172
171
173
let compiler : any ;
172
174
try {
@@ -221,18 +223,18 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
221
223
222
224
// Forward requests to webpack server.
223
225
customFileHandlers . push ( {
224
- urlRegex : / ^ \/ _ k a r m a _ w e b p a c k _ \ / .* / ,
226
+ urlRegex : new RegExp ( `\\/ ${ KARMA_APPLICATION_PATH } \\ /.*` ) ,
225
227
handler : function handler ( req : any , res : any ) {
226
228
webpackMiddleware ( req , res , function ( ) {
227
229
// Ensure script and style bundles are served.
228
230
// They are mentioned in the custom karma context page and we don't want them to 404.
229
231
const alwaysServe = [
230
- '/_karma_webpack_ /runtime.js' ,
231
- '/_karma_webpack_ /polyfills.js' ,
232
- '/_karma_webpack_ /polyfills-es5.js' ,
233
- '/_karma_webpack_ /scripts.js' ,
234
- '/_karma_webpack_ /styles.js' ,
235
- '/_karma_webpack_ /vendor.js' ,
232
+ `/ ${ KARMA_APPLICATION_PATH } /runtime.js` ,
233
+ `/ ${ KARMA_APPLICATION_PATH } /polyfills.js` ,
234
+ `/ ${ KARMA_APPLICATION_PATH } /polyfills-es5.js` ,
235
+ `/ ${ KARMA_APPLICATION_PATH } /scripts.js` ,
236
+ `/ ${ KARMA_APPLICATION_PATH } /styles.js` ,
237
+ `/ ${ KARMA_APPLICATION_PATH } /vendor.js` ,
236
238
] ;
237
239
if ( alwaysServe . indexOf ( req . url ) != - 1 ) {
238
240
res . statusCode = 200 ;
@@ -325,11 +327,10 @@ sourceMapReporter.$inject = ['baseReporterDecorator', 'config'];
325
327
326
328
// When a request is not found in the karma server, try looking for it from the webpack server root.
327
329
function fallbackMiddleware ( ) {
328
- return function ( req : any , res : any , next : ( ) => void ) {
330
+ return function ( request : http . IncomingMessage , response : http . ServerResponse , next : ( ) => void ) {
329
331
if ( webpackMiddleware ) {
330
- const webpackUrl = '/_karma_webpack_' + req . url ;
331
- const webpackReq = { ...req , url : webpackUrl }
332
- webpackMiddleware ( webpackReq , res , next ) ;
332
+ request . url = '/' + KARMA_APPLICATION_PATH + request . url ;
333
+ webpackMiddleware ( request , response , next ) ;
333
334
} else {
334
335
next ( ) ;
335
336
}
0 commit comments