Skip to content

Commit f4bdc3d

Browse files
authored
Merge pull request #12 from Realytics/feature/v0.2.0
v0.2.0
2 parents 4311b8d + 7075f7f commit f4bdc3d

16 files changed

+980
-95
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ npm-debug.log*
66
# Coverage directory used by tools like istanbul
77
coverage
88

9+
# Tmp directory for integration test
10+
tmp
11+
912
# Dependency directories
1013
node_modules
1114
jspm_packages

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## v0.2.0
2+
* tsconfig.json and tslint.json path are not printed anymore.
3+
* `watch` option is not used on 'build' mode
4+
* Handle case with no options object (`new ForkTsCheckerWebpacPlugin()`)
5+
* Basic integration tests (along units)
6+
* **Breaking changes**:
7+
* tslint is not enabled by default - you have to set `tslint: true` or `tslint: './path/to/tslint.json'` to enable it.
8+
* `blockEmit` option is removed - it choose automatically - blocks always on 'build' mode, never on 'watch' mode.
9+
110
## v0.1.5
211
* Disable tslint if module is not installed and no tslint path is passed
312
* Improve README.md

README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ var webpackConfig = {
3434
]
3535
},
3636
plugins: [
37-
new ForkTsCheckerWebpackPlugin({
38-
watch: './src' // optional but improves performance (less stat calls)
39-
})
37+
new ForkTsCheckerWebpackPlugin()
4038
]
4139
};
4240
```
@@ -59,24 +57,20 @@ to compile files (which traverses dependency graph during compilation) - we have
5957
To debug typescript's modules resolution, you can use `tsc --traceResolution` command.
6058

6159
## TSLint
62-
If you have installed [tslint](https://palantir.github.io/tslint), it's enabled by default. To disable it, set `tslint: false` in plugin
63-
options. We recommend changing `defaultSeverity` to the `"warning"` in `tslint.json` file. It helps to distinguish lints from typescript's
64-
diagnostics.
60+
If you have installed [tslint](https://palantir.github.io/tslint), you can enable it by setting `tslint: true` or
61+
`tslint: './path/to/tslint.json'`. We recommend changing `defaultSeverity` to a `"warning"` in `tslint.json` file.
62+
It helps to distinguish lints from typescript's diagnostics.
6563

6664
## Options
6765
* **tsconfig** `string`:
68-
Path to tsconfig.json file. Default: `path.resolve(compiler.options.context, './tsconfig.json')`
66+
Path to *tsconfig.json* file. Default: `path.resolve(compiler.options.context, './tsconfig.json')`.
6967

70-
* **tslint** `string | false`:
71-
Path to tslint.json file. If `false`, disables tslint. Default: `path.resolve(compiler.options.context, './tslint.json')`
68+
* **tslint** `string | true`:
69+
Path to *tslint.json* file or `true`. If `true`, uses `path.resolve(compiler.options.context, './tslint.json')`. Default: `undefined`.
7270

7371
* **watch** `string | string[]`:
7472
Directories or files to watch by service. Not necessary but improves performance (reduces number of `fs.stat` calls).
7573

76-
* **blockEmit** `boolean`:
77-
If `true`, plugin will block emit until check will be done. It's good setting for ci/production build because webpack will return code != 0
78-
if there are type/lint errors. Default: `false`.
79-
8074
* **ignoreDiagnostics** `number[]`:
8175
List of typescript diagnostic codes to ignore.
8276

@@ -118,8 +112,8 @@ This plugin provides some custom webpack hooks (all are sync):
118112
|`fork-ts-checker-service-start-error` | Cannot start service | `error` |
119113
|`fork-ts-checker-service-out-of-memory`| Service is out of memory | - |
120114
|`fork-ts-checker-receive`| Plugin receives diagnostics and lints from service | `diagnostics`, `lints` |
121-
|`fork-ts-checker-emit`| Service will add errors and warnings to webpack compilation (`blockEmit: true`) | `diagnostics`, `lints`, `elapsed` |
122-
|`fork-ts-checker-done`| Service finished type checking and webpack finished compilation (`blockEmit: false`) | `diagnostics`, `lints`, `elapsed` |
115+
|`fork-ts-checker-emit`| Service will add errors and warnings to webpack compilation ('build' mode) | `diagnostics`, `lints`, `elapsed` |
116+
|`fork-ts-checker-done`| Service finished type checking and webpack finished compilation ('watch' mode) | `diagnostics`, `lints`, `elapsed` |
123117

124118
## License
125119
MIT

lib/index.js

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,30 @@ var NormalizedMessage = require('./NormalizedMessage');
1616
* Options description in README.md
1717
*/
1818
function ForkTsCheckerWebpackPlugin (options) {
19-
var tslintInstalled;
20-
21-
try {
22-
require.resolve('tslint');
23-
tslintInstalled = true;
24-
} catch (error) {
25-
tslintInstalled = false;
26-
}
27-
19+
options = options || {};
2820
this.options = Object.assign({}, options);
21+
2922
this.tsconfig = options.tsconfig || './tsconfig.json';
30-
this.tslint = options.tslint === false ? false : (options.tslint || (tslintInstalled ? './tslint.json' : false));
23+
this.tslint = options.tslint ? isString(options.tslint) ? options.tslint : './tslint.json' : undefined;
3124
this.watch = isString(options.watch) ? [options.watch] : options.watch || [];
32-
this.blockEmit = !!options.blockEmit;
3325
this.ignoreDiagnostics = options.ignoreDiagnostics || [];
3426
this.ignoreLints = options.ignoreLints || [];
3527
this.logger = options.logger || console;
3628
this.silent = !!options.silent;
3729
this.workersNumber = options.workers || ForkTsCheckerWebpackPlugin.ONE_CPU;
3830
this.memoryLimit = options.memoryLimit || ForkTsCheckerWebpackPlugin.DEFAULT_MEMORY_LIMIT;
31+
this.colors = new chalk.constructor({ enabled: options.colors === undefined ? true : !!options.colors });
3932

4033
this.tsconfigPath = undefined;
4134
this.tslintPath = undefined;
4235
this.watchPaths = [];
43-
this.isWatching = false;
4436
this.compiler = undefined;
45-
this.colors = new chalk.constructor({
46-
enabled: options.colors === undefined ? true : !!options.colors
47-
});
37+
4838
this.started = undefined;
4939
this.elapsed = undefined;
5040
this.cancellationToken = undefined;
41+
42+
this.isWatching = false;
5143
this.checkDone = false;
5244
this.compilationDone = false;
5345
this.diagnostics = [];
@@ -90,12 +82,8 @@ ForkTsCheckerWebpackPlugin.prototype.apply = function (compiler) {
9082
this.pluginStart();
9183
this.pluginStop();
9284
this.pluginCompile();
93-
94-
if (this.blockEmit) {
95-
this.pluginAfterEmit();
96-
} else {
97-
this.pluginDone();
98-
}
85+
this.pluginEmit();
86+
this.pluginDone();
9987
} else {
10088
if (!tsconfigOk) {
10189
throw new Error(
@@ -123,8 +111,7 @@ ForkTsCheckerWebpackPlugin.prototype.apply = function (compiler) {
123111
};
124112

125113
ForkTsCheckerWebpackPlugin.prototype.computeContextPath = function (filePath) {
126-
return path.isAbsolute(filePath)
127-
? filePath : path.resolve(this.compiler.options.context, filePath);
114+
return path.isAbsolute(filePath) ? filePath : path.resolve(this.compiler.options.context, filePath);
128115
};
129116

130117
ForkTsCheckerWebpackPlugin.prototype.pluginStart = function () {
@@ -141,11 +128,13 @@ ForkTsCheckerWebpackPlugin.prototype.pluginStart = function () {
141128

142129
ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
143130
this.compiler.plugin('done', function () {
144-
this.killService();
131+
if (!this.isWatching) {
132+
this.killService();
133+
}
145134
}.bind(this));
146135

147136
process.on('exit', function () {
148-
this.killService(true);
137+
this.killService();
149138
}.bind(this));
150139
};
151140

@@ -170,7 +159,7 @@ ForkTsCheckerWebpackPlugin.prototype.pluginCompile = function () {
170159
try {
171160
this.service.send(this.cancellationToken);
172161
} catch (error) {
173-
if (!this.options.silent && this.logger) {
162+
if (!this.silent && this.logger) {
174163
this.logger.error(this.colors.red('Cannot start checker service: ' + (error ? error.toString() : 'Unknown error')));
175164
}
176165

@@ -179,8 +168,13 @@ ForkTsCheckerWebpackPlugin.prototype.pluginCompile = function () {
179168
}.bind(this));
180169
};
181170

182-
ForkTsCheckerWebpackPlugin.prototype.pluginAfterEmit = function () {
183-
this.compiler.plugin('after-emit', function (compilation, callback) {
171+
ForkTsCheckerWebpackPlugin.prototype.pluginEmit = function () {
172+
this.compiler.plugin('emit', function (compilation, callback) {
173+
if (this.isWatching) {
174+
callback();
175+
return;
176+
}
177+
184178
this.emitCallback = this.createEmitCallback(compilation, callback);
185179

186180
if (this.checkDone) {
@@ -193,6 +187,10 @@ ForkTsCheckerWebpackPlugin.prototype.pluginAfterEmit = function () {
193187

194188
ForkTsCheckerWebpackPlugin.prototype.pluginDone = function () {
195189
this.compiler.plugin('done', function () {
190+
if (!this.isWatching) {
191+
return;
192+
}
193+
196194
if (this.checkDone) {
197195
this.doneCallback();
198196
} else {
@@ -224,13 +222,14 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
224222
env: {
225223
TSCONFIG: this.tsconfigPath,
226224
TSLINT: this.tslintPath || '',
227-
WATCH: this.watchPaths.join('|'),
225+
WATCH: this.isWatching ? this.watchPaths.join('|') : '',
228226
WORK_DIVISION: Math.max(1, this.workersNumber),
229227
MEMORY_LIMIT: this.memoryLimit
230228
},
231229
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
232230
}
233231
);
232+
234233
this.compiler.applyPlugins(
235234
'fork-ts-checker-service-start',
236235
this.tsconfigPath,
@@ -241,22 +240,12 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
241240
);
242241

243242
if (!this.silent && this.logger) {
244-
var message = 'Starting type checking' + (this.tslint ? ' and linting' : '') + ' service...';
245-
var performance = (
243+
this.logger.info('Starting type checking' + (this.tslint ? ' and linting' : '') + ' service...');
244+
this.logger.info(
246245
'Using ' + this.colors.bold(this.workersNumber === 1 ? '1 worker' : this.workersNumber + ' workers') +
247246
' with ' + this.colors.bold(this.memoryLimit + 'MB') + ' memory limit'
248247
);
249-
var lines = [message, performance];
250-
if (!this.options.tsconfig) {
251-
// auto-detect tsconfig path - print to the user to be sure that it's proper file
252-
lines.push(this.colors.grey(this.tsconfigPath));
253-
}
254-
if (this.tslint && !this.options.tslint) {
255-
// auto-detect tslint path - print to the user to be sure that it's proper file
256-
lines.push(this.colors.grey(this.tslintPath));
257-
}
258248

259-
this.logger.info(lines.join('\n'));
260249
if (this.watchPaths.length && this.isWatching) {
261250
this.logger.info(
262251
'Watching:' +
@@ -272,11 +261,11 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
272261
this.service.on('exit', this.handleServiceExit.bind(this));
273262
};
274263

275-
ForkTsCheckerWebpackPlugin.prototype.killService = function (force) {
276-
if ((force || !this.isWatching) && this.service) {
264+
ForkTsCheckerWebpackPlugin.prototype.killService = function () {
265+
if (this.service) {
277266
try {
278267
if (this.cancellationToken) {
279-
this.cancellationToken.requestCancellation();
268+
this.cancellationToken.cleanupCancellation();
280269
}
281270

282271
this.service.kill();
@@ -316,7 +305,7 @@ ForkTsCheckerWebpackPlugin.prototype.handleServiceMessage = function (message) {
316305
this.compiler.applyPlugins('fork-ts-checker-receive', this.diagnostics, this.lints);
317306

318307
if (this.compilationDone) {
319-
this.blockEmit ? this.emitCallback() : this.doneCallback();
308+
this.isWatching ? this.doneCallback() : this.emitCallback();
320309
}
321310
};
322311

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"name": "fork-ts-checker-webpack-plugin",
3-
"version": "0.1.5",
3+
"version": "0.2.0",
44
"description": "Runs typescript type checker and linter on separate process.",
55
"main": "lib/index.js",
66
"scripts": {
7-
"test": "NODE_ENV=test node_modules/.bin/mocha -R spec",
8-
"test:watch": "NODE_ENV=test node_modules/.bin/mocha -R spec --watch",
9-
"test:cover": "rimraf coverage && node_modules/.bin/istanbul cover -root lib --include-all-sources node_modules/.bin/_mocha -- -R spec",
7+
"test:unit": "NODE_ENV=test node_modules/.bin/mocha -R spec ./test/unit",
8+
"test:integration": "NODE_ENV=test node_modules/.bin/mocha -R spec ./test/integration && rimraf tmp",
9+
"test": "npm run test:unit && npm run test:integration",
10+
"test:watch": "NODE_ENV=test node_modules/.bin/mocha -R spec --watch ./test/unit",
11+
"test:coverage": "rimraf coverage && node_modules/.bin/istanbul cover -root lib --include-all-sources node_modules/.bin/_mocha -- -R spec ./test/unit ./test/integration",
1012
"lint": "node node_modules/.bin/eslint ./lib ./test",
1113
"lint:fix": "node node_modules/.bin/eslint ./lib ./test --fix"
1214
},
@@ -43,7 +45,10 @@
4345
"mock-require": "^2.0.2",
4446
"rimraf": "^2.5.4",
4547
"sinon": "^2.3.1",
46-
"typescript": "^2.1.0"
48+
"ts-loader": "^2.1.0",
49+
"tslint": "^5.0.0",
50+
"typescript": "^2.1.0",
51+
"webpack": "^2.0.0"
4752
},
4853
"peerDependencies": {
4954
"typescript": "^2.1.0",

0 commit comments

Comments
 (0)