fork-ts-checker-webpack-plugin 0.2.6 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -1,16 +1,16 @@
1
- var path = require('path');
2
- var process = require('process');
3
- var childProcess = require('child_process');
4
- var chalk = require('chalk');
5
- var fs = require('fs');
6
- var os = require('os');
7
- var isString = require('lodash.isstring');
8
- var isFunction = require('lodash.isfunction');
9
- var CancellationToken = require('./CancellationToken');
10
- var NormalizedMessage = require('./NormalizedMessage');
11
- var createDefaultFormatter = require('./formatter/defaultFormatter');
12
- var createCodeframeFormatter = require('./formatter/codeframeFormatter');
13
-
1
+ "use strict";
2
+ var path = require("path");
3
+ var process = require("process");
4
+ var childProcess = require("child_process");
5
+ var chalk = require("chalk");
6
+ var fs = require("fs");
7
+ var os = require("os");
8
+ var isString = require("lodash.isstring");
9
+ var isFunction = require("lodash.isfunction");
10
+ var CancellationToken = require("./CancellationToken");
11
+ var NormalizedMessage = require("./NormalizedMessage");
12
+ var createDefaultFormatter = require("./formatter/defaultFormatter");
13
+ var createCodeframeFormatter = require("./formatter/codeframeFormatter");
14
14
  /**
15
15
  * ForkTsCheckerWebpackPlugin
16
16
  * Runs typescript type checker and linter (tslint) on separate process.
@@ -18,405 +18,334 @@ var createCodeframeFormatter = require('./formatter/codeframeFormatter');
18
18
  *
19
19
  * Options description in README.md
20
20
  */
