The Compilation module is used by the Compiler to create new compilations
(or builds). A compilation instance has access to all modules and their
dependencies (most of which are circular references). It is the literal
compilation of all the modules in the dependency graph of an application.
During the compilation phase, modules are loaded, sealed, optimized, chunked,
hashed and restored.
The Compilation class also extends Tapable and provides the following
lifecycle hooks. They can be tapped the same way as compiler hooks:
compilation.hooks.someHook.tap(/* ... */);
As with the compiler, tapAsync and tapPromise may also be available
depending on the type of hook.
buildModuleSyncHook
Triggered before a module build has started, can be used to modify the module.
modulecompilation.hooks.buildModule.tap('SourceMapDevToolModuleOptionsPlugin',
module => {
module.useSourceMap = true;
}
);
rebuildModuleSyncHook
Fired before rebuilding a module.
modulefailedModuleSyncHook
Run when a module build has failed.
module errorsucceedModuleSyncHook
Executed when a module has been built successfully.
modulefinishModulesAsyncSeriesHook
Called when all modules have been built without errors.
modulesfinishRebuildingModuleSyncHook
Executed when a module has been rebuilt, in case of both success or with errors.
modulesealSyncHook
Fired when the compilation stops accepting new modules.
unsealSyncHook
Fired when a compilation begins accepting new modules.
optimizeDependenciesBasicSyncBailHook
This hook will be removed in v5.0.0
Parameters: modules
optimizeDependenciesSyncBailHook
Fired at the beginning of dependency optimization.
modulesoptimizeDependenciesAdvancedSyncBailHook
This hook will be removed in v5.0.0
modulesafterOptimizeDependenciesSyncHook
Fired after the dependency optimization.
modulesoptimizeSyncHook
Triggered at the beginning of the optimization phase.
optimizeModulesBasicSyncBailHook
This hook will be removed in v5.0.0
modulesoptimizeModulesSyncBailHook
Called at the beginning of the modules optimization phase. A plugin can tap into this hook to perform modules optimizations.
modulesoptimizeModulesAdvancedSyncBailHook
This hook will be removed in v5.0.0
modulesafterOptimizeModulesSyncHook
Called after modules optimization has completed.
modulesoptimizeChunksBasicSyncBailHook
This hook will be removed in v5.0.0
chunksoptimizeChunksSyncBailHook
Called at the beginning of the chunks optimizion phase. A plugin can tap into this hook to perform chunks optimizations.
chunksoptimizeChunksAdvancedSyncBailHook
This hook will be removed in v5.0.0
chunksafterOptimizeChunksSyncHook
Fired after chunk optimization has completed.
chunksoptimizeTreeAsyncSeriesHook
Called before optimizing the dependency tree. A plugin can tap into this hook to perform a dependency tree optimization.
chunks modulesafterOptimizeTreeSyncHook
Called after the dependency tree optimization has completed with success.
chunks modulesoptimizeChunkModulesBasicSyncBailHook
This hook will be removed in v5.0.0
chunks modulesoptimizeChunkModulesSyncBailHook
Called after the tree optimization, at the beginning of the chunk modules optimization. A plugin can tap into this hook to perform optimizations of chunk modules.
chunks modulesoptimizeChunkModulesAdvancedSyncBailHook
This hook will be removed in v5.0.0
chunks modulesafterOptimizeChunkModulesSyncHook
Called after the chunkmodules optimization has successfully completed.
chunks modulesshouldRecordSyncBailHook
Called to determine whether or not to store records. Returning anything !== false will prevent every other "record" hook from being executed (record, recordModules, recordChunks and recordHash).
reviveModulesSyncHook
Restore module information from records.
modules recordsoptimizeModuleOrderSyncHook
This hook will be removed in v5.0.0
Sort the modules from most to least important.
modulesadvancedOptimizeModuleOrderSyncHook
This hook will be removed in v5.0.0
modulesbeforeModuleIdsSyncHook
Executed before assigning an id to each module.
modulesmoduleIdsSyncHook
Called to assign an id to each module.
modulesoptimizeModuleIdsSyncHook
Called at the beginning of the modules id optimization.
modulesafterOptimizeModuleIdsSyncHook
Called when the modules id optimization phase has completed.
modulesreviveChunksSyncHook
Restore chunk information from records.
chunks recordsoptimizeChunkOrderSyncHook
This hook will be removed in v5.0.0
Sort the chunks in from most to least important.
chunksbeforeChunkIdsSyncHook
Executed before assigning an id to each chunk.
chunkschunkIdsSyncHook
This hook will be available in v5.0.0
Called to assign an id to each chunk.
modulesbeforeOptimizeChunkIdsSyncHook
This hook will be available in v5.0.0
Fired before chunks id optimization.
chunksoptimizeChunkIdsSyncHook
Called at the beginning of the chunks id optimization phase.
chunksafterOptimizeChunkIdsSyncHook
Triggered after chunk id optimization has finished.
chunksrecordModulesSyncHook
Store module info to the records. This is only triggered if shouldRecord returns a truthy value.
modules recordsrecordChunksSyncHook
Store chunk info to the records. This is only triggered if shouldRecord returns a truthy value.
chunks recordsoptimizeCodeGenerationThis hook will be available in v5.0.0
A plugin can tap into this hook to optimize the generated code.
modulesbeforeModuleHashThis hook will be available in v5.0.0
Called before hashing modules.
afterModuleHashThis hook will be available in v5.0.0
Called after hashing modules.
beforeRuntimeRequirementsThis hook will be available in v5.0.0
Called before processing the modules required at runtime.
entrypointsafterRuntimeRequirementsThis hook will be available in v5.0.0
Called after processing the runtime requirements.
beforeHashSyncHook
Called before the compilation is hashed.
afterHashSyncHook
Called after the compilation is hashed.
recordHashSyncHook
Store information about record hash to the records. This is only triggered if shouldRecord returns a truthy value.
recordsrecordSyncHook
Store information about the compilation to the records. This is only triggered if shouldRecord returns a truthy value.
compilation recordsbeforeModuleAssetsSyncHook
Executed before module assets creation.
additionalChunkAssetsSyncHook
Create additional assets for the chunks.
chunksshouldGenerateChunkAssetsSyncBailHook
Called to determine wheter or not generate chunks assets. Returning anything !== false will allow chunk assets generation.
beforeChunkAssetsSyncHook
Executed before creating the chunks assets.
additionalAssetsAsyncSeriesHook
Create additional assets for the compilation. This hook can be used to download an image, for example:
compilation.hooks.additionalAssets.tapAsync('MyPlugin', callback => {
download('https://img.shields.io/npm/v/webpack.svg', function(resp) {
if(resp.status === 200) {
compilation.assets['webpack-version.svg'] = toAsset(resp);
callback();
} else {
callback(new Error('[webpack-example-plugin] Unable to download the image'));
}
});
});
optimizeChunkAssetsAsyncSeriesHook
Optimize any chunk assets. The assets are stored in compilation.assets. A
Chunk has a property files which points to all files created by a chunk.
Any additional chunk assets are stored in compilation.additionalChunkAssets.
chunksHere's an example that simply adds a banner to each chunk.
compilation.hooks
.optimizeChunkAssets
.tapAsync('MyPlugin', (chunks, callback) => {
chunks.forEach(chunk => {
chunk.files.forEach(file => {
compilation.assets[file] = new ConcatSource(
'\/**Sweet Banner**\/',
'\n',
compilation.assets[file]
);
});
});
callback();
});
afterOptimizeChunkAssetsSyncHook
The chunk assets have been optimized.
chunksHere's an example plugin from @boopathi that outputs exactly what went into each chunk.
compilation.hooks.afterOptimizeChunkAssets.tap('MyPlugin', chunks => {
chunks.forEach(chunk => {
console.log({
id: chunk.id,
name: chunk.name,
includes: chunk.getModules().map(module => module.request)
});
});
});
optimizeAssetsAsyncSeriesHook
Optimize all assets stored in compilation.assets.
assetsafterOptimizeAssetsSyncHook
The assets have been optimized.
assetsneedAdditionalSealSyncBailHook
Called to determine if the compilation needs to be unsealed to include other files.
afterSealAsyncSeriesHook
Executed right after needAdditionalSeal.
chunkHashSyncHook
Triggered to emit the hash for each chunk.
chunk chunkHashmoduleAssetSyncHook
Called when an asset from a module was added to the compilation.
module filenamechunkAssetSyncHook
Triggered when an asset from a chunk was added to the compilation.
chunk filenameassetPathSyncWaterfallHook
Called to determine the path of an asset.
path optionsneedAdditionalPassSyncBailHook
Called to determine if a asset need to be processed further after being emitted.
childCompilerSyncHook
Executed after setting up a child compiler.
childCompiler compilerName compilerIndexnormalModuleLoaderSyncHook
This hook will be moved in v5.0.0 to
NormalModule.getCompilationHooks(compilation).loader
The normal module loader is the function that actually loads all the modules
in the module graph (one-by-one).
loaderContext moduledependencyReferenceSyncWaterfallHook
This hooks allows changing the references reported by dependencies.
depRef dependency moduleThe
moduleparameter will be removed in v5.0.0