Skip to content

Commit b4322b1

Browse files
authored
Merge pull request #44 from dannsam/issue_40_allow_delaying_service_start
Adding `fork-ts-checker-service-before-start` plugin to allow delaying service-start
2 parents fc4d9bd + 6d1b29c commit b4322b1

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ This plugin provides some custom webpack hooks (all are sync):
127127
|------------|-------------|--------|
128128
|`fork-ts-checker-cancel`| Cancellation has been requested | `cancellationToken` |
129129
|`fork-ts-checker-waiting`| Waiting for results | `hasTsLint` |
130+
|`fork-ts-checker-service-before-start`| Async plugin that can be used for delaying `fork-ts-checker-service-start` | - |
130131
|`fork-ts-checker-service-start`| Service will be started | `tsconfigPath`, `tslintPath`, `watchPaths`, `workersNumber`, `memoryLimit` |
131132
|`fork-ts-checker-service-start-error` | Cannot start service | `error` |
132133
|`fork-ts-checker-service-out-of-memory`| Service is out of memory | - |

lib/index.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -164,31 +164,33 @@ ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
164164

165165
ForkTsCheckerWebpackPlugin.prototype.pluginCompile = function () {
166166
this.compiler.plugin('compile', function () {
167-
if (this.cancellationToken) {
168-
// request cancellation if there is not finished job
169-
this.cancellationToken.requestCancellation();
170-
this.compiler.applyPlugins('fork-ts-checker-cancel', this.cancellationToken);
171-
}
172-
this.checkDone = false;
173-
this.compilationDone = false;
174-
175-
this.started = process.hrtime();
167+
this.compiler.applyPluginsAsync('fork-ts-checker-service-before-start', function() {
168+
if (this.cancellationToken) {
169+
// request cancellation if there is not finished job
170+
this.cancellationToken.requestCancellation();
171+
this.compiler.applyPlugins('fork-ts-checker-cancel', this.cancellationToken);
172+
}
173+
this.checkDone = false;
174+
this.compilationDone = false;
176175

177-
// create new token for current job
178-
this.cancellationToken = new CancellationToken();
179-
if (!this.service || !this.service.connected) {
180-
this.spawnService();
181-
}
176+
this.started = process.hrtime();
182177

183-
try {
184-
this.service.send(this.cancellationToken);
185-
} catch (error) {
186-
if (!this.silent && this.logger) {
187-
this.logger.error(this.colors.red('Cannot start checker service: ' + (error ? error.toString() : 'Unknown error')));
178+
// create new token for current job
179+
this.cancellationToken = new CancellationToken();
180+
if (!this.service || !this.service.connected) {
181+
this.spawnService();
188182
}
189183

190-
this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
191-
}
184+
try {
185+
this.service.send(this.cancellationToken);
186+
} catch (error) {
187+
if (!this.silent && this.logger) {
188+
this.logger.error(this.colors.red('Cannot start checker service: ' + (error ? error.toString() : 'Unknown error')));
189+
}
190+
191+
this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
192+
}
193+
}.bind(this));
192194
}.bind(this));
193195
};
194196

test/integration/index.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,26 @@ describe('[INTEGRATION] index', function () {
187187
}).to.not.throw.error;
188188
});
189189

190+
it('should allow delaying service-start', function (callback) {
191+
var compiler = createCompiler();
192+
var delayed = false;
193+
194+
compiler.plugin('fork-ts-checker-service-before-start', function (cb) {
195+
setTimeout(function () {
196+
delayed = true;
197+
198+
cb();
199+
}, 0);
200+
});
201+
202+
compiler.plugin('fork-ts-checker-service-start', function () {
203+
expect(delayed).to.be.true;
204+
callback();
205+
});
206+
207+
compiler.run(function () {});
208+
});
209+
190210
it('should not find syntactic errors when checkSyntacticErrors is false', function (callback) {
191211
var compiler = createCompiler({}, true);
192212

0 commit comments

Comments
 (0)