Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(compiler-hooks): adds examples, formatting and missing descriptions #2840

Merged
merged 11 commits into from
Mar 18, 2019
118 changes: 77 additions & 41 deletions src/content/api/compiler-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ contributors:
- rishantagarwal
- byzyk
- madhavarshney
- misterdev
---

The `Compiler` module is the main engine that creates a compilation instance
Expand Down Expand Up @@ -37,7 +38,9 @@ The following lifecycle hooks are exposed by the `compiler` and can be accessed
as such:

``` js
compiler.hooks.someHook.tap(/* ... */);
compiler.hooks.someHook.tap('MyPlugin', (params) => {
/* ... */
});
```

Depending on the hook type, `tapAsync` and `tapPromise` may also be available.
Expand All @@ -49,77 +52,84 @@ For the description of hook types, see [the Tapable docs](https://github.com/web

`SyncBailHook`

Executes a plugin after [the `entry` configuration](https://webpack.js.org/configuration/entry-context/#entry) from webpack options has been processed.
Called after the [`entry` configuration](/configuration/entry-context/#entry) from webpack options has been processed.

- Callback Parameters: [`context`](/configuration/entry-context/#context), [`entry`](/configuration/entry-context/#entry)

```js
compiler.hooks.entryOption.tap('MyPlugin', (context, entry) => {
/* ... */
});
```

Parameters: `context`, `entry`

### `afterPlugins`

`SyncHook`

Runs a plugin after setting up initial set of plugins.
Called after setting up initial set of internal plugins.

Parameters: `compiler`
- Callback Parameters: `compiler`


### `afterResolvers`

`SyncHook`

Executes a plugin after resolver setup is complete.
Triggered after resolver setup is complete.

Parameters: `compiler`
- Callback Parameters: `compiler`


### `environment`

`SyncHook`

Runs a plugin before the environment is prepared.
Called while preparing the compiler environment, right after inizializing the plugins in the configuration file.


### `afterEnvironment`

`SyncHook`

Executes a plugin after the environment setup is complete.
Called right after the `environment` hook, when the compiler environment setup is complete.


### `beforeRun`

`AsyncSeriesHook`

Adds a hook right before `compiler.run()` is executed.
Adds a hook right before running the compiler.

Parameters: `compiler`
- Callback Parameters: `compiler`


### `run`

`AsyncSeriesHook`

Hook into the compiler before it begins reading records.
Hook into the compiler before it begins reading [`records`](/configuration/other-options/#recordspath).

Parameters: `compiler`
- Callback Parameters: `compiler`


### `watchRun`

`AsyncSeriesHook`

Executes a plugin during watch mode after a new compilation is triggered
but before the compilation is actually started.
Executes a plugin during watch mode after a new compilation is triggered but before the compilation is actually started.

Parameters: `compiler`
- Callback Parameters: `compiler`


### `normalModuleFactory`

`SyncHook`

Runs a plugin after a `NormalModuleFactory` is created.
Called after a `NormalModuleFactory` is created.

Parameters: `normalModuleFactory`
- Callback Parameters: `normalModuleFactory`


### `contextModuleFactory`
Expand All @@ -128,7 +138,7 @@ Parameters: `normalModuleFactory`

Runs a plugin after a `ContextModuleFactory` is created.

Parameters: `contextModuleFactory`
- Callback Parameters: `contextModuleFactory`


### `beforeCompile`
Expand All @@ -137,25 +147,44 @@ Parameters: `contextModuleFactory`

Executes a plugin after compilation parameters are created.

Parameters: `compilationParams`
- Callback Parameters: `compilationParams`

The `compilationParams` variable is initialized as follows:

```js
compilationParams = {
normalModuleFactory,
contextModuleFactory,
compilationDependencies
};
```

This hook can be used to add/modify the compilation parameters:

```js
compiler.hooks.beforeCompile.tapAsync('MyPlugin', (params, callback) => {
params['MyPlugin - data'] = 'important stuff my plugin will use later';
callback();
});
```


### `compile`

`SyncHook`

Hook into the compiler before a new compilation is created.
Called right after `beforeCompile`, before a new compilation is created.

Parameters: `compilationParams`
- Callback Parameters: `compilationParams`


### `thisCompilation`

`SyncHook`

Executed before emitting the `compilation` event (see below).
Executed while initializing the compilation, right before emitting the `compilation` event.

Parameters: `compilation`, `compilationParams`
- Callback Parameters: `compilation`, `compilationParams`


### `compilation`
Expand All @@ -164,83 +193,90 @@ Parameters: `compilation`, `compilationParams`

Runs a plugin after a compilation has been created.

Parameters: `compilation`, `compilationParams`
- Callback Parameters: `compilation`, `compilationParams`


### `make`

`AsyncParallelHook`

...
Executed before finishing the compilation.

Parameters: `compilation`
- Callback Parameters: `compilation`


### `afterCompile`

`AsyncSeriesHook`

...
Called after finishing and sealing the compilation.

Parameters: `compilation`
- Callback Parameters: `compilation`


### `shouldEmit`

`SyncBailHook`

Can return true/false at this point
Called before emitting assets. Should return a boolean telling whether to emit.

Parameters: `compilation`
- Callback Parameters: `compilation`

```js
compiler.hooks.shouldEmit.tap('MyPlugin', (compilation) => {
// return true to emit the output, otherwise false
return true;
});
```


### `emit`

`AsyncSeriesHook`

Before emitting assets to output dir
Executed right before emitting assets to output dir.

Parameters: `compilation`
- Callback Parameters: `compilation`


### `afterEmit`

`AsyncSeriesHook`

After emitting assets to output dir
Called after emitting assets to output directory.

Parameters: `compilation`
- Callback Parameters: `compilation`


### `done`

`AsyncSeriesHook`

Compilation has completed.
Executed when the compilation has completed.

Parameters: `stats`
- Callback Parameters: `stats`


### `failed`

`SyncHook`

Compilation has failed.
Called if the compilation fails.

Parameters: `error`
- Callback Parameters: `error`


### `invalid`

`SyncHook`

Watch compilation has been invalidated.
Executed when a watching compilation has been invalidated.

Parameters: `fileName`, `changeTime`
- Callback Parameters: `fileName`, `changeTime`


### `watchClose`

`SyncHook`

Watch mode has stopped.
Called when a watching compilation has stopped.