Skip to content

Commit

Permalink
Merge pull request #12 from Realytics/feature/v0.2.0
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
piotr-oles committed Jun 2, 2017
2 parents 4311b8d + 7075f7f commit f4bdc3d
Show file tree
Hide file tree
Showing 16 changed files with 980 additions and 95 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ npm-debug.log*
# Coverage directory used by tools like istanbul
coverage

# Tmp directory for integration test
tmp

# Dependency directories
node_modules
jspm_packages
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v0.2.0
* tsconfig.json and tslint.json path are not printed anymore.
* `watch` option is not used on 'build' mode
* Handle case with no options object (`new ForkTsCheckerWebpacPlugin()`)
* Basic integration tests (along units)
* **Breaking changes**:
* tslint is not enabled by default - you have to set `tslint: true` or `tslint: './path/to/tslint.json'` to enable it.
* `blockEmit` option is removed - it choose automatically - blocks always on 'build' mode, never on 'watch' mode.

## v0.1.5
* Disable tslint if module is not installed and no tslint path is passed
* Improve README.md
Expand Down
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ var webpackConfig = {
]
},
plugins: [
new ForkTsCheckerWebpackPlugin({
watch: './src' // optional but improves performance (less stat calls)
})
new ForkTsCheckerWebpackPlugin()
]
};
```
Expand All @@ -59,24 +57,20 @@ to compile files (which traverses dependency graph during compilation) - we have
To debug typescript's modules resolution, you can use `tsc --traceResolution` command.

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

## Options
* **tsconfig** `string`:
Path to tsconfig.json file. Default: `path.resolve(compiler.options.context, './tsconfig.json')`
Path to *tsconfig.json* file. Default: `path.resolve(compiler.options.context, './tsconfig.json')`.

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

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

* **blockEmit** `boolean`:
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
if there are type/lint errors. Default: `false`.

* **ignoreDiagnostics** `number[]`:
List of typescript diagnostic codes to ignore.

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

## License
MIT
79 changes: 34 additions & 45 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,30 @@ var NormalizedMessage = require('./NormalizedMessage');
* Options description in README.md
*/
function ForkTsCheckerWebpackPlugin (options) {
var tslintInstalled;

try {
require.resolve('tslint');
tslintInstalled = true;
} catch (error) {
tslintInstalled = false;
}

options = options || {};
this.options = Object.assign({}, options);

this.tsconfig = options.tsconfig || './tsconfig.json';
this.tslint = options.tslint === false ? false : (options.tslint || (tslintInstalled ? './tslint.json' : false));
this.tslint = options.tslint ? isString(options.tslint) ? options.tslint : './tslint.json' : undefined;
this.watch = isString(options.watch) ? [options.watch] : options.watch || [];
this.blockEmit = !!options.blockEmit;
this.ignoreDiagnostics = options.ignoreDiagnostics || [];
this.ignoreLints = options.ignoreLints || [];
this.logger = options.logger || console;
this.silent = !!options.silent;
this.workersNumber = options.workers || ForkTsCheckerWebpackPlugin.ONE_CPU;
this.memoryLimit = options.memoryLimit || ForkTsCheckerWebpackPlugin.DEFAULT_MEMORY_LIMIT;
this.colors = new chalk.constructor({ enabled: options.colors === undefined ? true : !!options.colors });

this.tsconfigPath = undefined;
this.tslintPath = undefined;
this.watchPaths = [];
this.isWatching = false;
this.compiler = undefined;
this.colors = new chalk.constructor({
enabled: options.colors === undefined ? true : !!options.colors
});

this.started = undefined;
this.elapsed = undefined;
this.cancellationToken = undefined;

this.isWatching = false;
this.checkDone = false;
this.compilationDone = false;
this.diagnostics = [];
Expand Down Expand Up @@ -90,12 +82,8 @@ ForkTsCheckerWebpackPlugin.prototype.apply = function (compiler) {
this.pluginStart();
this.pluginStop();
this.pluginCompile();

if (this.blockEmit) {
this.pluginAfterEmit();
} else {
this.pluginDone();
}
this.pluginEmit();
this.pluginDone();
} else {
if (!tsconfigOk) {
throw new Error(
Expand Down Expand Up @@ -123,8 +111,7 @@ ForkTsCheckerWebpackPlugin.prototype.apply = function (compiler) {
};

ForkTsCheckerWebpackPlugin.prototype.computeContextPath = function (filePath) {
return path.isAbsolute(filePath)
? filePath : path.resolve(this.compiler.options.context, filePath);
return path.isAbsolute(filePath) ? filePath : path.resolve(this.compiler.options.context, filePath);
};

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

ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
this.compiler.plugin('done', function () {
this.killService();
if (!this.isWatching) {
this.killService();
}
}.bind(this));

process.on('exit', function () {
this.killService(true);
this.killService();
}.bind(this));
};

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

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

ForkTsCheckerWebpackPlugin.prototype.pluginAfterEmit = function () {
this.compiler.plugin('after-emit', function (compilation, callback) {
ForkTsCheckerWebpackPlugin.prototype.pluginEmit = function () {
this.compiler.plugin('emit', function (compilation, callback) {
if (this.isWatching) {
callback();
return;
}

this.emitCallback = this.createEmitCallback(compilation, callback);

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

ForkTsCheckerWebpackPlugin.prototype.pluginDone = function () {
this.compiler.plugin('done', function () {
if (!this.isWatching) {
return;
}

if (this.checkDone) {
this.doneCallback();
} else {
Expand Down Expand Up @@ -224,13 +222,14 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
env: {
TSCONFIG: this.tsconfigPath,
TSLINT: this.tslintPath || '',
WATCH: this.watchPaths.join('|'),
WATCH: this.isWatching ? this.watchPaths.join('|') : '',
WORK_DIVISION: Math.max(1, this.workersNumber),
MEMORY_LIMIT: this.memoryLimit
},
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
}
);

this.compiler.applyPlugins(
'fork-ts-checker-service-start',
this.tsconfigPath,
Expand All @@ -241,22 +240,12 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
);

if (!this.silent && this.logger) {
var message = 'Starting type checking' + (this.tslint ? ' and linting' : '') + ' service...';
var performance = (
this.logger.info('Starting type checking' + (this.tslint ? ' and linting' : '') + ' service...');
this.logger.info(
'Using ' + this.colors.bold(this.workersNumber === 1 ? '1 worker' : this.workersNumber + ' workers') +
' with ' + this.colors.bold(this.memoryLimit + 'MB') + ' memory limit'
);
var lines = [message, performance];
if (!this.options.tsconfig) {
// auto-detect tsconfig path - print to the user to be sure that it's proper file
lines.push(this.colors.grey(this.tsconfigPath));
}
if (this.tslint && !this.options.tslint) {
// auto-detect tslint path - print to the user to be sure that it's proper file
lines.push(this.colors.grey(this.tslintPath));
}

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

ForkTsCheckerWebpackPlugin.prototype.killService = function (force) {
if ((force || !this.isWatching) && this.service) {
ForkTsCheckerWebpackPlugin.prototype.killService = function () {
if (this.service) {
try {
if (this.cancellationToken) {
this.cancellationToken.requestCancellation();
this.cancellationToken.cleanupCancellation();
}

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

if (this.compilationDone) {
this.blockEmit ? this.emitCallback() : this.doneCallback();
this.isWatching ? this.doneCallback() : this.emitCallback();
}
};

Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "fork-ts-checker-webpack-plugin",
"version": "0.1.5",
"version": "0.2.0",
"description": "Runs typescript type checker and linter on separate process.",
"main": "lib/index.js",
"scripts": {
"test": "NODE_ENV=test node_modules/.bin/mocha -R spec",
"test:watch": "NODE_ENV=test node_modules/.bin/mocha -R spec --watch",
"test:cover": "rimraf coverage && node_modules/.bin/istanbul cover -root lib --include-all-sources node_modules/.bin/_mocha -- -R spec",
"test:unit": "NODE_ENV=test node_modules/.bin/mocha -R spec ./test/unit",
"test:integration": "NODE_ENV=test node_modules/.bin/mocha -R spec ./test/integration && rimraf tmp",
"test": "npm run test:unit && npm run test:integration",
"test:watch": "NODE_ENV=test node_modules/.bin/mocha -R spec --watch ./test/unit",
"test:coverage": "rimraf coverage && node_modules/.bin/istanbul cover -root lib --include-all-sources node_modules/.bin/_mocha -- -R spec ./test/unit ./test/integration",
"lint": "node node_modules/.bin/eslint ./lib ./test",
"lint:fix": "node node_modules/.bin/eslint ./lib ./test --fix"
},
Expand Down Expand Up @@ -43,7 +45,10 @@
"mock-require": "^2.0.2",
"rimraf": "^2.5.4",
"sinon": "^2.3.1",
"typescript": "^2.1.0"
"ts-loader": "^2.1.0",
"tslint": "^5.0.0",
"typescript": "^2.1.0",
"webpack": "^2.0.0"
},
"peerDependencies": {
"typescript": "^2.1.0",
Expand Down

0 comments on commit f4bdc3d

Please sign in to comment.