Compilation Object
The Compilation object has many methods and hooks available. On this page, we will list the available methods and properties.
compilation object methods
The signatures below use two callback shapes that recur throughout the API:
type ModuleCallback = (
err?: WebpackError | null,
result?: Module | null,
) => void;
type ErrorCallback = (err?: WebpackError | null) => void;getStats
() => Stats
Returns Stats object for the current compilation.
addModule
(module: Module, callback: ModuleCallback) => void
Adds a module to the current compilation.
Parameters:
module- module to be addedcallback- a callback after the module has been added
getModule
(module: Module) => Module
Fetches a module from a compilation by its identifier.
Parameters:
module- module to be fetched. The identifier is extracted from the module by the compilation usingmodule.identifier()method.
findModule
(identifier: string) => Module | undefined
Attempts to search for a module by its identifier. Returns the module, or undefined if none matches.
Parameters:
identifier- the identifier string of the module to be searched for.
buildModule
(module: Module, callback: ModuleCallback) => void
Builds the given module.
Parameters:
module- the module to be built.callback- invoked ascallback(err, module)when the build finishes.
processModuleDependencies
(module: Module, callback: ModuleCallback) => void
Process the given module dependencies.
Parameters:
module- module to be processed for the dependencies.callback- function to be invoked when dependencies of the module had been processed.
addEntry
(context: string, entry: Dependency, optionsOrName: string | EntryOptions, callback: ModuleCallback) => void
Adds an entry to the compilation.
Parameters:
context- context path for entry.entry- entry dependency.name- the name of entry.callback- function to be invoked when addEntry finishes.
rebuildModule
(module: Module, callback: ModuleCallback) => void
Triggers a re-build of the module.
Parameters:
module- module to be rebuilt.thisCallback- function to be invoked when the module finishes rebuilding.
finish
(callback: ErrorCallback) => void
Finishes compilation and invokes the given callback.
Parameters:
callback- function to be invoked when the compilation has been finished.
seal
(callback: ErrorCallback) => void
Seals the compilation.
Parameters:
callback- function to be invoked when the compilation has been sealed.
unseal
() => void
Unseals the compilation.
reportDependencyErrorsAndWarnings
(module: Module, blocks: DependenciesBlock[]) => boolean
Adds errors and warnings of the given module to the compilation errors and warnings.
Parameters:
module- the module whose errors and warnings are to be reported.blocks- a set of dependency blocks to report from.
addChunkInGroup
(groupOptions: string | ChunkGroupOptions, module?: Module, loc?: DependencyLocation, request?: string) => ChunkGroup
Adds module to an existing chunk group or creates a new one. Returns a chunkGroup.
Parameters:
groupOptions- options for the chunk group.module- a module that references the chunk group.loc- the location from which the chunk group is referenced (inside of the module).request- the request from which the chunk group is referenced.
addChunk
(name?: string | null) => Chunk
Creates and adds a new chunk to the compilation.chunks. Returns that chunk.
Parameters:
name- the name of the chunk.
assignDepth
(module: Module) => void
Assigns depth to the given module and its dependency blocks recursively.
Parameters:
module- the module to assign depth to.
getDependencyReferencedExports
(dependency: Dependency, runtime: RuntimeSpec) => (string[] | ReferencedExport)[]
Returns the exports referenced by the given dependency.
Parameters:
dependency- the dependency to get the referenced exports of.runtime- the runtime to get them for.
removeReasonsOfDependencyBlock
(module: Module, block: DependenciesBlockLike) => void
Removes relation of the module to the dependency block.
Parameters:
module- a module relationship to be removed.block- dependency block.
patchChunksAfterReasonRemoval
(module: Module, chunk: Chunk) => void
Patches ties of module and chunk after removing dependency reasons. Called automatically by removeReasonsOfDependencyBlock.
Parameters:
module- a module to patch tie.chunk- a chunk to patch tie.
removeChunkFromDependencies
(block: DependenciesBlock, chunk: Chunk) => void
Removes given chunk from a dependencies block module and chunks after removing dependency reasons. Called automatically by removeReasonsOfDependencyBlock.
Parameters:
block- block tie forChunk.chunk- a chunk to remove from dependencies.
sortItemsWithChunkIds
() => void
summarizeDependencies
() => void
createHash
() => CodeGenerationJob[]
createModuleAssets
() => void
createChunkAssets
(callback: ErrorCallback) => void
getPath
(filename: string | TemplatePathFn, data?: PathData) => string
Returns the interpolated path.
Parameters:
filename- used to get asset path with hash.data- data object.
getPathWithInfo
(filename: string | TemplatePathFn, data?: PathData) => { path: string, info: AssetInfo }
Returns interpolated path and asset information.
Parameters:
filename- used to get asset path with hash.data- data object.
createChildCompiler
(name: string, outputOptions?: Partial<OutputNormalized>, plugins?: (WebpackPluginInstance | ((this: Compiler, compiler: Compiler) => void) | Falsy)[]) => Compiler
Allows running another instance of webpack inside of webpack. However, as a child with different settings and configurations applied. It copies all hooks and plugins from the parent (or top-level compiler) and creates a child Compiler instance. Returns the created Compiler.
Parameters:
name- name for the childCompiler.outputOptions- output options object.plugins- webpack plugins that will be applied.
checkConstraints
() => void
emitAsset
(file: string, source: Source, assetInfo?: AssetInfo) => void
Parameters:
file- file name of the assetsource- the source of the assetassetInfo- additional asset information,{}when omitted
updateAsset
(file: string, newSourceOrFunction: Source | ((source: Source) => Source), assetInfoUpdateOrFunction?: AssetInfo | ((assetInfo?: AssetInfo) => AssetInfo | undefined)) => void
Parameters:
file- file name of the assetnewSourceOrFunction- new asset source or function converting old to newassetInfoUpdateOrFunction- new asset info or function converting old to new
deleteAsset
(file: string) => void
Parameters:
file- file name of the asset
getAssets
() => Readonly<Asset>[]
Returns array of all assets under the current compilation.
getAsset
(name: string) => Readonly<Asset> | undefined
Parameters:
name- the name of the asset to return
getCache
(name: string) => CacheFacade
Returns a CacheFacade for the given name, the API a plugin uses to store results between builds and reuse them from the persistent cache. Prefer it over compiler.getCache(name), and over the deprecated compilation.cache, so that the cached entries belong to the compilation being built.
Parameters:
name- a name identifying the cache, conventionally your plugin's name
compiler.hooks.compilation.tap("MyPlugin", (compilation) => {
const cache = compilation.getCache("MyPlugin");
compilation.hooks.processAssets.tapPromise("MyPlugin", async (assets) => {
for (const [name, source] of Object.entries(assets)) {
// the etag decides when an entry is stale: pass something derived
// from the inputs, here the source itself
const etag = cache.getLazyHashedEtag(source);
const cached = await cache.getPromise(name, etag);
if (cached !== undefined) {
compilation.updateAsset(name, cached);
continue;
}
const result = transform(source);
await cache.storePromise(name, etag, result);
compilation.updateAsset(name, result);
}
});
});A CacheFacade provides:
| Method | Description |
|---|---|
get(identifier, etag, callback) | Read an entry. The callback receives undefined when nothing valid is stored. |
getPromise(identifier, etag) | The promise form of get. |
store(identifier, etag, data, callback) | Write an entry. |
storePromise(identifier, etag, data) | The promise form of store. |
provide(identifier, etag, computer, callback) | Read an entry, or compute it with computer and store the result. |
getItemCache(identifier, etag) | An ItemCacheFacade bound to one identifier and etag, with get / getPromise / store / storePromise / provide taking no identifier. |
getChildCache(name) | A nested CacheFacade whose entries are namespaced under name. |
getLazyHashedEtag(obj) | An etag for a hashable object such as a Source, computed only when it is actually needed. |
mergeEtags(a, b) | One etag standing for two, when an entry depends on more than one input. |
isEnabled() | Whether caching is enabled at all. |
Module and chunk graphs
5.0.0+webpack 5 moved the relations between modules and chunks out of the Module and Chunk objects themselves and into two graphs held by the compilation:
compilation.moduleGraph— how modules relate to each other: which module issued which, what a dependency refers to, which exports are used.compilation.chunkGraph— how modules relate to chunks: which chunk contains a module, a module's id and hash, which modules are entry modules of a chunk.
The reason is that one module can now take part in several graphs (a module built once can appear in more than one runtime), so a value like "the id of this module" is only meaningful together with a graph.
compilation.hooks.optimizeChunks.tap("MyPlugin", (chunks) => {
const { chunkGraph, moduleGraph } = compilation;
for (const chunk of chunks) {
for (const module of chunkGraph.getChunkModules(chunk)) {
const id = chunkGraph.getModuleId(module);
const issuer = moduleGraph.getIssuer(module);
// ...
}
}
});Accessing the old members still works, but logs a deprecation warning naming a DEP_WEBPACK_* code. These are the replacements:
| Deprecated | Use instead |
|---|---|
chunk.entryModule | chunkGraph.getChunkEntryModulesIterable(chunk) |
chunk.hasEntryModule() | chunkGraph.getNumberOfEntryModules(chunk) > 0 |
chunk.getModules() | chunkGraph.getChunkModules(chunk) |
chunk.modulesIterable | chunkGraph.getChunkModulesIterable(chunk) |
chunk.getNumberOfModules() | chunkGraph.getNumberOfChunkModules(chunk) |
chunk.containsModule(module) | chunkGraph.isModuleInChunk(module, chunk) |
chunk.addModule(module) | chunkGraph.connectChunkAndModule(chunk, module) |
chunk.removeModule(module) | chunkGraph.disconnectChunkAndModule(chunk, module) |
chunk.moveModule(module, other) | chunkGraph.disconnectChunkAndModule(chunk, module) then connectChunkAndModule |
chunk.remove() | chunkGraph.disconnectChunk(chunk) and chunk.disconnectFromGroups() |
chunk.isEmpty() | chunkGraph.getNumberOfChunkModules(chunk) === 0 |
chunk.integrate(other) | chunkGraph.integrateChunks(chunk, other) |
chunk.canBeIntegrated(other) | chunkGraph.canChunksBeIntegrated(chunk, other) |
chunk.integratedSize(other, options) | chunkGraph.getIntegratedChunksSize(chunk, other, options) |
chunk.modulesSize() | chunkGraph.getChunkModulesSize(chunk) |
chunk.size(options) | chunkGraph.getChunkSize(chunk, options) |
chunk.compareTo(other) | chunkGraph.compareChunks(chunk, other) |
chunk.hasModuleInGraph(filter, filter2) | chunkGraph.hasModuleInGraph(chunk, filter, filter2) |
module.id | chunkGraph.getModuleId(module) |
module.hash | chunkGraph.getModuleHash(module, runtime) |
module.renderedHash | chunkGraph.getRenderedModuleHash(module, runtime) |
module.getChunks() | chunkGraph.getModuleChunks(module) |
module.getNumberOfChunks() | chunkGraph.getNumberOfModuleChunks(module) |
module.isInChunk(chunk) | chunkGraph.isModuleInChunk(module, chunk) |
module.addChunk(chunk) | chunkGraph.connectChunkAndModule(chunk, module) |
module.removeChunk(chunk) | chunkGraph.disconnectChunkAndModule(chunk, module) |
module.isEntryModule() | chunkGraph.isEntryModule(module) |
module.index | moduleGraph.getPreOrderIndex(module) |
module.index2 | moduleGraph.getPostOrderIndex(module) |
module.depth | moduleGraph.getDepth(module) |
module.issuer | moduleGraph.getIssuer(module) |
module.usedExports | moduleGraph.getUsedExports(module, runtime) |
module.optimizationBailout | moduleGraph.getOptimizationBailout(module) |
module.profile | moduleGraph.getProfile(module) |
module.optional | module.isOptional(moduleGraph) |
dependency.originModule | moduleGraph.getParentModule(dependency) |



