Movatterモバイル変換


[0]ホーム

URL:


webpack logo
ag grid
ag charts

Compiler Hooks

TheCompiler module is the main engine that creates a compilation instancewith all the options passed through theCLI orNode API. It extends theTapable class in order to register and call plugins. Most user-facing pluginsare first registered on theCompiler.

When developing a plugin for webpack, you might want to know where each hook is called. To learn this, search forhooks.<hook name>.call across the webpack source.

warning

Since webpack 5,hooks are no longer extendable. Use aWeakMap to add custom hooks.

Watching

TheCompiler supportswatching which monitors the filesystem and recompiles as files change. When in watch mode, the compiler willemit the additional events such aswatchRun,watchClose, andinvalid.This is typically used indevelopment, usually underthe hood of tools likewebpack-dev-server, so that the developer doesn'tneed to re-compile manually every time. Watch mode can also be entered via theCLI.

Hooks

The following lifecycle hooks are exposed by thecompiler and can be accessedas such:

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

Depending on the hook type,tapAsync andtapPromise may also be available.

For the description of hook types, seethe Tapable docs.

environment

SyncHook

Called while preparing the compiler environment, right after initializing the plugins in the configuration file.

afterEnvironment

SyncHook

Called right after theenvironment hook, when the compiler environment setup is complete.

entryOption

SyncBailHook

Called after theentry configuration from webpack options has been processed.

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

afterPlugins

SyncHook

Called after setting up initial set of internal plugins.

  • Callback Parameters:compiler

afterResolvers

SyncHook

Triggered after resolver setup is complete.

  • Callback Parameters:compiler

initialize

SyncHook

Called when a compiler object is initialized.

beforeRun

AsyncSeriesHook

Adds a hook right before running the compiler.

  • Callback Parameters:compiler

run

AsyncSeriesHook

Hook into the compiler before it begins readingrecords.

  • Callback Parameters:compiler

watchRun

AsyncSeriesHook

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

  • Callback Parameters:compiler

normalModuleFactory

SyncHook

Called after aNormalModuleFactory is created.

  • Callback Parameters:normalModuleFactory

contextModuleFactory

SyncHook

Runs a plugin after aContextModuleFactory is created.

  • Callback Parameters:contextModuleFactory

beforeCompile

AsyncSeriesHook

Executes a plugin after compilation parameters are created.

  • Callback Parameters:compilationParams

ThecompilationParams 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();});

compile

SyncHook

Called right afterbeforeCompile, before a new compilation is created. This hook isnot copied to child compilers.

  • Callback Parameters:compilationParams

thisCompilation

SyncHook

Executed while initializing the compilation, right before emitting thecompilation event. This hook isnot copied to child compilers.

  • Callback Parameters:compilation,compilationParams

compilation

SyncHook

Runs a plugin after a compilation has been created.

  • Callback Parameters:compilation,compilationParams

make

AsyncParallelHook

Executed before finishing the compilation. This hook isnot copied to child compilers.

  • Callback Parameters:compilation

afterCompile

AsyncSeriesHook

Called after finishing and sealing the compilation.

  • Callback Parameters:compilation

shouldEmit

SyncBailHook

Called before emitting assets. Should return a boolean telling whether to emit.

  • Callback Parameters:compilation
compiler.hooks.shouldEmit.tap('MyPlugin',(compilation)=>{// return true to emit the output, otherwise falsereturntrue;});

emit

AsyncSeriesHook

Executed right before emitting assets to output dir. This hook isnot copied to child compilers.

  • Callback Parameters:compilation

afterEmit

AsyncSeriesHook

Called after emitting assets to output directory. This hook isnot copied to child compilers.

  • Callback Parameters:compilation

assetEmitted

AsyncSeriesHook

Executed when an asset has been emitted. Provides access to information about the emitted asset, such as its output path and byte content.

  • Callback Parameters:file,info

For example, you may access the asset's content buffer viainfo.content:

compiler.hooks.assetEmitted.tap('MyPlugin',(file,{ content, source, outputPath, compilation, targetPath})=>{    console.log(content);// <Buffer 66 6f 6f 62 61 72>});

done

AsyncSeriesHook

Executed when the compilation has completed. This hook isnot copied to child compilers.

  • Callback Parameters:stats

additionalPass

AsyncSeriesHook

This hook allows you to do a one more additional pass of the build.

failed

SyncHook

Called if the compilation fails.

  • Callback Parameters:error

invalid

SyncHook

Executed when a watching compilation has been invalidated. This hook isnot copied to child compilers.

  • Callback Parameters:fileName,changeTime

watchClose

SyncHook

Called when a watching compilation has stopped.

shutdown

AsyncSeriesHook

Called when the compiler is closing.

infrastructureLog

SyncBailHook

Allows to use infrastructure logging when enabled in the configuration viainfrastructureLogging option.

  • Callback Parameters:name,type,args

log

SyncBailHook

Allows to log intostats when enabled, seestats.logging,stats.loggingDebug andstats.loggingTrace options.

  • Callback Parameters:origin,logEntry

8 Contributors

rishantagarwalbyzykmadhavarshneymisterdevEugeneHlushkosuperburritochenxsansnitin315

[8]ページ先頭

©2009-2025 Movatter.jp