Skip to content

Adding fork-ts-checker-service-before-start plugin to allow delaying service-start #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ This plugin provides some custom webpack hooks (all are sync):
|------------|-------------|--------|
|`fork-ts-checker-cancel`| Cancellation has been requested | `cancellationToken` |
|`fork-ts-checker-waiting`| Waiting for results | `hasTsLint` |
|`fork-ts-checker-service-before-start`| Async plugin that can be used for delaying `fork-ts-checker-service-start` | - |
|`fork-ts-checker-service-start`| Service will be started | `tsconfigPath`, `tslintPath`, `watchPaths`, `workersNumber`, `memoryLimit` |
|`fork-ts-checker-service-start-error` | Cannot start service | `error` |
|`fork-ts-checker-service-out-of-memory`| Service is out of memory | - |
Expand Down
44 changes: 23 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,31 +164,33 @@ ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {

ForkTsCheckerWebpackPlugin.prototype.pluginCompile = function () {
this.compiler.plugin('compile', function () {
if (this.cancellationToken) {
// request cancellation if there is not finished job
this.cancellationToken.requestCancellation();
this.compiler.applyPlugins('fork-ts-checker-cancel', this.cancellationToken);
}
this.checkDone = false;
this.compilationDone = false;

this.started = process.hrtime();
this.compiler.applyPluginsAsync('fork-ts-checker-service-before-start', function() {
if (this.cancellationToken) {
// request cancellation if there is not finished job
this.cancellationToken.requestCancellation();
this.compiler.applyPlugins('fork-ts-checker-cancel', this.cancellationToken);
}
this.checkDone = false;
this.compilationDone = false;

// create new token for current job
this.cancellationToken = new CancellationToken();
if (!this.service || !this.service.connected) {
this.spawnService();
}
this.started = process.hrtime();

try {
this.service.send(this.cancellationToken);
} catch (error) {
if (!this.silent && this.logger) {
this.logger.error(this.colors.red('Cannot start checker service: ' + (error ? error.toString() : 'Unknown error')));
// create new token for current job
this.cancellationToken = new CancellationToken();
if (!this.service || !this.service.connected) {
this.spawnService();
}

this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
}
try {
this.service.send(this.cancellationToken);
} catch (error) {
if (!this.silent && this.logger) {
this.logger.error(this.colors.red('Cannot start checker service: ' + (error ? error.toString() : 'Unknown error')));
}

this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
}
}.bind(this));
}.bind(this));
};

Expand Down
20 changes: 20 additions & 0 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ describe('[INTEGRATION] index', function () {
}).to.not.throw.error;
});

it('should allow delaying service-start', function (callback) {
var compiler = createCompiler();
var delayed = false;

compiler.plugin('fork-ts-checker-service-before-start', function (cb) {
setTimeout(function () {
delayed = true;

cb();
}, 0);
});

compiler.plugin('fork-ts-checker-service-start', function () {
expect(delayed).to.be.true;
callback();
});

compiler.run(function () {});
});

it('should not find syntactic errors when checkSyntacticErrors is false', function (callback) {
var compiler = createCompiler({}, true);

Expand Down