Skip to content

Plugin onchange hook for notifications when a file change is detected #2383

Closed

Description

@tivac

Feature Use Case

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.

Activity

  1. guybedford commented on Aug 7, 2018

    @guybedford
    Contributor

    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.

  2. tivac commented on Aug 7, 2018

    @tivac
    ContributorAuthor

    The only watcher events I get when listening for event are:

    • START
    • BUNDLE_START
    • BUNDLE_END
    • END

    None of which have any data attached that looks like it'll tell me which files on disk actually changed.

  3. guybedford commented on Aug 7, 2018

    @guybedford
    Contributor

    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 -

    this.on('watchFileChange', handler);

    or

    if (this.watcher)
      this.watcher.on('event', eventHandlerFiltering_FILE_CHANGE_EVENT)
  4. tivac commented on Aug 7, 2018

    @tivac
    ContributorAuthor

    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.

  5. guybedford commented on Aug 7, 2018

    @guybedford
    Contributor

    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:

    {
      watchFileChange (filePath: string) {
      }
    }
  6. tivac commented on Aug 7, 2018

    @tivac
    ContributorAuthor

    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. 😥

  7. tivac commented on Aug 8, 2018

    @tivac
    ContributorAuthor

    Here's what I'm doing currently since this hook doesn't exist:

    tivac/modular-css@cd34a6f

    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.

  8. guybedford commented on Aug 9, 2018

    @guybedford
    Contributor

    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.

  9. guybedford commented on Aug 13, 2018

    @guybedford
    Contributor

    @tivac I've created #2405.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions