Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Stale virtual module contents upon ".writeModule" #89

Closed
1 task done
kettanaito opened this issue Jan 21, 2021 · 6 comments
Closed
1 task done

[Bug] Stale virtual module contents upon ".writeModule" #89

kettanaito opened this issue Jan 21, 2021 · 6 comments
Labels

Comments

@kettanaito
Copy link

kettanaito commented Jan 21, 2021

  • I'd be willing to submit the fix

Describe the bug

I have a file system watcher (gatsby-page-utils). I derive a JSON from the file changes (addition/removal) of a given directory. I then use your plugin to write the contents of that JSON as a virtual module for my build.

The issue is that when a file is added/removed, although .writeModule is called with the right JSON, the value of the virtual module remains stale. Actually, I believe it remains the same as when the build was done. Nothing changes the value: working with files, changing the build's entrypoint.

To Reproduce

  1. Checkout this pull request.
  2. yarn install && yarn build
  3. Run yarn reproduce. This will open a local dev server.
  4. Open the DevTools, switch to "Console".
  5. See the virtual module's contents (entry.js) in the console.
  6. Try adding/removing *.js files in the examples/virtual-issue directory.
  7. See the console outputting the same JSON.

You can observe the value of the serializedPages in the FileRouterPlugin plugin to see that .writeModule() call receives updated value.

Environment if relevant (please complete the following information):

  • OS: MacOS Catalina 10.15.5
  • Node version: v12.18.0
  • Webpack version: 5.15.0

Additional context

I'm building a plugin that represents a given directory as a JSON, watches for changes, and exposes that JSON to your bundle. I'd love to use your plugin underneath to provide that JSON as a virtual module. Thank you for creating this plugin!

@kettanaito kettanaito added the bug label Jan 21, 2021
@kettanaito
Copy link
Author

I'm expecting the .writeModule() method to update the virtual module's contents as mentioned in this article. I've also found this comment stating that the method should behave similarly to fs.writeFile, which would update the contents of the file.

@camoonke
Copy link

same problem in webpack5

@xwchris
Copy link

xwchris commented Nov 10, 2021

Have same problem, anyone resolved this?

@christopherjbaker
Copy link

I am also running into this. I've tried fixes mentioned in #128 and #68 to no avail. Has anybody looked deeper into this yet or had any luck?

@andersekdahl
Copy link

andersekdahl commented May 6, 2022

I've been having the same issue of everything working on build but nothing happens during watch mode.

I've been able to localize it to this line for webpack5:

if (fileWatcher.path === modulePath) {

All the uses of fileWatcher there should be fileWatcher.watcher. So fileWatcher.watcher.path and fileWatcher.watcher.directoryWatcher etc rather than fileWatcher.path and fileWatcher.directoryWatcher. After that the change event is propagated correctly and I can tell that watchpack signals an aggregated change event for the virtual modules.

However I'm still not getting any compiled output. It's as if webpack sees the event but decides that nothing has happened and skips outputting anything.

EDIT:
After cowboy applying the fixes in this PR #128 the watch mode started to work for me with webpack 5.72.0.

@larixer
Copy link
Member

larixer commented Nov 1, 2022

Closing the issue as stale, please retest on latest webpack-virtual-modules and reopen with reproduction steps if the issue still exists.

@larixer larixer closed this as completed Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants