Skip to content

Commit 053f218

Browse files
author
dannsam
committed
Adding fork-ts-checker-service-before-start plugin to allow delaying service-start
1 parent 46fe421 commit 053f218

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ This plugin provides some custom webpack hooks (all are sync):
124124
|------------|-------------|--------|
125125
|`fork-ts-checker-cancel`| Cancellation has been requested | `cancellationToken` |
126126
|`fork-ts-checker-waiting`| Waiting for results | `hasTsLint` |
127+
|`fork-ts-checker-service-before-start`| Async plugin that can be used for delaying `fork-ts-checker-service-start` | - |
127128
|`fork-ts-checker-service-start`| Service will be started | `tsconfigPath`, `tslintPath`, `watchPaths`, `workersNumber`, `memoryLimit` |
128129
|`fork-ts-checker-service-start-error` | Cannot start service | `error` |
129130
|`fork-ts-checker-service-out-of-memory`| Service is out of memory | - |

lib/index.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,31 +163,33 @@ ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
163163

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

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

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

189-
this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
190-
}
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')));
188+
}
189+
190+
this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
191+
}
192+
}.bind(this));
191193
}.bind(this));
192194
};
193195

test/integration/index.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,24 @@ describe('[INTEGRATION] index', function () {
185185
createCompiler({ tslint: true });
186186
}).to.not.throw.error;
187187
});
188+
189+
it('should allow delaying service-start', function (callback) {
190+
var compiler = createCompiler();
191+
var delayed = false;
192+
193+
compiler.plugin('fork-ts-checker-service-before-start', function (cb) {
194+
setTimeout(function () {
195+
delayed = true;
196+
197+
cb();
198+
}, 0);
199+
});
200+
201+
compiler.plugin('fork-ts-checker-service-start', function () {
202+
expect(delayed).to.be.true;
203+
callback();
204+
});
205+
206+
compiler.run(function () {});
207+
});
188208
});

0 commit comments

Comments
 (0)