21
- function ForkTsCheckerWebpackPlugin (options) {
22
- options = options || {};
23
- this.options = Object.assign({}, options);
24
-
25
- this.tsconfig = options.tsconfig || './tsconfig.json';
26
- this.tslint = options.tslint ?
27
- options.tslint === true ? './tslint.json' : options.tslint : undefined;
28
- this.watch = isString(options.watch) ? [options.watch] : options.watch || [];
29
- this.ignoreDiagnostics = options.ignoreDiagnostics || [];
30
- this.ignoreLints = options.ignoreLints || [];
31
- this.logger = options.logger || console;
32
- this.silent = options.silent === true; // default false
33
- this.async = options.async !== false; // default true
34
- this.workersNumber = options.workers || ForkTsCheckerWebpackPlugin.ONE_CPU;
35
- this.memoryLimit = options.memoryLimit || ForkTsCheckerWebpackPlugin.DEFAULT_MEMORY_LIMIT;
36
- this.useColors = options.colors !== false; // default true
37
- this.colors = new chalk.constructor({ enabled: this.useColors });
38
- this.formatter = (options.formatter && isFunction(options.formatter)) ?
39
- options.formatter : ForkTsCheckerWebpackPlugin.createFormatter(options.formatter || 'default', options.formatterOptions || {});
40
-
41
- this.tsconfigPath = undefined;
42
- this.tslintPath = undefined;
43
- this.watchPaths = [];
44
- this.compiler = undefined;
45
-
46
- this.started = undefined;
47
- this.elapsed = undefined;
48
- this.cancellationToken = undefined;
49
-
50
- this.isWatching = false;
51
- this.checkDone = false;
52
- this.compilationDone = false;
53
- this.diagnostics = [];
54
- this.lints = [];
55
-
56
- this.emitCallback = this.createNoopEmitCallback();
57
- this.doneCallback = this.createDoneCallback();
58
-
59
- this.typescriptVersion = require('typescript').version;
60
- this.tslintVersion = this.tslint ? require('tslint').Linter.VERSION : undefined;
61
- }
62
- module.exports = ForkTsCheckerWebpackPlugin;
63
-
64
- ForkTsCheckerWebpackPlugin.DEFAULT_MEMORY_LIMIT = 2048;
65
-
66
- ForkTsCheckerWebpackPlugin.ONE_CPU = 1;
67
- ForkTsCheckerWebpackPlugin.ALL_CPUS = os.cpus().length;
68
- ForkTsCheckerWebpackPlugin.ONE_CPU_FREE = Math.max(1, ForkTsCheckerWebpackPlugin.ALL_CPUS - 1);
69
- ForkTsCheckerWebpackPlugin.TWO_CPUS_FREE = Math.max(1, ForkTsCheckerWebpackPlugin.ALL_CPUS - 2);
70
-
71
- ForkTsCheckerWebpackPlugin.createFormatter = function (type, options) {
72
- switch (type) {
73
- case 'default':
74
- return createDefaultFormatter(options);
75
- case 'codeframe':
76
- return createCodeframeFormatter(options);
77
- default:
78
- throw new Error('Unknown "' + type + '" formatter. Available are: default, codeframe.');
79
- }
80
- };
81
-
82
- ForkTsCheckerWebpackPlugin.prototype.apply = function (compiler) {
83
- this.compiler = compiler;
84
-
85
- this.tsconfigPath = this.computeContextPath(this.tsconfig);
86
- this.tslintPath = this.tslint ? this.computeContextPath(this.tslint) : null;
87
- this.watchPaths = this.watch.map(this.computeContextPath.bind(this));
88
-
89
- // validate config
90
- var tsconfigOk = fs.existsSync(this.tsconfigPath);
91
- var tslintOk = !this.tslintPath || fs.existsSync(this.tslintPath);
92
-
93
- // validate logger
94
- if (this.logger) {
95
- if (!this.logger.error || !this.logger.warn || !this.logger.info) {
96
- throw new Error('Invalid logger object - doesn\'t provide `error`, `warn` or `info` method.');
97
- }
98
- }
99
-
100
- if (tsconfigOk && tslintOk) {
101
- this.pluginStart();
102
- this.pluginStop();
103
- this.pluginCompile();
104
- this.pluginEmit();
105
- this.pluginDone();
106
- } else {
107
- if (!tsconfigOk) {
108
- throw new Error(
109
- 'Cannot find "' + this.tsconfigPath + '" file. Please check webpack and ForkTsCheckerWebpackPlugin configuration. \n' +
110
- 'Possible errors: \n' +
111
- ' - wrong `context` directory in webpack configuration' +
112
- ' (if `tsconfig` is not set or is a relative path in fork plugin configuration)\n' +
113
- ' - wrong `tsconfig` path in fork plugin configuration' +
114
- ' (should be a relative or absolute path)'
115
- );
116
- }
117
- if (!tslintOk) {
118
- throw new Error(
119
- 'Cannot find "' + this.tslintPath + '" file. Please check webpack and ForkTsCheckerWebpackPlugin configuration. \n' +
120
- 'Possible errors: \n' +
121
- ' - wrong `context` directory in webpack configuration' +
122
- ' (if `tslint` is not set or is a relative path in fork plugin configuration)\n' +
123
- ' - wrong `tslint` path in fork plugin configuration' +
124
- ' (should be a relative or absolute path)\n' +
125
- ' - `tslint` path is not set to false in fork plugin configuration' +
126
- ' (if you want to disable tslint support)'
127
- );
128
- }
129
- }
130
- };
131
-
132
- ForkTsCheckerWebpackPlugin.prototype.computeContextPath = function (filePath) {
133
- return path.isAbsolute(filePath) ? filePath : path.resolve(this.compiler.options.context, filePath);
134
- };
135
-
136
- ForkTsCheckerWebpackPlugin.prototype.pluginStart = function () {
137
- this.compiler.plugin('run', function (compiler, callback) {
138
- this.isWatching = false;
139
- callback();
140
- }.bind(this));
141
-
142
- this.compiler.plugin('watch-run', function (watching, callback) {
143
- this.isWatching = true;
144
- callback();
145
- }.bind(this));
146
- };
147
-
148
- ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
149
- this.compiler.plugin('done', function () {
150
- if (!this.isWatching) {
151
- this.killService();
152
- }
153
- }.bind(this));
154
-
155
- process.on('exit', function () {
156
- this.killService();
157
- }.bind(this));
158
- };
159
-
160
- ForkTsCheckerWebpackPlugin.prototype.pluginCompile = function () {
161
- this.compiler.plugin('compile', function () {
162
- if (this.cancellationToken) {
163
- // request cancellation if there is not finished job
164
- this.cancellationToken.requestCancellation();
165
- this.compiler.applyPlugins('fork-ts-checker-cancel', this.cancellationToken);
166
- }
167
- this.checkDone = false;
168
- this.compilationDone = false;
169
-
170
- this.started = process.hrtime();
171
-
172
- // create new token for current job
173
- this.cancellationToken = new CancellationToken();
174
- if (!this.service || !this.service.connected) {
175
- this.spawnService();
176
- }
177
-
178
- try {
179
- this.service.send(this.cancellationToken);
180
- } catch (error) {
181
- if (!this.silent && this.logger) {
182
- this.logger.error(this.colors.red('Cannot start checker service: ' + (error ? error.toString() : 'Unknown error')));
183
- }
184
-
185
- this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
186
- }
187
- }.bind(this));
188
- };
189
-
190
- ForkTsCheckerWebpackPlugin.prototype.pluginEmit = function () {
191
- this.compiler.plugin('emit', function (compilation, callback) {
192
- if (this.isWatching && this.async) {
193
- callback();
194
- return;
195
- }
196
-
197
- this.emitCallback = this.createEmitCallback(compilation, callback);
198
-
199
- if (this.checkDone) {
200
- this.emitCallback();
201
- }
202
-
203
- this.compilationDone = true;
204
- }.bind(this));
205
- };
206
-
207
- ForkTsCheckerWebpackPlugin.prototype.pluginDone = function () {
208
- this.compiler.plugin('done', function () {
209
- if (!this.isWatching || !this.async) {
210
- return;
21
+ var ForkTsCheckerWebpackPlugin = /** @class */ (function () {
22
+ function ForkTsCheckerWebpackPlugin(options) {
23
+ options = options || {};
24
+ this.options = Object.assign({}, options);
25
+ this.tsconfig = options.tsconfig || './tsconfig.json';
26
+ this.tslint = options.tslint ?
27
+ options.tslint === true ? './tslint.json' : options.tslint : undefined;
28
+ this.watch = isString(options.watch) ? [options.watch] : options.watch || [];
29
+ this.ignoreDiagnostics = options.ignoreDiagnostics || [];
30
+ this.ignoreLints = options.ignoreLints || [];
31
+ this.logger = options.logger || console;
32
+ this.silent = options.silent === true; // default false
33
+ this.async = options.async !== false; // default true
34
+ this.checkSyntacticErrors = options.checkSyntacticErrors === true; // default false
35
+ this.workersNumber = options.workers || ForkTsCheckerWebpackPlugin.ONE_CPU;
36
+ this.memoryLimit = options.memoryLimit || ForkTsCheckerWebpackPlugin.DEFAULT_MEMORY_LIMIT;
37
+ this.useColors = options.colors !== false; // default true
38
+ this.colors = new chalk.constructor({ enabled: this.useColors });
39
+ this.formatter = (options.formatter && isFunction(options.formatter))
40
+ ? options.formatter
41
+ : ForkTsCheckerWebpackPlugin.createFormatter(options.formatter || 'default', options.formatterOptions || {});
42
+ this.tsconfigPath = undefined;
43
+ this.tslintPath = undefined;
44
+ this.watchPaths = [];
45
+ this.compiler = undefined;
46
+ this.started = undefined;
47
+ this.elapsed = undefined;
48
+ this.cancellationToken = undefined;
49
+ this.isWatching = false;
50
+ this.checkDone = false;
51
+ this.compilationDone = false;
52
+ this.diagnostics = [];
53
+ this.lints = [];
54
+ this.emitCallback = this.createNoopEmitCallback();
55
+ this.doneCallback = this.createDoneCallback();
56
+ this.typescriptVersion = require('typescript').version;
57
+ this.tslintVersion = this.tslint ? require('tslint').Linter.VERSION : undefined;
211
58
  }
212
-
213
- if (this.checkDone) {
214
- this.doneCallback();
215
- } else {
216
- if (this.compiler) {
217
- this.compiler.applyPlugins(
218
- 'fork-ts-checker-waiting',
219
- this.tslint !== false
220
- );
221
- }
222
- if (!this.silent && this.logger) {
223
- this.logger.info(
224
- this.tslint
225
- ? 'Type checking in progress...'
226
- : 'Type checking and linting in progress...'
227
- );
228
- }
229
- }
230
-
231
- this.compilationDone = true;
232
- }.bind(this));
233
- };
234
-
235
- ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
236
- this.service = childProcess.fork(
237
- path.resolve(__dirname, this.workersNumber > 1 ? './cluster.js' : './service.js'),
238
- [],
239
- {
240
- execArgv: this.workersNumber > 1 ? [] : ['--max-old-space-size=' + this.memoryLimit],
241
- env: {
242
- TSCONFIG: this.tsconfigPath,
243
- TSLINT: this.tslintPath || '',
244
- WATCH: this.isWatching ? this.watchPaths.join('|') : '',
245
- WORK_DIVISION: Math.max(1, this.workersNumber),
246
- MEMORY_LIMIT: this.memoryLimit
247
- },
248
- stdio: ['inherit', 'inherit', 'inherit', 'ipc']
249
- }
250
- );
251
-
252
- this.compiler.applyPlugins(
253
- 'fork-ts-checker-service-start',
254
- this.tsconfigPath,
255
- this.tslintPath,
256
- this.watchPaths,
257
- this.workersNumber,
258
- this.memoryLimit
259
- );
260
-
261
- if (!this.silent && this.logger) {
262
- this.logger.info('Starting type checking' + (this.tslint ? ' and linting' : '') + ' service...');
263
- this.logger.info(
264
- 'Using ' + this.colors.bold(this.workersNumber === 1 ? '1 worker' : this.workersNumber + ' workers') +
265
- ' with ' + this.colors.bold(this.memoryLimit + 'MB') + ' memory limit'
266
- );
267
-
268
- if (this.watchPaths.length && this.isWatching) {
269
- this.logger.info(
270
- 'Watching:' +
271
- (this.watchPaths.length > 1 ? '\n' : ' ') +
272
- this.watchPaths
273
- .map(function (path) { return this.colors.grey(path); }.bind(this))
274
- .join('\n')
275
- );
276
- }
277
- }
278
-
279
- this.service.on('message', this.handleServiceMessage.bind(this));
280
- this.service.on('exit', this.handleServiceExit.bind(this));
281
- };
282
-
283
- ForkTsCheckerWebpackPlugin.prototype.killService = function () {
284
- if (this.service) {
285
- try {
286
- if (this.cancellationToken) {
287
- this.cancellationToken.cleanupCancellation();
288
- }
289
-
290
- this.service.kill();
291
- this.service = undefined;
292
- } catch (e) {
293
- if (this.logger && !this.silent) {
294
- this.logger.error(e);
295
- }
296
- }
297
- }
298
- };
299
-
300
- ForkTsCheckerWebpackPlugin.prototype.handleServiceMessage = function (message) {
301
- if (this.cancellationToken) {
302
- this.cancellationToken.cleanupCancellation();
303
- // job is done - nothing to cancel
304
- this.cancellationToken = undefined;
305
- }
306
-
307
- this.checkDone = true;
308
- this.elapsed = process.hrtime(this.started);
309
- this.diagnostics = message.diagnostics.map(NormalizedMessage.createFromJSON);
310
- this.lints = message.lints.map(NormalizedMessage.createFromJSON);
311
-
312
- if (this.ignoreDiagnostics.length) {
313
- this.diagnostics = this.diagnostics.filter(function (diagnostic) {
314
- return this.ignoreDiagnostics.indexOf(diagnostic.getCode()) === -1;
315
- }.bind(this));
316
- }
317
-
318
- if (this.ignoreLints.length) {
319
- this.lints = this.lints.filter(function (lint) {
320
- return this.ignoreLints.indexOf(lint.getCode()) === -1;
321
- }.bind(this));
322
- }
323
-
324
- this.compiler.applyPlugins('fork-ts-checker-receive', this.diagnostics, this.lints);
325
-
326
- if (this.compilationDone) {
327
- (this.isWatching && this.async) ? this.doneCallback() : this.emitCallback();
328
- }
329
- };
330
-
331
- ForkTsCheckerWebpackPlugin.prototype.handleServiceExit = function (code, signal) {
332
- if (signal === 'SIGABRT') {
333
- // probably out of memory :/
334
- if (this.compiler) {
335
- this.compiler.applyPlugins('fork-ts-checker-service-out-of-memory');
336
- }
337
- if (!this.silent && this.logger) {
338
- this.logger.error(
339
- this.colors.red(
340
- 'Type checking and linting aborted - probably out of memory. ' +
341
- 'Check `memoryLimit` option in ForkTsCheckerWebpackPlugin configuration.'
342
- )
343
- );
344
- }
345
- }
346
- };
347
-
348
- ForkTsCheckerWebpackPlugin.prototype.createEmitCallback = function (compilation, callback) {
349
- return function emitCallback () {
350
- var elapsed = Math.round(this.elapsed[0] * 1E9 + this.elapsed[1]);
351
-
352
- this.compiler.applyPlugins(
353
- 'fork-ts-checker-emit',
354
- this.diagnostics,
355
- this.lints,
356
- elapsed
357
- );
358
-
359
- this.diagnostics.concat(this.lints).forEach(function (message) {
360
- // webpack message format
361
- var formatted = {
362
- rawMessage: (
363
- message.getSeverity().toUpperCase() + ' ' + message.getFormattedCode() + ': ' +
364
- message.getContent()
365
- ),
366
- message: '(' + message.getLine() + ',' + message.getCharacter() + '): ' + message.getContent(),
367
- location: {
368
- line: message.getLine(),
369
- character: message.getCharacter()
370
- },
371
- file: message.getFile()
372
- };
373
-
374
- if (message.isWarningSeverity()) {
375
- compilation.warnings.push(formatted);
376
- } else {
377
- compilation.errors.push(formatted);
378
- }
379
- });
380
-
381
- callback();
382
- };
383
- };
384
-
385
- ForkTsCheckerWebpackPlugin.prototype.createNoopEmitCallback = function () {
386
- return function noopEmitCallback () {};
387
- };
388
-
389
- ForkTsCheckerWebpackPlugin.prototype.createDoneCallback = function () {
390
- return function doneCallback () {
391
- var elapsed = Math.round(this.elapsed[0] * 1E9 + this.elapsed[1]);
392
-
393
- if (this.compiler) {
394
- this.compiler.applyPlugins(
395
- 'fork-ts-checker-done',
396
- this.diagnostics,
397
- this.lints,
398
- elapsed
399
- );
400
- }
401
- if (!this.silent && this.logger) {
402
- if (this.diagnostics.length || this.lints.length) {
403
- (this.lints || []).concat(this.diagnostics).forEach(function (message) {
404
- var formattedMessage = this.formatter(message, this.useColors);
405
-
406
- message.isWarningSeverity() ? this.logger.warn(formattedMessage) : this.logger.error(formattedMessage);
407
- }.bind(this));
408
- }
409
- if (!this.diagnostics.length) {
410
- this.logger.info(this.colors.green('No type errors found'));
411
- }
412
- if (this.tslint && !this.lints.length) {
413
- this.logger.info(this.colors.green('No lint errors found'));
414
- }
415
- this.logger.info(
416
- 'Version: typescript ' + this.colors.bold(this.typescriptVersion) +
417
- (this.tslint ? ', tslint ' + this.colors.bold(this.tslintVersion) : '')
418
- );
419
- this.logger.info('Time: ' + this.colors.bold(Math.round(elapsed / 1E6)) + 'ms');
420
- }
421
- };
422
- };
59
+ ForkTsCheckerWebpackPlugin.createFormatter = function (type, options) {
60
+ switch (type) {
61
+ case 'default':
62
+ return createDefaultFormatter();
63
+ case 'codeframe':
64
+ return createCodeframeFormatter(options);
65
+ default:
66
+ throw new Error('Unknown "' + type + '" formatter. Available are: default, codeframe.');
67
+ }
68
+ };
69
+ ForkTsCheckerWebpackPlugin.prototype.apply = function (compiler) {
70
+ this.compiler = compiler;
71
+ this.tsconfigPath = this.computeContextPath(this.tsconfig);
72
+ this.tslintPath = this.tslint ? this.computeContextPath(this.tslint) : null;
73
+ this.watchPaths = this.watch.map(this.computeContextPath.bind(this));
74
+ // validate config
75
+ var tsconfigOk = fs.existsSync(this.tsconfigPath);
76
+ var tslintOk = !this.tslintPath || fs.existsSync(this.tslintPath);
77
+ // validate logger
78
+ if (this.logger) {
79
+ if (!this.logger.error || !this.logger.warn || !this.logger.info) {
80
+ throw new Error('Invalid logger object - doesn\'t provide `error`, `warn` or `info` method.');
81
+ }
82
+ }
83
+ if (tsconfigOk && tslintOk) {
84
+ this.pluginStart();
85
+ this.pluginStop();
86
+ this.pluginCompile();
87
+ this.pluginEmit();
88
+ this.pluginDone();
89
+ }
90
+ else {
91
+ if (!tsconfigOk) {
92
+ throw new Error('Cannot find "' + this.tsconfigPath + '" file. Please check webpack and ForkTsCheckerWebpackPlugin configuration. \n' +
93
+ 'Possible errors: \n' +
94
+ ' - wrong `context` directory in webpack configuration' +
95
+ ' (if `tsconfig` is not set or is a relative path in fork plugin configuration)\n' +
96
+ ' - wrong `tsconfig` path in fork plugin configuration' +
97
+ ' (should be a relative or absolute path)');
98
+ }
99
+ if (!tslintOk) {
100
+ throw new Error('Cannot find "' + this.tslintPath + '" file. Please check webpack and ForkTsCheckerWebpackPlugin configuration. \n' +
101
+ 'Possible errors: \n' +
102
+ ' - wrong `context` directory in webpack configuration' +
103
+ ' (if `tslint` is not set or is a relative path in fork plugin configuration)\n' +
104
+ ' - wrong `tslint` path in fork plugin configuration' +
105
+ ' (should be a relative or absolute path)\n' +
106
+ ' - `tslint` path is not set to false in fork plugin configuration' +
107
+ ' (if you want to disable tslint support)');
108
+ }
109
+ }
110
+ };
111
+ ForkTsCheckerWebpackPlugin.prototype.computeContextPath = function (filePath) {
112
+ return path.isAbsolute(filePath) ? filePath : path.resolve(this.compiler.options.context, filePath);
113
+ };
114
+ ForkTsCheckerWebpackPlugin.prototype.pluginStart = function () {
115
+ var _this = this;
116
+ this.compiler.plugin('run', function (_compiler, callback) {
117
+ _this.isWatching = false;
118
+ callback();
119
+ });
120
+ this.compiler.plugin('watch-run', function (_watching, callback) {
121
+ _this.isWatching = true;
122
+ callback();
123
+ });
124
+ };
125
+ ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
126
+ var _this = this;
127
+ this.compiler.plugin('watch-close', function () {
128
+ _this.killService();
129
+ });
130
+ this.compiler.plugin('done', function () {
131
+ if (!_this.isWatching) {
132
+ _this.killService();
133
+ }
134
+ });
135
+ process.on('exit', function () {
136
+ _this.killService();
137
+ });
138
+ };
139
+ ForkTsCheckerWebpackPlugin.prototype.pluginCompile = function () {
140
+ var _this = this;
141
+ this.compiler.plugin('compile', function () {
142
+ _this.compiler.applyPluginsAsync('fork-ts-checker-service-before-start', function () {
143
+ if (_this.cancellationToken) {
144
+ // request cancellation if there is not finished job
145
+ _this.cancellationToken.requestCancellation();
146
+ _this.compiler.applyPlugins('fork-ts-checker-cancel', _this.cancellationToken);
147
+ }
148
+ _this.checkDone = false;
149
+ _this.compilationDone = false;
150
+ _this.started = process.hrtime();
151
+ // create new token for current job
152
+ _this.cancellationToken = new CancellationToken(undefined, undefined);
153
+ if (!_this.service || !_this.service.connected) {
154
+ _this.spawnService();
155
+ }
156
+ try {
157
+ _this.service.send(_this.cancellationToken);
158
+ }
159
+ catch (error) {
160
+ if (!_this.silent && _this.logger) {
161
+ _this.logger.error(_this.colors.red('Cannot start checker service: ' + (error ? error.toString() : 'Unknown error')));
162
+ }
163
+ _this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
164
+ }
165
+ });
166
+ });
167
+ };
168
+ ForkTsCheckerWebpackPlugin.prototype.pluginEmit = function () {
169
+ var _this = this;
170
+ this.compiler.plugin('emit', function (compilation, callback) {
171
+ if (_this.isWatching && _this.async) {
172
+ callback();
173
+ return;
174
+ }
175
+ _this.emitCallback = _this.createEmitCallback(compilation, callback);
176
+ if (_this.checkDone) {
177
+ _this.emitCallback();
178
+ }
179
+ _this.compilationDone = true;
180
+ });
181
+ };
182
+ ForkTsCheckerWebpackPlugin.prototype.pluginDone = function () {
183
+ var _this = this;
184
+ this.compiler.plugin('done', function () {
185
+ if (!_this.isWatching || !_this.async) {
186
+ return;
187
+ }
188
+ if (_this.checkDone) {
189
+ _this.doneCallback();
190
+ }
191
+ else {
192
+ if (_this.compiler) {
193
+ _this.compiler.applyPlugins('fork-ts-checker-waiting', _this.tslint !== false);
194
+ }
195
+ if (!_this.silent && _this.logger) {
196
+ _this.logger.info(_this.tslint
197
+ ? 'Type checking and linting in progress...'
198
+ : 'Type checking in progress...');
199
+ }
200
+ }
201
+ _this.compilationDone = true;
202
+ });
203
+ };
204
+ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
205
+ var _this = this;
206
+ this.service = childProcess.fork(path.resolve(__dirname, this.workersNumber > 1 ? './cluster.js' : './service.js'), [], {
207
+ execArgv: this.workersNumber > 1 ? [] : ['--max-old-space-size=' + this.memoryLimit],
208
+ env: Object.assign({}, process.env, {
209
+ TSCONFIG: this.tsconfigPath,
210
+ TSLINT: this.tslintPath || '',
211
+ WATCH: this.isWatching ? this.watchPaths.join('|') : '',
212
+ WORK_DIVISION: Math.max(1, this.workersNumber),
213
+ MEMORY_LIMIT: this.memoryLimit,
214
+ CHECK_SYNTACTIC_ERRORS: this.checkSyntacticErrors
215
+ }),
216
+ stdio: ['inherit', 'inherit', 'inherit', 'ipc']
217
+ });
218
+ this.compiler.applyPlugins('fork-ts-checker-service-start', this.tsconfigPath, this.tslintPath, this.watchPaths, this.workersNumber, this.memoryLimit);
219
+ if (!this.silent && this.logger) {
220
+ this.logger.info('Starting type checking' + (this.tslint ? ' and linting' : '') + ' service...');
221
+ this.logger.info('Using ' + this.colors.bold(this.workersNumber === 1 ? '1 worker' : this.workersNumber + ' workers') +
222
+ ' with ' + this.colors.bold(this.memoryLimit + 'MB') + ' memory limit');
223
+ if (this.watchPaths.length && this.isWatching) {
224
+ this.logger.info('Watching:' +
225
+ (this.watchPaths.length > 1 ? '\n' : ' ') +
226
+ this.watchPaths
227
+ .map(function (wpath) { return _this.colors.grey(wpath); })
228
+ .join('\n'));
229
+ }
230
+ }
231
+ this.service.on('message', function (message) { return _this.handleServiceMessage(message); });
232
+ this.service.on('exit', function (code, signal) { return _this.handleServiceExit(code, signal); });
233
+ };
234
+ ForkTsCheckerWebpackPlugin.prototype.killService = function () {
235
+ if (this.service) {
236
+ try {
237
+ if (this.cancellationToken) {
238
+ this.cancellationToken.cleanupCancellation();
239
+ }
240
+ this.service.kill();
241
+ this.service = undefined;
242
+ }
243
+ catch (e) {
244
+ if (this.logger && !this.silent) {
245
+ this.logger.error(e);
246
+ }
247
+ }
248
+ }
249
+ };
250
+ ForkTsCheckerWebpackPlugin.prototype.handleServiceMessage = function (message) {
251
+ var _this = this;
252
+ if (this.cancellationToken) {
253
+ this.cancellationToken.cleanupCancellation();
254
+ // job is done - nothing to cancel
255
+ this.cancellationToken = undefined;
256
+ }
257
+ this.checkDone = true;
258
+ this.elapsed = process.hrtime(this.started);
259
+ this.diagnostics = message.diagnostics.map(NormalizedMessage.createFromJSON);
260
+ this.lints = message.lints.map(NormalizedMessage.createFromJSON);
261
+ if (this.ignoreDiagnostics.length) {
262
+ this.diagnostics = this.diagnostics.filter(function (diagnostic) {
263
+ return _this.ignoreDiagnostics.indexOf(parseInt(diagnostic.getCode(), 10)) === -1;
264
+ });
265
+ }
266
+ if (this.ignoreLints.length) {
267
+ this.lints = this.lints.filter(function (lint) {
268
+ return _this.ignoreLints.indexOf(lint.getCode()) === -1;
269
+ });
270
+ }
271
+ this.compiler.applyPlugins('fork-ts-checker-receive', this.diagnostics, this.lints);
272
+ if (this.compilationDone) {
273
+ (this.isWatching && this.async) ? this.doneCallback() : this.emitCallback();
274
+ }
275
+ };
276
+ ForkTsCheckerWebpackPlugin.prototype.handleServiceExit = function (_code, signal) {
277
+ if (signal === 'SIGABRT') {
278
+ // probably out of memory :/
279
+ if (this.compiler) {
280
+ this.compiler.applyPlugins('fork-ts-checker-service-out-of-memory');
281
+ }
282
+ if (!this.silent && this.logger) {
283
+ this.logger.error(this.colors.red('Type checking and linting aborted - probably out of memory. ' +
284
+ 'Check `memoryLimit` option in ForkTsCheckerWebpackPlugin configuration.'));
285
+ }
286
+ }
287
+ };
288
+ ForkTsCheckerWebpackPlugin.prototype.createEmitCallback = function (compilation, callback) {
289
+ return function emitCallback() {
290
+ var elapsed = Math.round(this.elapsed[0] * 1E9 + this.elapsed[1]);
291
+ this.compiler.applyPlugins('fork-ts-checker-emit', this.diagnostics, this.lints, elapsed);
292
+ this.diagnostics.concat(this.lints).forEach(function (message) {
293
+ // webpack message format
294
+ var formatted = {
295
+ rawMessage: (message.getSeverity().toUpperCase() + ' ' + message.getFormattedCode() + ': ' +
296
+ message.getContent()),
297
+ message: '(' + message.getLine() + ',' + message.getCharacter() + '): ' + message.getContent(),
298
+ location: {
299
+ line: message.getLine(),
300
+ character: message.getCharacter()
301
+ },
302
+ file: message.getFile()
303
+ };
304
+ if (message.isWarningSeverity()) {
305
+ compilation.warnings.push(formatted);
306
+ }
307
+ else {
308
+ compilation.errors.push(formatted);
309
+ }
310
+ });
311
+ callback();
312
+ };
313
+ };
314
+ ForkTsCheckerWebpackPlugin.prototype.createNoopEmitCallback = function () {
315
+ // tslint:disable-next-line:no-empty
316
+ return function noopEmitCallback() { };
317
+ };
318
+ ForkTsCheckerWebpackPlugin.prototype.createDoneCallback = function () {
319
+ return function doneCallback() {
320
+ var _this = this;
321
+ var elapsed = Math.round(this.elapsed[0] * 1E9 + this.elapsed[1]);
322
+ if (this.compiler) {
323
+ this.compiler.applyPlugins('fork-ts-checker-done', this.diagnostics, this.lints, elapsed);
324
+ }
325
+ if (!this.silent && this.logger) {
326
+ if (this.diagnostics.length || this.lints.length) {
327
+ (this.lints || []).concat(this.diagnostics).forEach(function (message) {
328
+ var formattedMessage = _this.formatter(message, _this.useColors);
329
+ message.isWarningSeverity() ? _this.logger.warn(formattedMessage) : _this.logger.error(formattedMessage);
330
+ });
331
+ }
332
+ if (!this.diagnostics.length) {
333
+ this.logger.info(this.colors.green('No type errors found'));
334
+ }
335
+ if (this.tslint && !this.lints.length) {
336
+ this.logger.info(this.colors.green('No lint errors found'));
337
+ }
338
+ this.logger.info('Version: typescript ' + this.colors.bold(this.typescriptVersion) +
339
+ (this.tslint ? ', tslint ' + this.colors.bold(this.tslintVersion) : ''));
340
+ this.logger.info('Time: ' + this.colors.bold(Math.round(elapsed / 1E6).toString()) + 'ms');
341
+ }
342
+ };
343
+ };
344
+ ForkTsCheckerWebpackPlugin.DEFAULT_MEMORY_LIMIT = 2048;
345
+ ForkTsCheckerWebpackPlugin.ONE_CPU = 1;
346
+ ForkTsCheckerWebpackPlugin.ALL_CPUS = os.cpus().length;
347
+ ForkTsCheckerWebpackPlugin.ONE_CPU_FREE = Math.max(1, ForkTsCheckerWebpackPlugin.ALL_CPUS - 1);
348
+ ForkTsCheckerWebpackPlugin.TWO_CPUS_FREE = Math.max(1, ForkTsCheckerWebpackPlugin.ALL_CPUS - 2);
349
+ return ForkTsCheckerWebpackPlugin;
350
+ }());
351
+ module.exports = ForkTsCheckerWebpackPlugin;