Skip to content

Commit 7150ffa

Browse files
authored
docs: improve custom plugin hooks documentation (#637)
1 parent 4cb7e39 commit 7150ffa

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,24 +362,37 @@ When we call this method with a [webpack compiler instance](https://webpack.js.o
362362
[tapable](https://github.com/webpack/tapable) hooks where you can pass in your callbacks.
363363

364364
```js
365-
const webpack = require('webpack');
365+
// ./src/webpack/MyWebpackPlugin.js
366366
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
367367

368-
const compiler = webpack({
369-
// ... webpack config
370-
});
368+
class MyWebpackPlugin {
369+
apply(compiler) {
370+
const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler);
371+
372+
// log some message on waiting
373+
hooks.waiting.tap('MyPlugin', () => {
374+
console.log('waiting for issues');
375+
});
376+
// don't show warnings
377+
hooks.issues.tap('MyPlugin', (issues) =>
378+
issues.filter((issue) => issue.severity === 'error')
379+
);
380+
}
381+
}
371382

372-
// optionally add the plugin to the compiler
373-
// **don't do this if already added through configuration**
374-
new ForkTsCheckerWebpackPlugin().apply(compiler);
383+
module.exports = MyWebpackPlugin;
375384

376-
// now get the plugin hooks from compiler
377-
const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler);
385+
// webpack.config.js
386+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
387+
const MyWebpackPlugin = require('./src/webpack/MyWebpackPlugin');
378388

379-
// say we want to show some message when plugin is waiting for issues results
380-
hooks.waiting.tap('yourListenerName', () => {
381-
console.log('waiting for issues');
382-
});
389+
module.exports = {
390+
/* ... */
391+
plugins: [
392+
new ForkTsCheckerWebpackPlugin(),
393+
new MyWebpackPlugin()
394+
]
395+
};
383396
```
384397

385398
## Typings

0 commit comments

Comments
 (0)