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

fix: handle addWatchFile in load hooks #14967

Merged
merged 3 commits into from
Nov 13, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Nov 13, 2023

Description

fix #3474

The _addedImports of the plugin context only exists for each hook in isolation, e.g. load() and transform(). In importAnalysis.ts, we handle _addedImports in the transform() hook, so only addWatchFile in transform() hook works. load() was always being ignored.

This PR saves the _addedImports from load() (cached via ModuleNode weak map), and re-instates in the transform() hook later.

NOTE: this is not supported for CSS, looks like we hardcode dependency handling for our CSS plugin only.

Additional context

https://rollupjs.org/plugin-development/#this-addwatchfile

I also tried supporting this in resolveId() as the Rollup docs mention that it supports it, however I reverted it (see 3rd commit) as it doesn't seem to work in Rollup (vite build --watch). Plus the implementation to support in resolveId() has a few downsides:

  • Since resolveId doesn't guarantee a ModuleNode yet, we have to cache by the id string (unable to GC)
  • resolveId can be called but we don't necessarily want to load it. Saving the _addedImports could pollute subsequent load + transform

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines, especially the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

@bluwy bluwy added the p3-minor-bug An edge case that only affects very specific usage (priority) label Nov 13, 2023
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

I think we can merge this one now, but our approach in general to communicate from one hook context to another suffers from possible race conditions. This one is similar to what we do with module.meta. I think it would be good to have something like a pipeline context with this information passed to resolveId/load/transform to pass this information in transformRequest at one point.

@bluwy
Copy link
Member Author

bluwy commented Nov 13, 2023

Yeah the approach here isn't the best either. A higher-level pipeline does sound like the better solution, which I think it roughly resides in transformRequest.ts today. Maybe it's better to handle the _addedImports there?

It'll be a bit tricky to pass it around, or otherwise I think we can also stick with this implementation for now and improve later.

@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 94fb93d: Open

suite result latest scheduled
analogjs failure failure
astro success success
histoire failure failure
ladle success success
laravel failure failure
marko success success
nuxt failure failure
nx failure failure
previewjs success success
qwik failure failure
rakkas success failure
sveltekit failure failure
unocss success success
vike failure failure
vite-plugin-pwa success success
vite-plugin-react success success
vite-plugin-react-pages success success
vite-plugin-react-swc success success
vite-plugin-svelte success success
vite-plugin-vue success success
vite-setup-catalogue success success
vitepress success success
vitest success success

@patak-dev patak-dev merged commit a0ab85b into main Nov 13, 2023
10 checks passed
@patak-dev patak-dev deleted the pluginContainer-addWatchFile-load branch November 13, 2023 18:06
@patak-dev
Copy link
Member

Lets go with this implementation for now, and we can later improve it 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

this.addWatchFile() in the load() hook does not work
2 participants