You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Plugin onchange hook for notifications when a file change is detected #2383
modular-css-rollup is already using the asset APIs and the transform dependency systems, those have been lovely quality-of-life improvements to my plugin. I still struggle with my transform hook and understanding why it was called. There's no way for it to know if the actual id was the changed file, or if it was one of its dependencies. Since modular-css-rollup is keeping its own cache of file results under-the-covers for perf reasons this makes it really tricky to invalidate that cache when things change w/ the rollup watcher.
Feature Proposal
I'd like to see a new plugin hook onchanged or something. Naming isn't important, but I'd like a function callback that my plugin can provide that will be called whenever the watcher notices that a file changed. Then I can do whatever housekeeping I need to and my transform hook can be drastically simplified and just focus on the transform bit instead of trying to handle both things in one spot.
In cases of watched builds, we make this.watcher available to plugins.
I forget the exact event, but I believe this is something like - if (this.watcher) this.watcher.on('event', handleWatcherEvent).
This isn't properly documented as it is fairly recent, and we're still considering the stability of this (particularly around the event structure and shape still), but let me know what you think of that as an option.
I prefer the former, since it makes it less likely that you'll forget the guard and blow something up when not running in watch mode. If you're not watching it would be a no-op since the event would never fire.
Also I find the watcher doing everything via a single event and requiring inspection of the the code property super-annoying, but I'm sure having a single subscription is something some people like.
Yeah, that's what I was originally proposing and still my favorite approach. I don't mind an event listener if it's easier to accomplish though.
I'm looking at the rollup watching code now to try and puzzle my way through tasks and watchers and invalidations to see if I can send a PR. Not making much headway yet, the rollup codebase always gives me trouble for some reason. 😥
When a transform runs I check if the modular-css instance knows about the file, and I also keep track of how many times rollup has run. If it's not the first run and it's a known file, I compare the code value vs what the instance has. If they're different, I know the id file changed. Otherwise I have to go and check the code on disk for each one of the ids dependencies against what modular-css has until I figure out which file actually changed and triggered the rebuild.
It's awkward and slower than it needs to be since I'm doing a bunch of disk access. I could start statting files and keeping that info around to speed it up some but this would be a trivial thing to do with a watchFileChange hook.
The filePath passed to the watchFileChange should be the path to the actual file that changed, whether it was the original file found by rollup or one of the dependencies returned by the transform hook. I understand why transform gets passed the original file id every time but for this I'd want to know the exact file that changed.
I'm looking at the rollup watching code now to try and puzzle my way through tasks and watchers and invalidations to see if I can send a PR. Not making much headway yet, the rollup codebase always gives me trouble for some reason. 😥
Sorry to hear that, if you care to share where you got stuck, I'd be happy to try offer suggestions on how to get there.
Glad you found a workaround for now, it would be great to start moving in these directions though.
Activity
guybedford commentedon Aug 7, 2018
In cases of watched builds, we make
this.watcheravailable to plugins.I forget the exact event, but I believe this is something like -
if (this.watcher) this.watcher.on('event', handleWatcherEvent).This isn't properly documented as it is fairly recent, and we're still considering the stability of this (particularly around the event structure and shape still), but let me know what you think of that as an option.
tivac commentedon Aug 7, 2018
The only watcher events I get when listening for
eventare:STARTBUNDLE_STARTBUNDLE_ENDENDNone of which have any data attached that looks like it'll tell me which files on disk actually changed.
guybedford commentedon Aug 7, 2018
AH. Yes we should have finer-grained info here.
I guess the question then is should we double-down on this interface, or provide a more general events system -
or
tivac commentedon Aug 7, 2018
I prefer the former, since it makes it less likely that you'll forget the guard and blow something up when not running in watch mode. If you're not watching it would be a no-op since the event would never fire.
Also I find the watcher doing everything via a single event and requiring inspection of the the
codeproperty super-annoying, but I'm sure having a single subscription is something some people like.guybedford commentedon Aug 7, 2018
The alternative could just be to treat this as a hook like any other for the plugin instead of introducing a whole new watcher pipeline:
tivac commentedon Aug 7, 2018
Yeah, that's what I was originally proposing and still my favorite approach. I don't mind an event listener if it's easier to accomplish though.
I'm looking at the rollup watching code now to try and puzzle my way through tasks and watchers and invalidations to see if I can send a PR. Not making much headway yet, the rollup codebase always gives me trouble for some reason. 😥
tivac commentedon Aug 8, 2018
Here's what I'm doing currently since this hook doesn't exist:
tivac/modular-css@cd34a6f
When a
transformruns I check if themodular-cssinstance knows about the file, and I also keep track of how many times rollup has run. If it's not the first run and it's a known file, I compare thecodevalue vs what the instance has. If they're different, I know theidfile changed. Otherwise I have to go and check the code on disk for each one of theids dependencies against whatmodular-csshas until I figure out which file actually changed and triggered the rebuild.It's awkward and slower than it needs to be since I'm doing a bunch of disk access. I could start statting files and keeping that info around to speed it up some but this would be a trivial thing to do with a
watchFileChangehook.The
filePathpassed to thewatchFileChangeshould be the path to the actual file that changed, whether it was the original file found by rollup or one of thedependenciesreturned by thetransformhook. I understand whytransformgets passed the original fileidevery time but for this I'd want to know the exact file that changed.guybedford commentedon Aug 9, 2018
Sorry to hear that, if you care to share where you got stuck, I'd be happy to try offer suggestions on how to get there.
Glad you found a workaround for now, it would be great to start moving in these directions though.
guybedford commentedon Aug 13, 2018
@tivac I've created #2405.