Skip to content

Commit 736440f

Browse files
author
Piotr Oleś
committed
Kill service on single run + README improvement
1 parent 538216d commit 736440f

File tree

5 files changed

+76
-133
lines changed

5 files changed

+76
-133
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: node_js
2-
after_success: npm run coveralls
32
deploy:
43
provider: npm
54

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Fork TS Checker Webpack Plugin
22
[![Npm version](https://img.shields.io/npm/v/@realytics/fork-ts-checker-webpack-plugin.svg?style=flat-square)](https://www.npmjs.com/package/@realytics/fork-ts-checker-webpack-plugin)
3-
[![Build Status](https://travis-ci.org/realytics/fork-ts-checker-webpack-plugin.svg?branch=master)](https://travis-ci.org/realytics/fork-ts-checker-webpack-plugin)
3+
[![Build Status](https://travis-ci.org/Realytics/fork-ts-checker-webpack-plugin.svg?branch=master)](https://travis-ci.org/realytics/fork-ts-checker-webpack-plugin)
44

55
Webpack plugin that runs typescript type checker (with optional linter) on separate processes.
66

@@ -82,11 +82,24 @@ power.
8282

8383
Pre-computed consts:
8484
* `ForkTsCheckerWebpackPlugin.ONE_CPU` - always use one cpu (core)
85-
* `ForkTsCheckerWebpackPlugin.ONE_FREE_CPU` - leave only one cpu for build (probably will increase build time)
86-
* `ForkTsCheckerWebpackPlugin.TWO_FREE_CPUS` - leave two cpus free (one for build, one for system)
85+
* `ForkTsCheckerWebpackPlugin.ONE_CPU_FREE` - leave only one cpu for build (probably will increase build time)
86+
* `ForkTsCheckerWebpackPlugin.TWO_CPUS_FREE` - leave two cpus free (one for build, one for system)
8787

8888
**memoryLimit** `number` - Memory limit for service process in MB. If service exits with allocation failed error, increase this number.
8989
Default: `2048`.
9090

91+
## Plugin Hooks ##
92+
This plugin provides some custom webpack hooks (all are sync):
93+
94+
| Event name | Description | Params |
95+
|------------|-------------|--------|
96+
|`fork-ts-checker-cancel`| Cancellation has been requested | `cancellationToken` |
97+
|`fork-ts-checker-waiting`| Waiting for results | `hasTsLint` |
98+
|`fork-ts-checker-service-start`| Service will be started | `tsconfigPath`, `tslintPath`, `watchPaths`, `workersNumber`, `memoryLimit` |
99+
|`fork-ts-checker-service-out-of-memory`| Service is out of memory | - |
100+
|`fork-ts-checker-receive`| Plugin receives diagnostics and lints from service | `diagnostics`, `lints` |
101+
|`fork-ts-checker-emit`| Service will add errors and warnings to webpack compilation (`blockEmit: true`) | `diagnostics`, `lints`, `elapsed` |
102+
|`fork-ts-checker-done`| Service finished type checking and webpack finished compilation (`blockEmit: false`) | `diagnostics`, `lints`, `elapsed` |
103+
91104
## License ##
92105
MIT

lib/index.js

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ function ForkTsCheckerWebpackPlugin (options) {
2424
this.ignoreLints = options.ignoreLints || [];
2525
this.logger = options.logger || console;
2626
this.silent = !!options.silent;
27-
this.workers = options.workers || ForkTsCheckerWebpackPlugin.ONE_CPU;
27+
this.workersNumber = options.workers || ForkTsCheckerWebpackPlugin.ONE_CPU;
2828
this.memoryLimit = options.memoryLimit || ForkTsCheckerWebpackPlugin.DEFAULT_MEMORY_LIMIT;
2929

3030
this.tsconfigPath = undefined;
3131
this.tslintPath = undefined;
3232
this.watchPaths = [];
33+
this.isWatching = false;
3334
this.compiler = undefined;
3435
this.colors = new chalk.constructor({
3536
enabled: options.colors === undefined ? true : !!options.colors
@@ -75,6 +76,8 @@ ForkTsCheckerWebpackPlugin.prototype.apply = function (compiler) {
7576
}
7677

7778
if (tsconfigOk && tslintOk) {
79+
this.pluginStart();
80+
this.pluginStop();
7881
this.pluginCompile();
7982

8083
if (this.blockEmit) {
@@ -113,11 +116,38 @@ ForkTsCheckerWebpackPlugin.prototype.computeContextPath = function (filePath) {
113116
? filePath : path.resolve(this.compiler.options.context, filePath);
114117
};
115118

119+
ForkTsCheckerWebpackPlugin.prototype.pluginStart = function () {
120+
this.compiler.plugin('run', function (compiler, callback) {
121+
this.isWatching = false;
122+
callback();
123+
}.bind(this));
124+
125+
this.compiler.plugin('watch-run', function (watching, callback) {
126+
this.isWatching = true;
127+
callback();
128+
}.bind(this));
129+
};
130+
131+
ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
132+
this.compiler.plugin('done', function () {
133+
if (!this.isWatching && this.service) {
134+
try {
135+
this.service.kill();
136+
} catch (e) {
137+
if (this.logger && !this.silent) {
138+
this.logger.error(e);
139+
}
140+
}
141+
}
142+
}.bind(this));
143+
};
144+
116145
ForkTsCheckerWebpackPlugin.prototype.pluginCompile = function () {
117146
this.compiler.plugin('compile', function () {
118147
if (this.cancellationToken) {
119148
// request cancellation if there is not finished job
120149
this.cancellationToken.requestCancellation();
150+
this.compiler.applyPlugins('fork-ts-checker-cancel', this.cancellationToken);
121151
}
122152
this.checkDone = false;
123153
this.compilationDone = false;
@@ -171,27 +201,33 @@ ForkTsCheckerWebpackPlugin.prototype.pluginDone = function () {
171201

172202
ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
173203
this.service = childProcess.fork(
174-
path.resolve(__dirname, this.workers > 1 ? './cluster.js' : './service.js'),
204+
path.resolve(__dirname, this.workersNumber > 1 ? './cluster.js' : './service.js'),
175205
[],
176206
{
177-
execArgv: this.workers > 1 ? [] : ['--max-old-space-size=' + this.memoryLimit],
207+
execArgv: this.workersNumber > 1 ? [] : ['--max-old-space-size=' + this.memoryLimit],
178208
env: {
179209
TSCONFIG: this.tsconfigPath,
180210
TSLINT: this.tslintPath || '',
181211
WATCH: this.watchPaths.join('|'),
182-
WORK_DIVISION: Math.max(1, this.workers),
212+
WORK_DIVISION: Math.max(1, this.workersNumber),
183213
MEMORY_LIMIT: this.memoryLimit
184214
},
185215
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
186216
}
187217
);
188-
189-
this.compiler.applyPlugins('fork-ts-checker-service-start');
218+
this.compiler.applyPlugins(
219+
'fork-ts-checker-service-start',
220+
this.tsconfigPath,
221+
this.tslintPath,
222+
this.watchPaths,
223+
this.workersNumber,
224+
this.memoryLimit
225+
);
190226

191227
if (!this.silent && this.logger) {
192228
var message = 'Starting type checking' + (this.tslint ? ' and linting' : '') + ' service...';
193229
var performance = (
194-
'Using ' + this.colors.bold(this.workers === 1 ? '1 worker' : this.workers + ' workers') +
230+
'Using ' + this.colors.bold(this.workersNumber === 1 ? '1 worker' : this.workersNumber + ' workers') +
195231
' with ' + this.colors.bold(this.memoryLimit + 'MB') + ' memory limit'
196232
);
197233
var lines = [message, performance, this.colors.grey(this.tsconfigPath)];
@@ -200,7 +236,7 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
200236
}
201237

202238
this.logger.info(lines.join('\n'));
203-
if (this.watchPaths.length) {
239+
if (this.watchPaths.length && this.isWatching) {
204240
this.logger.info(
205241
'Watching:' +
206242
(this.watchPaths.length > 1 ? '\n' : ' ') +
@@ -239,6 +275,8 @@ ForkTsCheckerWebpackPlugin.prototype.handleServiceMessage = function (message) {
239275
}.bind(this));
240276
}
241277

278+
this.compiler.applyPlugins('fork-ts-checker-receive', this.diagnostics, this.lints);
279+
242280
if (this.compilationDone) {
243281
this.blockEmit ? this.emitCallback() : this.doneCallback();
244282
}
@@ -263,6 +301,15 @@ ForkTsCheckerWebpackPlugin.prototype.handleServiceExit = function (code, signal)
263301

264302
ForkTsCheckerWebpackPlugin.prototype.createEmitCallback = function (compilation, callback) {
265303
return function emitCallback () {
304+
var elapsed = Math.round(this.elapsed[0] * 1E9 + this.elapsed[1]);
305+
306+
this.compiler.applyPlugins(
307+
'fork-ts-checker-emit',
308+
this.diagnostics,
309+
this.lints,
310+
elapsed
311+
);
312+
266313
this.diagnostics.concat(this.lints).forEach(function (message) {
267314
// webpack message format
268315
var formatted = {

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"test": "NODE_ENV=test node_modules/.bin/mocha -R spec",
88
"test:watch": "NODE_ENV=test node_modules/.bin/mocha -R spec --watch",
99
"test:cover": "rimraf coverage && node_modules/.bin/istanbul cover -root lib --include-all-sources node_modules/.bin/_mocha -- -R spec",
10-
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls",
1110
"lint": "node node_modules/.bin/eslint ./lib ./test",
1211
"lint:fix": "node node_modules/.bin/eslint ./lib ./test --fix"
1312
},
@@ -33,9 +32,6 @@
3332
},
3433
"devDependencies": {
3534
"chai": "^3.5.0",
36-
"chai-spies": "^0.7.1",
37-
"coveralls": "^2.11.15",
38-
"cross-env": "^4.0.0",
3935
"eslint": "^3.19.0",
4036
"istanbul": "^0.4.5",
4137
"mocha": "^3.2.0",

0 commit comments

Comments
 (0)