Skip to content

Commit 2ce5445

Browse files
clydinalan-agius4
authored andcommitted
feat(@angular-devkit/build-angular): support karma version 6.x
This change adds support for using karma 6.x within a project.
1 parent d93f78b commit 2ce5445

File tree

4 files changed

+87
-213
lines changed

4 files changed

+87
-213
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
"jest-worker": "26.6.2",
165165
"jquery": "^3.3.1",
166166
"jsonc-parser": "3.0.0",
167-
"karma": "~5.2.0",
167+
"karma": "~6.0.0",
168168
"karma-chrome-launcher": "~3.1.0",
169169
"karma-coverage": "~2.0.3",
170170
"karma-jasmine": "~4.0.0",

packages/angular_devkit/build_angular/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"peerDependencies": {
8282
"@angular/compiler-cli": "^11.0.0 || ^11.1.0-next",
8383
"@angular/localize": "^11.0.0 || ^11.1.0-next",
84-
"karma": "^5.2.0",
84+
"karma": "^5.2.0 || ^6.0.0",
8585
"ng-packagr": "^11.0.0 || ^11.1.0-next",
8686
"protractor": "^7.0.0",
8787
"tslint": "^6.1.0",

packages/angular_devkit/build_angular/src/webpack/plugins/karma.ts

+21-30
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
6161
}
6262
}
6363

64-
const init: any = (config: any, emitter: any, customFileHandlers: any) => {
64+
const init: any = (config: any, emitter: any) => {
6565
if (!config.buildWebpack) {
6666
throw new Error(`The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to` +
6767
` be used from within Angular CLI and will not work correctly outside of it.`
@@ -219,39 +219,13 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
219219

220220
webpackMiddleware = new webpackDevMiddleware(compiler, webpackMiddlewareConfig);
221221

222-
// Forward requests to webpack server.
223-
customFileHandlers.push({
224-
urlRegex: new RegExp(`\\/${KARMA_APPLICATION_PATH}\\/.*`),
225-
handler: function handler(req: any, res: any) {
226-
webpackMiddleware(req, res, function () {
227-
// Ensure script and style bundles are served.
228-
// They are mentioned in the custom karma context page and we don't want them to 404.
229-
const alwaysServe = [
230-
`/${KARMA_APPLICATION_PATH}/runtime.js`,
231-
`/${KARMA_APPLICATION_PATH}/polyfills.js`,
232-
`/${KARMA_APPLICATION_PATH}/polyfills-es5.js`,
233-
`/${KARMA_APPLICATION_PATH}/scripts.js`,
234-
`/${KARMA_APPLICATION_PATH}/styles.js`,
235-
`/${KARMA_APPLICATION_PATH}/vendor.js`,
236-
];
237-
if (alwaysServe.indexOf(req.url) != -1) {
238-
res.statusCode = 200;
239-
res.end();
240-
} else {
241-
res.statusCode = 404;
242-
res.end('Not found');
243-
}
244-
});
245-
}
246-
});
247-
248222
emitter.on('exit', (done: any) => {
249223
webpackMiddleware.close();
250224
done();
251225
});
252226
};
253227

254-
init.$inject = ['config', 'emitter', 'customFileHandlers'];
228+
init.$inject = ['config', 'emitter'];
255229

256230
// Block requests until the Webpack compilation is done.
257231
function requestBlocker() {
@@ -327,8 +301,25 @@ sourceMapReporter.$inject = ['baseReporterDecorator', 'config'];
327301
function fallbackMiddleware() {
328302
return function (request: http.IncomingMessage, response: http.ServerResponse, next: () => void) {
329303
if (webpackMiddleware) {
330-
request.url = '/' + KARMA_APPLICATION_PATH + request.url;
331-
webpackMiddleware(request, response, next);
304+
if (request.url && !new RegExp(`\\/${KARMA_APPLICATION_PATH}\\/.*`).test(request.url)) {
305+
request.url = '/' + KARMA_APPLICATION_PATH + request.url;
306+
}
307+
webpackMiddleware(request, response, () => {
308+
const alwaysServe = [
309+
`/${KARMA_APPLICATION_PATH}/runtime.js`,
310+
`/${KARMA_APPLICATION_PATH}/polyfills.js`,
311+
`/${KARMA_APPLICATION_PATH}/polyfills-es5.js`,
312+
`/${KARMA_APPLICATION_PATH}/scripts.js`,
313+
`/${KARMA_APPLICATION_PATH}/styles.js`,
314+
`/${KARMA_APPLICATION_PATH}/vendor.js`,
315+
];
316+
if (request.url && alwaysServe.includes(request.url)) {
317+
response.statusCode = 200;
318+
response.end();
319+
} else {
320+
next();
321+
}
322+
});
332323
} else {
333324
next();
334325
}

0 commit comments

Comments
 (0)