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.
optimizeDependenciesSyncBailHook
Fired at the beginning of dependency optimization.
modulesafterOptimizeDependenciesSyncHook
Fired after the dependency optimization.
modulesoptimizeSyncHook
Triggered at the beginning of the optimization phase.
optimizeModulesSyncBailHook
Called at the beginning of the module optimization phase. A plugin can tap into this hook to perform optimizations on modules.
modulesafterOptimizeModulesSyncHook
Called after modules optimization has completed.
modulesoptimizeChunksSyncBailHook
Called at the beginning of the chunk optimization phase. A plugin can tap into this hook to perform optimizations on chunks.
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 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 modulesafterOptimizeChunkModulesSyncHook
Called after the chunkmodules optimization has completed successfully.
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 recordsbeforeModuleIdsSyncHook
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 recordsbeforeChunkIdsSyncHook
Executed before assigning an id to each chunk.
chunkschunkIdsSyncHook
Called to assign an id to each chunk.
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 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 recordsbeforeHashSyncHook
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 whether 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.
assetsprocessAssetsAsyncSeriesHook
Asset processing.
assetsHere's an example:
compilation.hooks.processAssets.tap(
{
name: 'MyPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, // see below for more stages
},
(assets) => {
// code here
}
);In addition to name and stage, you can pass a additionalAssets 5.8.0+ option which accepts either a value of true or a function with assets as parameter:
true - Run callback against assets added later by plugins.
compilation.hooks.processAssets.tap(
{
name: 'MyPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
additionalAssets: true,
},
(assets) => {
// this callback will run against assets added later by plugins.
}
);(assets) => {} - Run this specified callback against assets added later by plugins.
compilation.hooks.processAssets.tap(
{
name: 'MyPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
additionalAssets: (assets) => {
// this callback will run against assets added later by plugins.
},
},
(assets) => {
// code
}
);Here's a list of stages we can use:
PROCESS_ASSETS_STAGE_ADDITIONAL - Add additional assets to the compilation.PROCESS_ASSETS_STAGE_PRE_PROCESS - Basic preprocessing of the assets.PROCESS_ASSETS_STAGE_DERIVED - Derive new assets from the existing assets.PROCESS_ASSETS_STAGE_ADDITIONS - Add additional sections to the existing assets e.g. banner or initialization code.PROCESS_ASSETS_STAGE_OPTIMIZE - Optimize existing assets in a general way.PROCESS_ASSETS_STAGE_OPTIMIZE_COUNT - Optimize the count of existing assets, e.g. by merging them.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY - Optimize the compatibility of existing assets, e.g. add polyfills or vendor prefixes.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE - Optimize the size of existing assets, e.g. by minimizing or omitting whitespace.PROCESS_ASSETS_STAGE_DEV_TOOLING - Add development tooling to the assets, e.g. by extracting a source map.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE 5.8.0+ - Optimize the numbers of existing assets, e.g. by inlining assets into other assets.PROCESS_ASSETS_STAGE_SUMMARIZE - Summarize the list of existing assets.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH - Optimize the hashes of the assets, e.g. by generating real hashes of the asset content.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER - Optimize the transfer of existing assets, e.g. by preparing a compressed (gzip) file as separate asset.PROCESS_ASSETS_STAGE_ANALYSE - Analyze the existing assets.PROCESS_ASSETS_STAGE_REPORT - Creating assets for the reporting purposes.afterProcessAssetsSyncHook
Called after the processAssets hook had finished without error.
needAdditionalSealSyncBailHook
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 an asset needs to be processed further after being emitted.
childCompilerSyncHook
Executed after setting up a child compiler.
childCompiler compilerName compilerIndexnormalModuleLoaderSince webpack v5 normalModuleLoader hook was removed. Now to access the loader use NormalModule.getCompilationHooks(compilation).loader instead.