Skip to content

docs(plugin): add watch mode plugin example with modifiedFiles and fileDependencies#8125

Merged
alexander-akait merged 6 commits intowebpack:mainfrom
raj-sapalya:docs/watch-mode-plugin-example
Mar 27, 2026
Merged

docs(plugin): add watch mode plugin example with modifiedFiles and fileDependencies#8125
alexander-akait merged 6 commits intowebpack:mainfrom
raj-sapalya:docs/watch-mode-plugin-example

Conversation

@raj-sapalya
Copy link
Copy Markdown
Contributor

@raj-sapalya raj-sapalya commented Mar 26, 2026

What

Adds a new section explaining how to work with file changes in watch mode.

Includes:

  • Example using compiler.modifiedFiles to detect changed files
  • Example using compilation.fileDependencies to watch external files
  • Explanation of behavior during cold build vs rebuild

Why

The documentation references watch mode hooks and APIs, but does not provide
a practical example showing how to use them in a plugin.

This PR fills that gap with real-world usage examples.

How

  • Added a new section under writing-a-plugin
  • Included two plugin examples with explanations
  • Focused on practical usage for plugin authors

Impact

Improves developer understanding of webpack watch mode and plugin behavior,
especially for advanced use cases involving file tracking.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
webpack-js-org Ready Ready Preview, Comment Mar 27, 2026 6:25am

Request Review

const templatePath = path.resolve(__dirname, "my-template.html");

// Ensure webpack watches this file
compilation.fileDependencies.add(templatePath);
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.

Please add example how to add context and missing dependencies too

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.

I’ve added examples for contextDependencies and missingDependencies as suggested. Let me know if anything else is needed.

You can also watch directories and even files that do not yet exist:

- `compilation.contextDependencies` → for directories
- `compilation.missingDependencies` → for future files
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.

Please rewrite it because it is misleading, union with before text and description for each

Copy link
Copy Markdown
Contributor Author

@raj-sapalya raj-sapalya Mar 26, 2026

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 section to integrate contextDependencies and missingDependencies with clearer explanations
Let me know if anything else should be improved

@@ -342,10 +342,14 @@ that webpack does not track by default, you must tell webpack to watch them.

The `compilation.fileDependencies` set allows you to add such files.
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.

Please union your description with it

Copy link
Copy Markdown
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Please fix union, also please use ECMA modules syntax only

@raj-sapalya
Copy link
Copy Markdown
Contributor Author

Please fix union, also please use ECMA modules syntax only

I’ve updated the section to unify the dependency explanations and switched the example to ECMAScript module syntax. Let me know if anything else should be improved.

If your plugin reads external files (config files, templates, etc.)
that webpack does not track by default, you must tell webpack to watch them.

The `compilation.fileDependencies` set allows you to add files that your plugin depends on, so webpack can trigger a rebuild when those files change.
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.

Let's use the same pattern for compilation.contextDependencies and compilation.missingDependencies, or move compilation.fileDependencies under list, use the same style for description them

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 section to use a consistent list format for fileDependencies, contextDependencies, and missingDependencies, and changed the wording to use webpack serve
Let me know if anything else should be improved


## Watching for file changes

When webpack runs in watch mode (`webpack --watch` or `devServer`),
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.

webpack watch or webpack serve

@alexander-akait alexander-akait merged commit 8d9e86b into webpack:main Mar 27, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants