Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/content/contribute/writing-a-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,42 @@ Various types of hooks supported are :
### Configuration defaults

Webpack applies configuration defaults after plugins defaults are applied. This allows plugins to feature their own defaults and provides a way to create configuration preset plugins.

### Example: AssetLoggerPlugin

The following example shows a minimal Webpack plugin that logs all generated assets using Webpack’s logger interface.

```js
class AssetLoggerPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap("AssetLoggerPlugin", (compilation) => {
const logger = compilation.getLogger("AssetLoggerPlugin");
compilation.hooks.processAssets.tap(
{
name: "AssetLoggerPlugin",
stage: compilation.constructor.PROCESS_ASSETS_STAGE_SUMMARIZE,
},
(assets) => {
logger.info("Generated assets:");
for (const assetName of Object.keys(assets)) {
logger.info(assetName);
}
},
);
});
}
}

module.exports = AssetLoggerPlugin;
```

This plugin taps into the `emit` hook of the Webpack compiler and prints the names of all generated assets to the console. It demonstrates how plugins can interact with the compilation process using Webpack hooks.

For example, when running a Webpack build, the output may look like:

```text
Generated assets:
main.js
vendor.js
styles.css
```
Loading