Skip to content

Commit 2426064

Browse files
committed
fix(docs): add watch mode plugin example
1 parent c8cb8e2 commit 2426064

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,11 @@ This is useful for plugins that need to react only to specific file changes
314314
```js
315315
class WatchNotifierPlugin {
316316
apply(compiler) {
317-
compiler.hooks.watchRun.tap('WatchNotifierPlugin', (compiler) => {
317+
compiler.hooks.watchRun.tap("WatchNotifierPlugin", (compiler) => {
318318
if (compiler.modifiedFiles) {
319-
const changedFiles = Array.from(compiler.modifiedFiles)
319+
const changedFiles = [...compiler.modifiedFiles]
320320
.map((file) => `${file}`)
321-
.join('\n');
321+
.join("\n");
322322

323323
console.log(`\nFiles changed:\n${changedFiles}`);
324324
}
@@ -328,7 +328,9 @@ class WatchNotifierPlugin {
328328

329329
module.exports = WatchNotifierPlugin;
330330
```
331+
331332
> **Note**:
333+
>
332334
> - `compiler.modifiedFiles` is a `Set`, not an array
333335
> - It is `undefined` on the first (cold) build
334336
> - It is only populated during watch rebuilds
@@ -341,12 +343,12 @@ that webpack does not track by default, you must tell webpack to watch them.
341343
The `compilation.fileDependencies` set allows you to add such files.
342344

343345
```js
344-
const path = require('path');
346+
const path = require("node:path");
345347

346348
class TemplateWatchPlugin {
347349
apply(compiler) {
348-
compiler.hooks.compilation.tap('TemplateWatchPlugin', (compilation) => {
349-
const templatePath = path.resolve(__dirname, 'my-template.html');
350+
compiler.hooks.compilation.tap("TemplateWatchPlugin", (compilation) => {
351+
const templatePath = path.resolve(__dirname, "my-template.html");
350352

351353
// Ensure webpack watches this file
352354
compilation.fileDependencies.add(templatePath);
@@ -356,6 +358,7 @@ class TemplateWatchPlugin {
356358

357359
module.exports = TemplateWatchPlugin;
358360
```
361+
359362
Without calling `fileDependencies.add()`, webpack will not trigger
360363
a rebuild when the file changes — even if your plugin depends on it.
361364

0 commit comments

Comments
 (0)