How to Trigger File Watch Recompile in Webpack 5 Without Modifying Files?

I’m developing a Webpack 5 plugin and need to manually trigger a recompile for a watched file without actually modifying it. This is primarily for some advanced HMR (Hot Module Replacement) use cases.

I came across this question on Stack Overflow and its solution, which involves using compiler.watchFileSystem to manipulate the file watcher. However, the provided solution seems outdated and incompatible with Webpack 5, as I couldn’t find:

compiler.watchFileSystem.watcher.fileWatchers.get(filePath).directoryWatcher.setFileTime( filePath, new Date().getTime(), false, false, "change" );

or:

compiler.watchFileSystem.watcher.fileWatchers.find(...).directoryWatcher.setFileTime(...)
The compiler.watchFileSystem object in my setup lacks the watcher property entirely, and thus the fileWatchers map is also unavailable.

What I’ve Tried:

  • Searching through Webpack 5’s source code to find if the watcher and fileWatchers are renamed or restructured.
  • Reading through the Webpack 5 documentation, but there doesn’t seem to be clear guidance on this.
  • Looking for other ways to simulate a “file change” event, but I couldn’t find anything concrete.

My Goal:
I want to:

  • Convince Webpack that a specific file has changed (without actually modifying the file on disk).
  • Trigger a recompilation for that file.

Does anyone know how to achieve this in Webpack 5? Alternatively, is there another approach to trigger a recompile programmatically for a specific file?

Any guidance or examples would be greatly appreciated!