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

getStats

function

Returns Stats object for the current compilation.

addModule

function (module, callback)

Adds a module to the current compilation.

Parameters:

  • module - module to be added
  • callback - a callback after the module has been added

getModule

function (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 using module.identifier() method.

findModule

function (identifier)

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

function (module, callback)

Builds the given module.

Parameters:

  • module - the module to be built.
  • callback - invoked as callback(err, module) when the build finishes.

processModuleDependencies

function (module, callback)

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

function (context, entry, name, callback)

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

function (module, thisCallback)

Triggers a re-build of the module.

Parameters:

  • module - module to be rebuilt.
  • thisCallback - function to be invoked when the module finishes rebuilding.

finish

function (callback)

Finishes compilation and invokes the given callback.

Parameters:

  • callback - function to be invoked when the compilation has been finished.

seal

function (callback)

Seals the compilation.

Parameters:

  • callback - function to be invoked when the compilation has been sealed.

unseal

function

Unseals the compilation.

reportDependencyErrorsAndWarnings

function (module, blocks)

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

function (groupOptions, module, loc, request)

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

function (name)

Creates and adds a new chunk to the compilation.chunks. Returns that chunk.

Parameters:

  • name - the name of the chunk.

assignDepth

function (module)

Assigns depth to the given module and its dependency blocks recursively.

Parameters:

  • module - the module to assign depth to.

getDependencyReferencedExports

function (dependency, runtime)

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

function (module, block)

Removes relation of the module to the dependency block.

Parameters:

  • module - a module relationship to be removed.
  • block - dependency block.

patchChunksAfterReasonRemoval

function (module, chunk)

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

function (block, chunk)

Removes given chunk from a dependencies block module and chunks after removing dependency reasons. Called automatically by removeReasonsOfDependencyBlock.

Parameters:

  • block - block tie for Chunk.
  • chunk - a chunk to remove from dependencies.

sortItemsWithChunkIds

function

summarizeDependencies

function

createHash

function

createModuleAssets

function

createChunkAssets

function

getPath

function (filename, data)

Returns the interpolated path.

Parameters:

  • filename - used to get asset path with hash.
  • data - data object.

getPathWithInfo

function (filename, data)

Returns interpolated path and asset information.

Parameters:

  • filename - used to get asset path with hash.
  • data - data object.

createChildCompiler

function (name, outputOptions, plugins)

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 child Compiler.
  • outputOptions - output options object.
  • plugins - webpack plugins that will be applied.

checkConstraints

function

emitAsset

function (file, source, assetInfo = {})

Parameters:

  • file - file name of the asset
  • source - the source of the asset
  • assetInfo - additional asset information

updateAsset

function (file, newSourceOrFunction, assetInfoUpdateOrFunction)

Parameters:

  • file - file name of the asset
  • newSourceOrFunction - new asset source or function converting old to new
  • assetInfoUpdateOrFunction - new asset info or function converting old to new

deleteAsset

function (file)

Parameters:

  • file - file name of the asset

getAssets

function

Returns array of all assets under the current compilation.

getAsset

function (name)

Parameters:

  • name - the name of the asset to return

getCache

function (name)

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:

MethodDescription
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:

DeprecatedUse instead
chunk.entryModulechunkGraph.getChunkEntryModulesIterable(chunk)
chunk.hasEntryModule()chunkGraph.getNumberOfEntryModules(chunk) > 0
chunk.getModules()chunkGraph.getChunkModules(chunk)
chunk.modulesIterablechunkGraph.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.idchunkGraph.getModuleId(module)
module.hashchunkGraph.getModuleHash(module, runtime)
module.renderedHashchunkGraph.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.indexmoduleGraph.getPreOrderIndex(module)
module.index2moduleGraph.getPostOrderIndex(module)
module.depthmoduleGraph.getDepth(module)
module.issuermoduleGraph.getIssuer(module)
module.usedExportsmoduleGraph.getUsedExports(module, runtime)
module.optimizationBailoutmoduleGraph.getOptimizationBailout(module)
module.profilemoduleGraph.getProfile(module)
module.optionalmodule.isOptional(moduleGraph)
dependency.originModulemoduleGraph.getParentModule(dependency)
Edit this page·

4 Contributors

EugeneHlushkowizardofhogwartsjamesgeorge007snitin315