The Compiler module is the main engine that creates a compilation instance with all the options passed through the CLI or Node API. It extends the Tapable class in order to register and call plugins. Most user facing plugins are first registered on the Compiler.
When developing a plugin for webpack, you might want to know where each hook is called. To learn this, search for hooks.<hook name>.call across the webpack source
The Compiler supports watching which monitors the file system and recompiles as files change. When in watch mode, the compiler will emit the additional events such as watchRun, watchClose, and invalid. This is typically used in development, usually under the hood of tools like webpack-dev-server, so that the developer doesn't need to re-compile manually every time. Watch mode can also be entered via the CLI.
The following lifecycle hooks are exposed by the compiler and can be accessed as such:
compiler.hooks.someHook.tap('MyPlugin', (params) => {
/* ... */
}); Depending on the hook type, tapAsync and tapPromise may also be available.
For the description of hook types, see the Tapable docs.
environmentSyncHook
Called while preparing the compiler environment, right after initializing the plugins in the configuration file.
afterEnvironmentSyncHook
Called right after the environment hook, when the compiler environment setup is complete.
entryOptionSyncBailHook
Called after the entry configuration from webpack options has been processed.
compiler.hooks.entryOption.tap('MyPlugin', (context, entry) => {
/* ... */
}); afterPluginsSyncHook
Called after setting up initial set of internal plugins.
compilerafterResolversSyncHook
Triggered after resolver setup is complete.
compilerinitializeSyncHook
Called when a compiler object is initialized.
beforeRunAsyncSeriesHook
Adds a hook right before running the compiler.
compilerrunAsyncSeriesHook
Hook into the compiler before it begins reading records.
compilerwatchRunAsyncSeriesHook
Executes a plugin during watch mode after a new compilation is triggered but before the compilation is actually started.
compilernormalModuleFactorySyncHook
Called after a NormalModuleFactory is created.
normalModuleFactorycontextModuleFactorySyncHook
Runs a plugin after a ContextModuleFactory is created.
contextModuleFactorybeforeCompileAsyncSeriesHook
Executes a plugin after compilation parameters are created.
compilationParamsThe compilationParams variable is initialized as follows:
compilationParams = {
normalModuleFactory,
contextModuleFactory,
}; This hook can be used to add/modify the compilation parameters:
compiler.hooks.beforeCompile.tapAsync('MyPlugin', (params, callback) => {
params['MyPlugin - data'] = 'important stuff my plugin will use later';
callback();
}); compileSyncHook
Called right after beforeCompile, before a new compilation is created.
compilationParamsthisCompilationSyncHook
Executed while initializing the compilation, right before emitting the compilation event.
compilation, compilationParamscompilationSyncHook
Runs a plugin after a compilation has been created.
compilation, compilationParamsmakeAsyncParallelHook
Executed before finishing the compilation.
compilationafterCompileAsyncSeriesHook
Called after finishing and sealing the compilation.
compilationshouldEmitSyncBailHook
Called before emitting assets. Should return a boolean telling whether to emit.
compilationcompiler.hooks.shouldEmit.tap('MyPlugin', (compilation) => {
// return true to emit the output, otherwise false
return true;
}); emitAsyncSeriesHook
Executed right before emitting assets to output dir.
compilationafterEmitAsyncSeriesHook
Called after emitting assets to output directory.
compilationassetEmittedAsyncSeriesHook
Executed when an asset has been emitted. Provides access to information about the emitted asset, such as its output path and byte content.
file, infoFor example, you may access the asset's content buffer via info.content:
compiler.hooks.assetEmitted.tap(
'MyPlugin',
(file, { content, source, outputPath, compilation, targetPath }) => {
console.log(content); // <Buffer 66 6f 6f 62 61 72>
}
); doneAsyncSeriesHook
Executed when the compilation has completed.
statsadditionalPassAsyncSeriesHook
This hook allows you to do a one more additional pass of the build.
failedSyncHook
Called if the compilation fails.
errorinvalidSyncHook
Executed when a watching compilation has been invalidated.
fileName, changeTimewatchCloseSyncHook
Called when a watching compilation has stopped.
infrastructureLogSyncBailHook
Allows to use infrastructure logging when enabled in the configuration via infrastructureLogging option.
name, type, argslogSyncBailHook
Allows to log into stats when enabled, see stats.logging, stats.loggingDebug and stats.loggingTrace options.
origin, logEntry