Skip to content

Commit

Permalink
feat(@angular-devkit/build-angular): support karma version 6.x
Browse files Browse the repository at this point in the history
This change adds support for using karma 6.x within a project.
  • Loading branch information
clydin authored and alan-agius4 committed Jan 13, 2021
1 parent d93f78b commit 2ce5445
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 213 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"jest-worker": "26.6.2",
"jquery": "^3.3.1",
"jsonc-parser": "3.0.0",
"karma": "~5.2.0",
"karma": "~6.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"peerDependencies": {
"@angular/compiler-cli": "^11.0.0 || ^11.1.0-next",
"@angular/localize": "^11.0.0 || ^11.1.0-next",
"karma": "^5.2.0",
"karma": "^5.2.0 || ^6.0.0",
"ng-packagr": "^11.0.0 || ^11.1.0-next",
"protractor": "^7.0.0",
"tslint": "^6.1.0",
Expand Down
51 changes: 21 additions & 30 deletions packages/angular_devkit/build_angular/src/webpack/plugins/karma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
}
}

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

webpackMiddleware = new webpackDevMiddleware(compiler, webpackMiddlewareConfig);

// Forward requests to webpack server.
customFileHandlers.push({
urlRegex: new RegExp(`\\/${KARMA_APPLICATION_PATH}\\/.*`),
handler: function handler(req: any, res: any) {
webpackMiddleware(req, res, function () {
// Ensure script and style bundles are served.
// They are mentioned in the custom karma context page and we don't want them to 404.
const alwaysServe = [
`/${KARMA_APPLICATION_PATH}/runtime.js`,
`/${KARMA_APPLICATION_PATH}/polyfills.js`,
`/${KARMA_APPLICATION_PATH}/polyfills-es5.js`,
`/${KARMA_APPLICATION_PATH}/scripts.js`,
`/${KARMA_APPLICATION_PATH}/styles.js`,
`/${KARMA_APPLICATION_PATH}/vendor.js`,
];
if (alwaysServe.indexOf(req.url) != -1) {
res.statusCode = 200;
res.end();
} else {
res.statusCode = 404;
res.end('Not found');
}
});
}
});

emitter.on('exit', (done: any) => {
webpackMiddleware.close();
done();
});
};

init.$inject = ['config', 'emitter', 'customFileHandlers'];
init.$inject = ['config', 'emitter'];

// Block requests until the Webpack compilation is done.
function requestBlocker() {
Expand Down Expand Up @@ -327,8 +301,25 @@ sourceMapReporter.$inject = ['baseReporterDecorator', 'config'];
function fallbackMiddleware() {
return function (request: http.IncomingMessage, response: http.ServerResponse, next: () => void) {
if (webpackMiddleware) {
request.url = '/' + KARMA_APPLICATION_PATH + request.url;
webpackMiddleware(request, response, next);
if (request.url && !new RegExp(`\\/${KARMA_APPLICATION_PATH}\\/.*`).test(request.url)) {
request.url = '/' + KARMA_APPLICATION_PATH + request.url;
}
webpackMiddleware(request, response, () => {
const alwaysServe = [
`/${KARMA_APPLICATION_PATH}/runtime.js`,
`/${KARMA_APPLICATION_PATH}/polyfills.js`,
`/${KARMA_APPLICATION_PATH}/polyfills-es5.js`,
`/${KARMA_APPLICATION_PATH}/scripts.js`,
`/${KARMA_APPLICATION_PATH}/styles.js`,
`/${KARMA_APPLICATION_PATH}/vendor.js`,
];
if (request.url && alwaysServe.includes(request.url)) {
response.statusCode = 200;
response.end();
} else {
next();
}
});
} else {
next();
}
Expand Down

0 comments on commit 2ce5445

Please sign in to comment.