Skip to content

Commit b058c2d

Browse files
committed
Migrate to webpack 4
1 parent ff3ade3 commit b058c2d

File tree

5 files changed

+1646
-324
lines changed

5 files changed

+1646
-324
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@
6161
"mock-require": "^2.0.2",
6262
"rimraf": "^2.5.4",
6363
"sinon": "^2.3.1",
64-
"ts-loader": "^2.1.0",
64+
"ts-loader": "^4.0.0-beta.0",
6565
"tslint": "^5.0.0",
66-
"typescript": "^2.1.0",
66+
"typescript": "^2.6.2",
6767
"vue": "^2.5.9",
6868
"vue-class-component": "^6.1.1",
6969
"vue-loader": "^13.5.0",
7070
"vue-template-compiler": "^2.5.9",
71-
"webpack": "^3.0.0"
71+
"webpack": "^4.0.0-beta.0"
7272
},
7373
"peerDependencies": {
74+
"tslint": "^5.0.0",
7475
"typescript": "^2.1.0",
7576
"webpack": "^2.3.0 || ^3.0.0"
7677
},
@@ -84,6 +85,7 @@
8485
"lodash.startswith": "^4.2.1",
8586
"minimatch": "^3.0.4",
8687
"resolve": "^1.5.0",
88+
"tapable": "^1.0.0-beta.5",
8789
"vue-parser": "^1.1.5"
8890
}
8991
}

src/index.ts

Lines changed: 93 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ import createDefaultFormatter = require('./formatter/defaultFormatter');
1313
import createCodeframeFormatter = require('./formatter/codeframeFormatter');
1414
import Message from './Message';
1515

16+
const AsyncSeriesHook = require("tapable").AsyncSeriesHook;
17+
const SyncHook = require("tapable").SyncHook;
18+
19+
const checkerPluginName = 'fork-ts-checker-webpack-plugin';
20+
21+
const customHooks = {
22+
forkTsCheckerServiceBeforeStart: 'fork-ts-checker-service-before-start',
23+
forkTsCheckerCancel: 'fork-ts-checker-cancel',
24+
forkTsCheckerServiceStartError: 'fork-ts-checker-service-start-error',
25+
forkTsCheckerWaiting: 'fork-ts-checker-waiting',
26+
forkTsCheckerServiceStart: 'fork-ts-checker-service-start',
27+
forkTsCheckerReceive: 'fork-ts-checker-receive',
28+
forkTsCheckerServiceOutOfMemory: 'fork-ts-checker-service-out-of-memory',
29+
forkTsCheckerEmit: 'fork-ts-checker-emit',
30+
forkTsCheckerDone: 'fork-ts-checker-done'
31+
};
32+
1633
type Formatter = (message: NormalizedMessage, useColors: boolean) => string;
1734

