Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 938fc96

Browse files
author
Nikita Korotkih
committed
feat(*): debounce running checker
1 parent fa71235 commit 938fc96

File tree

4 files changed

+117
-6
lines changed

4 files changed

+117
-6
lines changed

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"colors": "^1.1.2",
3535
"enhanced-resolve": "^0.9.1",
3636
"loader-utils": "^0.2.6",
37-
"lodash": "^3.10.0",
37+
"lodash": "^3.10.1",
3838
"object-assign": "^2.1.1",
3939
"parse-json": "^2.2.0",
4040
"strip-bom": "^2.0.0",
@@ -59,8 +59,10 @@
5959
"load-grunt-tasks": "^0.6.0",
6060
"mkdirp": "^0.5.1",
6161
"mocha": "^2.3.4",
62+
"ps-node": "0.0.5",
6263
"rimraf": "^2.5.0",
6364
"tslint": "^3.2.2-dev.1",
64-
"typescript": "^1.8.0-dev.20151202"
65+
"typescript": "^1.8.0-dev.20151202",
66+
"webpack-dev-server": "^1.14.1"
6567
}
6668
}

src/instance.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ function setupWatchRun(compiler, instanceName: string) {
258258
});
259259
}
260260

261+
let runChecker = (instance, payload) => {
262+
instance.checker.send({
263+
messageType: 'compile',
264+
payload
265+
});
266+
};
267+
268+
runChecker = _.debounce(runChecker, 200);
269+
261270
function setupAfterCompile(compiler, instanceName, forkChecker = false) {
262271
compiler.plugin('after-compile', function(compilation, callback) {
263272
let instance: ICompilerInstance = resolveInstance(compilation.compiler, instanceName);
@@ -269,10 +278,7 @@ function setupAfterCompile(compiler, instanceName, forkChecker = false) {
269278
resolutionCache: state.host.moduleResolutionHost.resolutionCache
270279
};
271280

272-
instance.checker.send({
273-
messageType: 'compile',
274-
payload
275-
});
281+
runChecker(instance, payload);
276282
} else {
277283
if (!state.program) {
278284
// program may be undefined here, if all files

test/checker.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var webpack = require('webpack');
2+
var Promise = require('bluebird');
3+
var ps = Promise.promisifyAll(require('ps-node'));
4+
var fs = Promise.promisifyAll(require('fs'));
5+
var expect = require('expect');
6+
7+
var path = require('path');
8+
var loader = path.resolve(__dirname, '../dist.babel');
9+
var ForkCheckerPlugin = require(loader).ForkCheckerPlugin;
10+
var globalConfig = {
11+
watch: true,
12+
module: {
13+
loaders: [
14+
{
15+
test: /\.ts?/,
16+
loader: loader + '?doTypeCheck&+forkChecker',
17+
},
18+
],
19+
},
20+
plugins: [
21+
new ForkCheckerPlugin(),
22+
],
23+
};
24+
25+
var outputDir = path.resolve(__dirname, './output/');
26+
27+
describe('checker test', function() {
28+
var toCheckFilename = './test/fixtures/to-check.ts';
29+
30+
this.timeout(5000);
31+
32+
var outputFile = path.resolve(outputDir, toCheckFilename);
33+
var config = {
34+
output: {
35+
path: outputDir,
36+
filename: toCheckFilename,
37+
},
38+
entry: toCheckFilename,
39+
};
40+
var getCheckerRuntimeProcess = () => {
41+
return ps.lookupAsync({
42+
command: new RegExp('node'),
43+
arguments: new RegExp('checker-runtime'),
44+
psargs: 'aux'
45+
}).then(res => {
46+
return res[0];
47+
});
48+
};
49+
50+
var utilizeProcess = (p) => {
51+
expect(p).toExist();
52+
53+
return ps.killAsync(p.pid);
54+
};
55+
56+
it('should fork checker in separate process', function(done) {
57+
// assert that checker starts on startup
58+
new Promise((resolve, reject) => {
59+
webpack(Object.assign(globalConfig, config), function(err, stat) {
60+
resolve();
61+
});
62+
})
63+
.then(getCheckerRuntimeProcess)
64+
.then(p => {
65+
expect(p).toExist();
66+
return utilizeProcess(p);
67+
})
68+
.then(() => {
69+
done();
70+
})
71+
});
72+
73+
it('should fork only one checker after multiple changes', function(done) {
74+
// I didn't get how to test it more precise, so it's more like a proof of work
75+
var fileContent = fs.readFileSync(toCheckFilename);
76+
var updateFile = () => fs.writeFileAsync(toCheckFilename, Date.now());
77+
var timeoutAsync = (ms) => {
78+
return new Promise((resolve, reject) => {
79+
setTimeout(resolve, ms);
80+
});
81+
};
82+
83+
new Promise((resolve, reject) => {
84+
webpack(Object.assign(globalConfig, config), function(err, stat) {
85+
resolve();
86+
});
87+
})
88+
.then(getCheckerRuntimeProcess)
89+
.then(p => {
90+
expect(p).toExist();
91+
var times = [];
92+
var i = 10;
93+
while(i--) { times.push(i); }
94+
95+
return Promise.each(times, () => {
96+
return updateFile().then(() => timeoutAsync(50));
97+
}).then(() => fs.writeFileAsync(toCheckFilename, fileContent));
98+
})
99+
.then(() => timeoutAsync(2000))
100+
.then(() => { done(); });
101+
});
102+
});

test/fixtures/to-check.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1453065049802

0 commit comments

Comments
 (0)