Skip to content
Merged
30 changes: 30 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,33 @@ 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 during compilation.

```js
class AssetLoggerPlugin {
apply(compiler) {
compiler.hooks.emit.tap("AssetLoggerPlugin", (compilation) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emit is deprecated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I’ve updated the example to use the
thisCompilation and processAssets hooks instead of the deprecated emit hook.

console.log("Generated assets:");
for (const assetName of Object.keys(compilation.assets)) {
console.log(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