Skip to content

Commit 8d9e86b

Browse files
authored
docs(plugin): add watch mode plugin example with modifiedFiles and fileDependencies (#8125)
1 parent 664fc79 commit 8d9e86b

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/content/contribute/writing-a-plugin.mdx

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,82 @@ T> We are using synchronous `tap()` method to tap into the `processAssets` hook
300300

301301
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).
302302

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).
313+
314+
```js
315+
class WatchNotifierPlugin {
316+
apply(compiler) {
317+
compiler.hooks.watchRun.tap("WatchNotifierPlugin", (compiler) => {
318+
if (compiler.modifiedFiles) {
319+
const changedFiles = [...compiler.modifiedFiles]
320+
.map((file) => `${file}`)
321+
.join("\n");
322+
323+
console.log(`\nFiles changed:\n${changedFiles}`);
324+
}
325+
});
326+
}
327+
}
328+
329+
export default WatchNotifierPlugin;
330+
```
331+
332+
> **Note**:
333+
>
334+
> - `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
350+
351+
```js
352+
import path from "node:path";
353+
354+
class TemplateWatchPlugin {
355+
apply(compiler) {
356+
compiler.hooks.compilation.tap("TemplateWatchPlugin", (compilation) => {
357+
const templatePath = path.resolve(__dirname, "my-template.html");
358+
359+
// Ensure webpack watches this file
360+
compilation.fileDependencies.add(templatePath);
361+
362+
// Watch a directory (context dependency)
363+
const templatesDir = path.resolve(__dirname, "templates");
364+
compilation.contextDependencies.add(templatesDir);
365+
366+
// Example: mark a missing dependency
367+
const missingFile = path.resolve(__dirname, "missing-file.txt");
368+
compilation.missingDependencies.add(missingFile);
369+
});
370+
}
371+
}
372+
373+
export default TemplateWatchPlugin;
374+
```
375+
376+
Without calling `fileDependencies.add()`, webpack will not trigger
377+
a rebuild when the file changes — even if your plugin depends on it.
378+
303379
## Different Plugin Shapes
304380

305381
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.

src/utilities/cn.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable import/no-extraneous-dependencies */
21
import { clsx } from "clsx";
32
import { twMerge } from "tailwind-merge";
43

0 commit comments

Comments
 (0)