Skip to content

Commit 2a89e25

Browse files
committed
compile in karma plugin
1 parent 2fa5abe commit 2a89e25

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"homepage": "https://github.com/angular/angular-cli",
4242
"dependencies": {
43+
"@types/webpack-dev-middleware": "^1.9.1",
4344
"async": "^2.3.0",
4445
"autoprefixer": "^6.5.3",
4546
"chalk": "^1.1.3",

packages/@angular/cli/models/webpack-configs/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function getTestConfig(testConfig: WebpackTestOptions) {
9191
rules: [].concat(extraRules)
9292
},
9393
plugins: [
94-
new webpackBuildLogger({ logEnabled: true }),
94+
// new webpackBuildLogger({ logEnabled: true }),
9595
// new (<any>FileListPlugin)(),
9696
new KarmaWebpackEmitlessError()
9797
].concat(extraPlugins)

packages/@angular/cli/plugins/karma.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
33
import * as glob from 'glob';
4+
import * as webpack from 'webpack';
5+
const webpackDevMiddleware = require('webpack-dev-middleware');
46

57
import { Pattern } from './glob-copy-webpack-plugin';
68
import { WebpackTestConfig, WebpackTestOptions } from '../models/webpack-test-config';
@@ -39,7 +41,7 @@ function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
3941
}
4042
}
4143

42-
const init: any = (config: any) => {
44+
const init: any = (config: any, emitter: any, customFileHandlers: any) => {
4345
const appConfig = getAppFromConfig(config.angularCli.app);
4446
const appRoot = path.join(config.basePath, appConfig.root);
4547
const testConfig: WebpackTestOptions = Object.assign({
@@ -88,6 +90,8 @@ const init: any = (config: any) => {
8890
const webpackConfig = new WebpackTestConfig(testConfig, appConfig).buildConfig();
8991
const webpackMiddlewareConfig = {
9092
noInfo: true, // Hide webpack output because its noisy.
93+
watchOptions: { poll: testConfig.poll },
94+
publicPath: '/_karma_webpack_/',
9195
stats: { // Also prevent chunk and module display output, cleaner look. Only emit errors.
9296
assets: false,
9397
colors: true,
@@ -97,9 +101,6 @@ const init: any = (config: any) => {
97101
chunks: false,
98102
chunkModules: false
99103
},
100-
watchOptions: {
101-
poll: testConfig.poll
102-
}
103104
};
104105

105106
// If Karma is being ran in single run mode, throw errors.
@@ -120,15 +121,54 @@ const init: any = (config: any) => {
120121
config.customDebugFile = `${__dirname}/karma-debug.html`;
121122

122123
config.files.forEach((file: any) => {
123-
if (file.pattern === `./${appConfig.root}/${appConfig.test}`){
124+
if (file.pattern === `./${appConfig.root}/${appConfig.test}`) {
124125
file.watched = false;
125126
file.served = false;
126127
file.included = false;
127128
}
128129
});
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: /^\/_karma_webpack_\/.*/,
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+
});
129169
};
130170

131-
init.$inject = ['config'];
171+
init.$inject = ['config', 'emitter', 'customFileHandlers'];
132172

133173
// Dummy preprocessor, just to keep karma from showing a warning.
134174
const preprocessor: any = () => (content: any, _file: string, done: any) => done(null, content);
@@ -138,4 +178,5 @@ preprocessor.$inject = [];
138178
module.exports = Object.assign({
139179
'framework:@angular/cli': ['factory', init],
140180
'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

Comments
 (0)