diff --git a/src/content/api/compilation-hooks.md b/src/content/api/compilation-hooks.md index 970121733655..ee801fdefd58 100644 --- a/src/content/api/compilation-hooks.md +++ b/src/content/api/compilation-hooks.md @@ -8,6 +8,7 @@ contributors: - misterdev - wizardofhogwarts - EugeneHlushko + - anikethsaha --- The `Compilation` module is used by the `Compiler` to create new compilations @@ -20,13 +21,14 @@ 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: -``` js +```js compilation.hooks.someHook.tap(/* ... */); ``` As with the `compiler`, `tapAsync` and `tapPromise` may also be available depending on the type of hook. +W> Since webpack 5, `hooks` are no longer extendable. Use a `WeakMap` to add custom hooks. ### `buildModule` @@ -36,16 +38,15 @@ Triggered before a module build has started, can be used to modify the module. - Callback Parameters: `module` - ```js -compilation.hooks.buildModule.tap('SourceMapDevToolModuleOptionsPlugin', +compilation.hooks.buildModule.tap( + 'SourceMapDevToolModuleOptionsPlugin', module => { module.useSourceMap = true; } ); ``` - ### `rebuildModule` `SyncHook` @@ -54,7 +55,6 @@ Fired before rebuilding a module. - Callback Parameters: `module` - ### `failedModule` `SyncHook` @@ -63,7 +63,6 @@ Run when a module build has failed. - Callback Parameters: `module` `error` - ### `succeedModule` `SyncHook` @@ -72,7 +71,6 @@ Executed when a module has been built successfully. - Callback Parameters: `module` - ### `finishModules` `AsyncSeriesHook` @@ -81,7 +79,6 @@ Called when all modules have been built without errors. - Callback Parameters: `modules` - ### `finishRebuildingModule` `SyncHook` @@ -90,21 +87,18 @@ Executed when a module has been rebuilt, in case of both success or with errors. - Callback Parameters: `module` - ### `seal` `SyncHook` Fired when the compilation stops accepting new modules. - ### `unseal` `SyncHook` Fired when a compilation begins accepting new modules. - ### `optimizeDependenciesBasic` `SyncBailHook` @@ -113,7 +107,6 @@ W> This hook will be removed in v5.0.0 Parameters: `modules` - ### `optimizeDependencies` `SyncBailHook` @@ -122,7 +115,6 @@ Fired at the beginning of dependency optimization. - Callback Parameters: `modules` - ### `optimizeDependenciesAdvanced` `SyncBailHook` @@ -131,7 +123,6 @@ W> This hook will be removed in v5.0.0 - Callback Parameters: `modules` - ### `afterOptimizeDependencies` `SyncHook` @@ -140,14 +131,12 @@ Fired after the dependency optimization. - Callback Parameters: `modules` - ### `optimize` `SyncHook` Triggered at the beginning of the optimization phase. - ### `optimizeModulesBasic` `SyncBailHook` @@ -156,7 +145,6 @@ W> This hook will be removed in v5.0.0 - Callback Parameters: `modules` - ### `optimizeModules` `SyncBailHook` @@ -165,7 +153,6 @@ Called at the beginning of the module optimization phase. A plugin can tap into - Callback Parameters: `modules` - ### `optimizeModulesAdvanced` `SyncBailHook` @@ -174,7 +161,6 @@ W> This hook will be removed in v5.0.0 - Callback Parameters: `modules` - ### `afterOptimizeModules` `SyncHook` @@ -183,7 +169,6 @@ Called after modules optimization has completed. - Callback Parameters: `modules` - ### `optimizeChunksBasic` `SyncBailHook` @@ -192,7 +177,6 @@ W> This hook will be removed in v5.0.0 - Callback Parameters: `chunks` - ### `optimizeChunks` `SyncBailHook` @@ -201,7 +185,6 @@ Called at the beginning of the chunk optimization phase. A plugin can tap into t - Callback Parameters: `chunks` - ### `optimizeChunksAdvanced` `SyncBailHook` @@ -210,7 +193,6 @@ W> This hook will be removed in v5.0.0 - Callback Parameters: `chunks` - ### `afterOptimizeChunks` `SyncHook` @@ -219,7 +201,6 @@ Fired after chunk optimization has completed. - Callback Parameters: `chunks` - ### `optimizeTree` `AsyncSeriesHook` @@ -228,7 +209,6 @@ Called before optimizing the dependency tree. A plugin can tap into this hook to - Callback Parameters: `chunks` `modules` - ### `afterOptimizeTree` `SyncHook` @@ -237,7 +217,6 @@ Called after the dependency tree optimization has completed with success. - Callback Parameters: `chunks` `modules` - ### `optimizeChunkModulesBasic` `SyncBailHook` @@ -246,7 +225,6 @@ W> This hook will be removed in v5.0.0 - Callback Parameters: `chunks` `modules` - ### `optimizeChunkModules` `SyncBailHook` @@ -255,7 +233,6 @@ Called after the tree optimization, at the beginning of the chunk modules optimi - Callback Parameters: `chunks` `modules` - ### `optimizeChunkModulesAdvanced` `SyncBailHook` @@ -264,7 +241,6 @@ W> This hook will be removed in v5.0.0 - Callback Parameters: `chunks` `modules` - ### `afterOptimizeChunkModules` `SyncHook` @@ -273,14 +249,12 @@ Called after the chunkmodules optimization has completed successfully. - Callback Parameters: `chunks` `modules` - ### `shouldRecord` `SyncBailHook` Called to determine whether or not to store records. Returning anything `!== false` will prevent every other "record" hook from being executed ([`record`](#record), [`recordModules`](#recordmodules), [`recordChunks`](#recordchunks) and [`recordHash`](#recordhash)). - ### `reviveModules` `SyncHook` @@ -289,7 +263,6 @@ Restore module information from records. - Callback Parameters: `modules` `records` - ### `optimizeModuleOrder` `SyncHook` @@ -300,7 +273,6 @@ Sort the modules from most to least important. - Callback Parameters: `modules` - ### `advancedOptimizeModuleOrder` `SyncHook` @@ -309,7 +281,6 @@ W> This hook will be removed in v5.0.0 - Callback Parameters: `modules` - ### `beforeModuleIds` `SyncHook` @@ -318,7 +289,6 @@ Executed before assigning an `id` to each module. - Callback Parameters: `modules` - ### `moduleIds` `SyncHook` @@ -327,7 +297,6 @@ Called to assign an `id` to each module. - Callback Parameters: `modules` - ### `optimizeModuleIds` `SyncHook` @@ -336,7 +305,6 @@ Called at the beginning of the modules `id` optimization. - Callback Parameters: `modules` - ### `afterOptimizeModuleIds` `SyncHook` @@ -345,7 +313,6 @@ Called when the modules `id` optimization phase has completed. - Callback Parameters: `modules` - ### `reviveChunks` `SyncHook` @@ -354,7 +321,6 @@ Restore chunk information from records. - Callback Parameters: `chunks` `records` - ### `optimizeChunkOrder` `SyncHook` @@ -365,7 +331,6 @@ Sort the chunks in from most to least important. - Callback Parameters: `chunks` - ### `beforeChunkIds` `SyncHook` @@ -374,7 +339,6 @@ Executed before assigning an `id` to each chunk. - Callback Parameters: `chunks` - ### `chunkIds` `SyncHook` @@ -385,7 +349,6 @@ Called to assign an `id` to each chunk. - Callback Parameters: `modules` - ### `beforeOptimizeChunkIds` `SyncHook` @@ -396,7 +359,6 @@ Fired before chunks `id` optimization. - Callback Parameters: `chunks` - ### `optimizeChunkIds` `SyncHook` @@ -405,7 +367,6 @@ Called at the beginning of the chunks `id` optimization phase. - Callback Parameters: `chunks` - ### `afterOptimizeChunkIds` `SyncHook` @@ -414,7 +375,6 @@ Triggered after chunk `id` optimization has finished. - Callback Parameters: `chunks` - ### `recordModules` `SyncHook` @@ -423,7 +383,6 @@ Store module info to the records. This is triggered if [`shouldRecord`](#shouldr - Callback Parameters: `modules` `records` - ### `recordChunks` `SyncHook` @@ -432,7 +391,6 @@ Store chunk info to the records. This is only triggered if [`shouldRecord`](#sho - Callback Parameters: `chunks` `records` - ### `optimizeCodeGeneration` T> This hook will be available in v5.0.0 @@ -441,21 +399,18 @@ A plugin can tap into this hook to optimize the generated code. - Callback Parameters: `modules` - ### `beforeModuleHash` T> This hook will be available in v5.0.0 Called before hashing modules. - ### `afterModuleHash` T> This hook will be available in v5.0.0 Called after hashing modules. - ### `beforeRuntimeRequirements` T> This hook will be available in v5.0.0 @@ -464,28 +419,24 @@ Called before processing the modules required at runtime. - Callback Parameters: `entrypoints` - ### `afterRuntimeRequirements` T> This hook will be available in v5.0.0 Called after processing the runtime requirements. - ### `beforeHash` `SyncHook` Called before the compilation is hashed. - ### `afterHash` `SyncHook` Called after the compilation is hashed. - ### `recordHash` `SyncHook` @@ -494,7 +445,6 @@ Store information about record hash to the `records`. This is only triggered if - Callback Parameters: `records` - ### `record` `SyncHook` @@ -503,14 +453,12 @@ Store information about the `compilation` to the `records`. This is only trigger - Callback Parameters: `compilation` `records` - ### `beforeModuleAssets` `SyncHook` Executed before module assets creation. - ### `additionalChunkAssets` `SyncHook` @@ -519,22 +467,18 @@ Create additional assets for the chunks. - Callback Parameters: `chunks` - ### `shouldGenerateChunkAssets` `SyncBailHook` Called to determine whether or not generate chunks assets. Returning anything `!== false` will allow chunk assets generation. - ### `beforeChunkAssets` `SyncHook` Executed before creating the chunks assets. - - ### `additionalAssets` `AsyncSeriesHook` @@ -542,14 +486,16 @@ Executed before creating the chunks assets. Create additional assets for the compilation. This hook can be used to download an image, for example: -``` js +```js compilation.hooks.additionalAssets.tapAsync('MyPlugin', callback => { download('https://img.shields.io/npm/v/webpack.svg', function(resp) { - if(resp.status === 200) { + if (resp.status === 200) { compilation.assets['webpack-version.svg'] = toAsset(resp); callback(); } else { - callback(new Error('[webpack-example-plugin] Unable to download the image')); + callback( + new Error('[webpack-example-plugin] Unable to download the image') + ); } }); }); @@ -567,14 +513,14 @@ Any additional chunk assets are stored in `compilation.additionalChunkAssets`. Here's an example that simply adds a banner to each chunk. -``` js -compilation.hooks - .optimizeChunkAssets - .tapAsync('MyPlugin', (chunks, callback) => { +```js +compilation.hooks.optimizeChunkAssets.tapAsync( + 'MyPlugin', + (chunks, callback) => { chunks.forEach(chunk => { chunk.files.forEach(file => { compilation.assets[file] = new ConcatSource( - '\/**Sweet Banner**\/', + '/__Sweet Banner__/', '\n', compilation.assets[file] ); @@ -582,10 +528,10 @@ compilation.hooks }); callback(); - }); + } +); ``` - ### `afterOptimizeChunkAssets` `SyncHook` @@ -596,7 +542,7 @@ The chunk assets have been optimized. Here's an example plugin from [@boopathi](https://github.com/boopathi) that outputs exactly what went into each chunk. -``` js +```js compilation.hooks.afterOptimizeChunkAssets.tap('MyPlugin', chunks => { chunks.forEach(chunk => { console.log({ @@ -608,8 +554,6 @@ compilation.hooks.afterOptimizeChunkAssets.tap('MyPlugin', chunks => { }); ``` - - ### `optimizeAssets` `AsyncSeriesHook` @@ -618,7 +562,6 @@ Optimize all assets stored in `compilation.assets`. - Callback Parameters: `assets` - ### `afterOptimizeAssets` `SyncHook` @@ -627,21 +570,18 @@ The assets have been optimized. - Callback Parameters: `assets` - ### `needAdditionalSeal` `SyncBailHook` Called to determine if the compilation needs to be unsealed to include other files. - ### `afterSeal` `AsyncSeriesHook` Executed right after `needAdditionalSeal`. - ### `chunkHash` `SyncHook` @@ -650,7 +590,6 @@ Triggered to emit the hash for each chunk. - Callback Parameters: `chunk` `chunkHash` - ### `moduleAsset` `SyncHook` @@ -659,7 +598,6 @@ Called when an asset from a module was added to the compilation. - Callback Parameters: `module` `filename` - ### `chunkAsset` `SyncHook` @@ -668,7 +606,6 @@ Triggered when an asset from a chunk was added to the compilation. - Callback Parameters: `chunk` `filename` - ### `assetPath` `SyncWaterfallHook` @@ -677,14 +614,12 @@ Called to determine the path of an asset. - Callback Parameters: `path` `options` - ### `needAdditionalPass` `SyncBailHook` Called to determine if an asset needs to be processed further after being emitted. - ### `childCompiler` `SyncHook` @@ -693,7 +628,6 @@ Executed after setting up a child compiler. - Callback Parameters: `childCompiler` `compilerName` `compilerIndex` - ### `normalModuleLoader` Since webpack v5 `normalModuleLoader` hook was removed. Now to access the loader use `NormalModule.getCompilationHooks(compilation).loader` instead. diff --git a/src/content/api/compiler-hooks.md b/src/content/api/compiler-hooks.md index c432b0c775b4..7fe3115fd3da 100644 --- a/src/content/api/compiler-hooks.md +++ b/src/content/api/compiler-hooks.md @@ -9,6 +9,7 @@ contributors: - misterdev - EugeneHlushko - superburrito + - anikethsaha --- The `Compiler` module is the main engine that creates a compilation instance @@ -22,6 +23,7 @@ for more information. When developing a plugin for webpack, you might want to know where each hook is called. To learn this, search for `hooks..call` across the webpack source +W> Since webpack 5, `hooks` are no longer extendable. Use a `WeakMap` to add custom hooks. ## Watching @@ -33,14 +35,13 @@ the hood of tools like `webpack-dev-server`, so that the developer doesn't need to re-compile manually every time. Watch mode can also be entered via the [CLI](/api/cli/#watch-options). - ## Hooks The following lifecycle hooks are exposed by the `compiler` and can be accessed as such: -``` js -compiler.hooks.someHook.tap('MyPlugin', (params) => { +```js +compiler.hooks.someHook.tap('MyPlugin', params => { /* ... */ }); ``` @@ -49,7 +50,6 @@ Depending on the hook type, `tapAsync` and `tapPromise` may also be available. For the description of hook types, see [the Tapable docs](https://github.com/webpack/tapable#tapable). - ### `entryOption` `SyncBailHook` @@ -74,7 +74,6 @@ Called after setting up initial set of internal plugins. - Callback Parameters: `compiler` - ### `afterResolvers` `SyncHook` @@ -83,21 +82,18 @@ Triggered after resolver setup is complete. - Callback Parameters: `compiler` - ### `environment` `SyncHook` Called while preparing the compiler environment, right after inizializing the plugins in the configuration file. - ### `afterEnvironment` `SyncHook` Called right after the `environment` hook, when the compiler environment setup is complete. - ### `beforeRun` `AsyncSeriesHook` @@ -106,7 +102,6 @@ Adds a hook right before running the compiler. - Callback Parameters: `compiler` - ### `run` `AsyncSeriesHook` @@ -115,7 +110,6 @@ Hook into the compiler before it begins reading [`records`](/configuration/other - Callback Parameters: `compiler` - ### `watchRun` `AsyncSeriesHook` @@ -124,7 +118,6 @@ Executes a plugin during watch mode after a new compilation is triggered but bef - Callback Parameters: `compiler` - ### `normalModuleFactory` `SyncHook` @@ -133,7 +126,6 @@ Called after a `NormalModuleFactory` is created. - Callback Parameters: `normalModuleFactory` - ### `contextModuleFactory` `SyncHook` @@ -142,7 +134,6 @@ Runs a plugin after a `ContextModuleFactory` is created. - Callback Parameters: `contextModuleFactory` - ### `beforeCompile` `AsyncSeriesHook` @@ -156,7 +147,7 @@ The `compilationParams` variable is initialized as follows: ```js compilationParams = { normalModuleFactory, - contextModuleFactory, + contextModuleFactory }; ``` @@ -169,7 +160,6 @@ compiler.hooks.beforeCompile.tapAsync('MyPlugin', (params, callback) => { }); ``` - ### `compile` `SyncHook` @@ -178,7 +168,6 @@ Called right after `beforeCompile`, before a new compilation is created. - Callback Parameters: `compilationParams` - ### `thisCompilation` `SyncHook` @@ -187,7 +176,6 @@ Executed while initializing the compilation, right before emitting the `compilat - Callback Parameters: `compilation`, `compilationParams` - ### `compilation` `SyncHook` @@ -196,7 +184,6 @@ Runs a plugin after a compilation has been created. - Callback Parameters: `compilation`, `compilationParams` - ### `make` `AsyncParallelHook` @@ -205,7 +192,6 @@ Executed before finishing the compilation. - Callback Parameters: `compilation` - ### `afterCompile` `AsyncSeriesHook` @@ -214,7 +200,6 @@ Called after finishing and sealing the compilation. - Callback Parameters: `compilation` - ### `shouldEmit` `SyncBailHook` @@ -224,13 +209,12 @@ Called before emitting assets. Should return a boolean telling whether to emit. - Callback Parameters: `compilation` ```js -compiler.hooks.shouldEmit.tap('MyPlugin', (compilation) => { +compiler.hooks.shouldEmit.tap('MyPlugin', compilation => { // return true to emit the output, otherwise false return true; }); ``` - ### `emit` `AsyncSeriesHook` @@ -239,7 +223,6 @@ Executed right before emitting assets to output dir. - Callback Parameters: `compilation` - ### `afterEmit` `AsyncSeriesHook` @@ -252,7 +235,7 @@ Called after emitting assets to output directory. `AsyncSeriesHook` -Executed when an asset has been emitted. Provides access to information about the emitted asset, such as its output path and byte content. +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` @@ -267,7 +250,6 @@ compiler.hooks.assetEmitted.tap( ); ``` - ### `done` `AsyncSeriesHook` @@ -276,7 +258,6 @@ Executed when the compilation has completed. - Callback Parameters: `stats` - ### `failed` `SyncHook` @@ -285,7 +266,6 @@ Called if the compilation fails. - Callback Parameters: `error` - ### `invalid` `SyncHook` @@ -294,7 +274,6 @@ Executed when a watching compilation has been invalidated. - Callback Parameters: `fileName`, `changeTime` - ### `watchClose` `SyncHook` @@ -309,7 +288,6 @@ Allows to use infrastructure logging when enabled in the configuration via [`inf - Callback Parameters: `name`, `type`, `args` - ### `log` `SyncBailHook` diff --git a/src/content/api/parser.md b/src/content/api/parser.md index 81cd5e4ae068..672f673906e5 100644 --- a/src/content/api/parser.md +++ b/src/content/api/parser.md @@ -7,6 +7,7 @@ contributors: - DeTeam - misterdev - EugeneHlushko + - anikethsaha --- The `parser` instance, found in the `compiler`, is used to parse each module @@ -34,6 +35,7 @@ depending on the type of hook. The following lifecycle hooks are exposed by the `parser` and can be accessed as such: +W> Since webpack 5, `hooks` are no longer extendable. Use a `WeakMap` to add custom hooks. ### evaluateTypeof