1835
interface Options {
@@ -163,6 +180,7 @@ class ForkTsCheckerWebpackPlugin {
163180
}
164181

165182
if (tsconfigOk && tslintOk) {
183+
this.registerCustomHooks();
166184
this.pluginStart();
167185
this.pluginStop();
168186
this.pluginCompile();
@@ -199,40 +217,87 @@ class ForkTsCheckerWebpackPlugin {
199217
}
200218

201219
pluginStart() {
202-
this.compiler.plugin('run', (_compiler: webpack.Compiler, callback: () => void) => {
203-
this.isWatching = false;
204-
callback();
205-
});
220+
this.compiler.hooks.run.tapAsync(checkerPluginName,
221+
(_compiler: webpack.Compiler, callback: () => void) => {
222+
this.isWatching = false;
223+
callback();
224+
});
206225

207-
this.compiler.plugin('watch-run', (_watching: webpack.Watching, callback: () => void) => {
208-
this.isWatching = true;
209-
callback();
210-
});
226+
this.compiler.hooks.watchRun.tapAsync(checkerPluginName,
227+
(_compiler: webpack.Compiler, callback: () => void) => {
228+
this.isWatching = true;
229+
callback();
230+
});
211231
}
212232

213233
pluginStop() {
214-
this.compiler.plugin('watch-close', () => {
215-
this.killService();
216-
});
217-
218-
this.compiler.plugin('done', () => {
219-
if (!this.isWatching) {
234+
this.compiler.hooks.watchClose.tap(checkerPluginName,
235+
() => {
220236
this.killService();
221-
}
222-
});
237+
});
238+
239+
this.compiler.hooks.done.tap(checkerPluginName,
240+
(_stats: webpack.Stats) => {
241+
if (!this.isWatching) {
242+
this.killService();
243+
}
244+
});
223245

224246
process.on('exit', () => {
225247
this.killService();
226248
});
227249
}
228250

251+
registerCustomHooks() {
252+
if (this.compiler.hooks.forkTsCheckerServiceBeforeStart
253+
|| this.compiler.hooks.forkTsCheckerCancel
254+
|| this.compiler.hooks.forkTsCheckerServiceStartError
255+
|| this.compiler.hooks.forkTsCheckerWaiting
256+
|| this.compiler.hooks.forkTsCheckerServiceStart
257+
|| this.compiler.hooks.forkTsCheckerReceive
258+
|| this.compiler.hooks.forkTsCheckerServiceOutOfMemory
259+
|| this.compiler.hooks.forkTsCheckerDone
260+
|| this.compiler.hooks.forkTsCheckerEmit) {
261+
throw new Error('fork-ts-checker-webpack-plugin hooks are already in use');
262+
}
263+
this.compiler.hooks.forkTsCheckerServiceBeforeStart = new AsyncSeriesHook([]);
264+
265+
this.compiler.hooks.forkTsCheckerCancel = new SyncHook([]);
266+
this.compiler.hooks.forkTsCheckerServiceStartError = new SyncHook([]);
267+
this.compiler.hooks.forkTsCheckerWaiting = new SyncHook([]);
268+
this.compiler.hooks.forkTsCheckerServiceStart = new SyncHook([]);
269+
this.compiler.hooks.forkTsCheckerReceive = new SyncHook([]);
270+
this.compiler.hooks.forkTsCheckerServiceOutOfMemory = new SyncHook([]);
271+
this.compiler.hooks.forkTsCheckerEmit = new SyncHook([]);
272+
this.compiler.hooks.forkTsCheckerDone = new SyncHook([]);
273+
274+
// for backwards compatibility
275+
this.compiler._pluginCompat.tap(checkerPluginName, (options: any) => {
276+
switch (options.name) {
277+
case customHooks.forkTsCheckerServiceBeforeStart:
278+
options.async = true;
279+
break;
280+
case customHooks.forkTsCheckerCancel:
281+
case customHooks.forkTsCheckerServiceStartError:
282+
case customHooks.forkTsCheckerWaiting:
283+
case customHooks.forkTsCheckerServiceStart:
284+
case customHooks.forkTsCheckerReceive:
285+
case customHooks.forkTsCheckerServiceOutOfMemory:
286+
case customHooks.forkTsCheckerEmit:
287+
case customHooks.forkTsCheckerDone:
288+
return true;
289+
}
290+
return undefined;
291+
});
292+
}
293+
229294
pluginCompile() {
230-
this.compiler.plugin('compile', () => {
231-
this.compiler.applyPluginsAsync('fork-ts-checker-service-before-start', () => {
295+
this.compiler.hooks.compile.tap(checkerPluginName, () => {
296+
this.compiler.hooks.forkTsCheckerServiceBeforeStart.callAsync(() => {
232297
if (this.cancellationToken) {
233298
// request cancellation if there is not finished job
234299
this.cancellationToken.requestCancellation();
235-
this.compiler.applyPlugins('fork-ts-checker-cancel', this.cancellationToken);
300+
this.compiler.hooks.forkTsCheckerCancel.call(this.cancellationToken);
236301
}
237302
this.checkDone = false;
238303
this.compilationDone = false;
@@ -252,14 +317,14 @@ class ForkTsCheckerWebpackPlugin {
252317
this.logger.error(this.colors.red('Cannot start checker service: ' + (error ? error.toString() : 'Unknown error')));
253318
}
254319

255-
this.compiler.applyPlugins('fork-ts-checker-service-start-error', error);
320+
this.compiler.hooks.forkTsCheckerServiceStartError.call(error);
256321
}
257322
});
258323
});
259324
}
260325

261326
pluginEmit() {
262-
this.compiler.plugin('emit', (compilation: any, callback: () => void) => {
327+
this.compiler.hooks.emit.tapAsync(checkerPluginName, (compilation: any, callback: () => void) => {
263328
if (this.isWatching && this.async) {
264329
callback();
265330
return;
@@ -276,7 +341,7 @@ class ForkTsCheckerWebpackPlugin {
276341
}
277342

278343
pluginDone() {
279-
this.compiler.plugin('done', () => {
344+
this.compiler.hooks.done.tap(checkerPluginName, (_stats: webpack.Stats) => {
280345
if (!this.isWatching || !this.async) {
281346
return;
282347
}
@@ -285,10 +350,7 @@ class ForkTsCheckerWebpackPlugin {
285350
this.doneCallback();
286351
} else {
287352
if (this.compiler) {
288-
this.compiler.applyPlugins(
289-
'fork-ts-checker-waiting',
290-
this.tslint !== false
291-
);
353+
this.compiler.hooks.forkTsCheckerWaiting.call(this.tslint !== false);
292354
}
293355
if (!this.silent && this.logger) {
294356
this.logger.info(
@@ -326,8 +388,7 @@ class ForkTsCheckerWebpackPlugin {
326388
}
327389
);
328390

329-
this.compiler.applyPlugins(
330-
'fork-ts-checker-service-start',
391+
this.compiler.hooks.forkTsCheckerServiceStart.call(
331392
this.tsconfigPath,
332393
this.tslintPath,
333394
this.watchPaths,
@@ -398,7 +459,7 @@ class ForkTsCheckerWebpackPlugin {
398459
);
399460
}
400461

401-
this.compiler.applyPlugins('fork-ts-checker-receive', this.diagnostics, this.lints);
462+
this.compiler.hooks.forkTsCheckerReceive.call(this.diagnostics, this.lints);
402463

403464
if (this.compilationDone) {
404465
(this.isWatching && this.async) ? this.doneCallback() : this.emitCallback();
@@ -409,7 +470,7 @@ class ForkTsCheckerWebpackPlugin {
409470
if (signal === 'SIGABRT') {
410471
// probably out of memory :/
411472
if (this.compiler) {
412-
this.compiler.applyPlugins('fork-ts-checker-service-out-of-memory');
473+
this.compiler.hooks.forkTsCheckerServiceOutOfMemory.call();
413474
}
414475
if (!this.silent && this.logger) {
415476
this.logger.error(
@@ -426,8 +487,7 @@ class ForkTsCheckerWebpackPlugin {
426487
return function emitCallback (this: ForkTsCheckerWebpackPlugin) {
427488
const elapsed = Math.round(this.elapsed[0] * 1E9 + this.elapsed[1]);
428489

429-
this.compiler.applyPlugins(
430-
'fork-ts-checker-emit',
490+
this.compiler.hooks.forkTsCheckerEmit.call(
431491
this.diagnostics,
432492
this.lints,
433493
elapsed
@@ -469,8 +529,7 @@ class ForkTsCheckerWebpackPlugin {
469529
const elapsed = Math.round(this.elapsed[0] * 1E9 + this.elapsed[1]);
470530

471531
if (this.compiler) {
472-
this.compiler.applyPlugins(
473-
'fork-ts-checker-done',
532+
this.compiler.hooks.forkTsCheckerDone.call(
474533
this.diagnostics,
475534
this.lints,
476535
elapsed

test/integration/index.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('[INTEGRATION] index', function () {
1818
: { transpileOnly: true, silent: true };
1919

2020
return webpack({
21+
mode: 'development',
2122
context: path.resolve(__dirname, './project'),
2223
entry: './src/index.ts',
2324
output: {
@@ -89,7 +90,7 @@ describe('[INTEGRATION] index', function () {
8990

9091
it('should block emit on build mode', function (callback) {
9192
var compiler = createCompiler();
92-
compiler.plugin('fork-ts-checker-emit', function () {
93+
compiler.hooks.forkTsCheckerEmit.tap('should block emit on build mode', function () {
9394
expect(true).to.be.true;
9495
callback();
9596
});
@@ -101,7 +102,7 @@ describe('[INTEGRATION] index', function () {
101102
var compiler = createCompiler();
102103
var watching = compiler.watch({}, function() {});
103104

104-
compiler.plugin('fork-ts-checker-done', function () {
105+
compiler.hooks.forkTsCheckerDone.tap('should not block emit on watch mode', function () {
105106
watching.close(function() {
106107
expect(true).to.be.true;
107108
callback();
@@ -113,7 +114,7 @@ describe('[INTEGRATION] index', function () {
113114
var compiler = createCompiler({ async: false });
114115
var watching = compiler.watch({}, function() {});
115116

116-
compiler.plugin('fork-ts-checker-emit', function () {
117+
compiler.hooks.forkTsCheckerEmit.tap('should block emit if async flag is false', function () {
117118
watching.close(function() {
118119
expect(true).to.be.true;
119120
callback();
@@ -125,7 +126,7 @@ describe('[INTEGRATION] index', function () {
125126
var compiler = createCompiler();
126127
var watching = compiler.watch({}, function() {});
127128

128-
compiler.plugin('fork-ts-checker-done', function () {
129+
compiler.hooks.forkTsCheckerDone.tap('kills the service when the watch is done', function () {
129130
watching.close(function() {
130131
expect(killServiceWasCalled()).to.be.true;
131132
done();
@@ -191,15 +192,15 @@ describe('[INTEGRATION] index', function () {
191192
var compiler = createCompiler();
192193
var delayed = false;
193194

194-
compiler.plugin('fork-ts-checker-service-before-start', function (cb) {
195+
compiler.hooks.forkTsCheckerServiceBeforeStart.tapAsync('should allow delaying service-start', function (cb) {
195196
setTimeout(function () {
196197
delayed = true;
197198

198199
cb();
199200
}, 0);
200201
});
201202

202-
compiler.plugin('fork-ts-checker-service-start', function () {
203+
compiler.hooks.forkTsCheckerServiceStart.tap('should allow delaying service-start', function () {
203204
expect(delayed).to.be.true;
204205
callback();
205206
});

test/integration/vue.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('[INTEGRATION] vue', function () {
2020
plugin = new ForkTsCheckerWebpackPlugin(Object.assign({}, options, { silent: true }));
2121

2222
compiler = webpack({
23+
mode: 'development',
2324
context: path.resolve(__dirname, './vue'),
2425
entry: './src/index.ts',
2526
output: {

0 commit comments

Comments
 (0)