You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/contribute/writing-a-plugin.mdx
+76Lines changed: 76 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -300,6 +300,82 @@ T> We are using synchronous `tap()` method to tap into the `processAssets` hook
300
300
301
301
T> The [`processAssets`](/api/compilation-hooks/#processassets) hook also supports the `additionalAssets` property, that allows your plugin to intercept not only assets that were added by other plugins prior to the execution of the specified stage, but also for assets that were added on a later stages. This allows to intercept absolutely all the assets which are part of the compilation. However, in our example we are fine with using the `SUMMARIZE` stage to capture all the assets generated on previous stages (this should account for all assets in general case).
302
302
303
+
## Watching for file changes
304
+
305
+
When webpack runs in watch mode (`webpack --watch` or `webpack serve`),
306
+
it creates a new compilation for each rebuild triggered by file changes.
307
+
308
+
The `compiler.modifiedFiles` Set lets your plugin know **which specific
309
+
files triggered the rebuild**, so you can skip expensive work for unrelated files.
310
+
311
+
This is useful for plugins that need to react only to specific file changes
312
+
(e.g., regenerating assets, reprocessing templates, or invalidating caches).
> -`compiler.modifiedFiles` is a `Set`, not an array
335
+
> - It is `undefined` on the first (cold) build
336
+
> - It is only populated during watch rebuilds
337
+
338
+
### Adding custom file dependencies
339
+
340
+
If your plugin reads external files (config files, templates, etc.)
341
+
that webpack does not track by default, you must tell webpack to watch them.
342
+
343
+
You can tell webpack to watch different types of dependencies:
344
+
345
+
-`compilation.fileDependencies` is used to track individual files that your plugin depends on, so webpack can trigger a rebuild when those files change
346
+
347
+
-`compilation.contextDependencies` is used to watch directories, so any change inside them triggers a rebuild
348
+
349
+
-`compilation.missingDependencies` is used to track files that are currently missing, so webpack can trigger a rebuild when they are created
Without calling `fileDependencies.add()`, webpack will not trigger
377
+
a rebuild when the file changes — even if your plugin depends on it.
378
+
303
379
## Different Plugin Shapes
304
380
305
381
A plugin can be classified into types based on the event hooks it taps into. Every event hook is pre-defined as synchronous or asynchronous or waterfall or parallel hook and hook is called internally using call/callAsync method. The list of hooks that are supported or can be tapped into is generally specified in `this.hooks` property.
0 commit comments