Skip to content

Commit

Permalink
feat: change start hook to AsyncSeriesWaterfallHook (#425)
Browse files Browse the repository at this point in the history
Adds possibility to delay the start of the plugin by tapping into the
start hook.

BREAKING CHANGE: 🧨 start hook changed from SyncWaterfallHook to AsyncSeriesWaterfallHook

✅ Closes: #424
  • Loading branch information
piotr-oles committed May 26, 2020
1 parent 8687c63 commit 5665dac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -178,7 +178,7 @@ Options for the ESLint linter (`eslint` option object).

| Name | Type | Default value | Description |
| -------------------- | ---------------------- | ------------------------- | ----------- |
| `enabled` | `boolean` | `true` | If `true`, it enables ESLint linter. |
| `enabled` | `boolean` | `false` | If `true`, it enables ESLint linter. |
| `files` | `string` or `string[]` | This value is required | One or more [glob patterns](https://en.wikipedia.org/wiki/Glob_(programming)) to the files that should be linted. Works the same as the `eslint` command. |
| `memoryLimit` | `number` | `2048` | Memory limit for the linter process in MB. If the process exits with the allocation failed error, try to increase this number. |
| `options` | `object` | `{}` | [Options](https://eslint.org/docs/developer-guide/nodejs-api#cliengine) that can be used to initialize ESLint. |
Expand Down Expand Up @@ -316,13 +316,13 @@ If you use **TypeScript >=3.8.0**, you can fix it by passing `"importsNotUsedAsV

This plugin provides some custom webpack hooks:

| Hook key | Type | Params | Description |
| ---------- | ------------------- | --------------------- | ----------- |
| `start` | `SyncWaterfallHook` | `change, compilation` | Starts issues checking for a compilation. It's a waterfall hook, so you can modify the list of changed and removed files. |
| `waiting` | `SyncHook` | `compilation` | Waiting for the issues checking. |
| `canceled` | `SyncHook` | `compilation` | Issues checking for the compilation has been canceled. |
| `error` | `SyncHook` | `compilation` | An error occurred during issues checking. |
| `issues` | `SyncWaterfallHook` | `issues, compilation` | Issues have been received and will be reported. It's a waterfall hook, so you can modify the list of received issues. |
| Hook key | Type | Params | Description |
| ---------- | -------------------------- | --------------------- | ----------- |
| `start` | `AsyncSeriesWaterfallHook` | `change, compilation` | Starts issues checking for a compilation. It's an async waterfall hook, so you can modify the list of changed and removed files or delay the start of the service. |
| `waiting` | `SyncHook` | `compilation` | Waiting for the issues checking. |
| `canceled` | `SyncHook` | `compilation` | Issues checking for the compilation has been canceled. |
| `error` | `SyncHook` | `compilation` | An error occurred during issues checking. |
| `issues` | `SyncWaterfallHook` | `issues, compilation` | Issues have been received and will be reported. It's a waterfall hook, so you can modify the list of received issues. |

To access plugin hooks and tap into the event, we need to use the `getCompilerHooks` static method.
When we call this method with a [webpack compiler instance](https://webpack.js.org/api/node/), it returns the object with
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/pluginHooks.ts
@@ -1,5 +1,5 @@
import * as webpack from 'webpack';
import { SyncHook, SyncWaterfallHook } from 'tapable';
import { SyncHook, SyncWaterfallHook, AsyncSeriesWaterfallHook } from 'tapable';
import { FilesChange } from '../reporter';
import { Issue } from '../issue';

Expand All @@ -10,7 +10,7 @@ const compilerHookMap = new WeakMap<

function createForkTsCheckerWebpackPluginHooks() {
return {
start: new SyncWaterfallHook<FilesChange, webpack.compilation.Compilation>([
start: new AsyncSeriesWaterfallHook<FilesChange, webpack.compilation.Compilation>([
'change',
'compilation',
]),
Expand All @@ -30,7 +30,7 @@ function forwardForkTsCheckerWebpackPluginHooks(
source: ForkTsCheckerWebpackPluginHooks,
target: ForkTsCheckerWebpackPluginHooks
) {
source.start.tap('ForkTsCheckerWebpackPlugin', target.start.call);
source.start.tapPromise('ForkTsCheckerWebpackPlugin', target.start.promise);
source.waiting.tap('ForkTsCheckerWebpackPlugin', target.waiting.call);
source.canceled.tap('ForkTsCheckerWebpackPlugin', target.canceled.call);
source.error.tap('ForkTsCheckerWebpackPlugin', target.error.call);
Expand Down
18 changes: 11 additions & 7 deletions src/hooks/tapStartToConnectAndRunReporter.ts
Expand Up @@ -61,20 +61,24 @@ function tapStartToConnectAndRunReporter(
configuration.logger.infrastructure.info('Calling reporter service for single check.');
}

change = hooks.start.call(change, compilation);
state.report = new Promise(async (resolve) => {
change = await hooks.start.promise(change, compilation);

state.report = reporter
.connect()
.then(() => reporter.getReport(change))
.catch((error) => {
try {
await reporter.connect();
const report = await reporter.getReport(change);

resolve(report);
} catch (error) {
if (error instanceof OperationCanceledError) {
hooks.canceled.call(compilation);
} else {
hooks.error.call(error, compilation);
}

return undefined;
});
resolve(undefined);
}
});
});
}

Expand Down

0 comments on commit 5665dac

Please sign in to comment.