Skip to content

fix: handle addWatchFile in load hooks - #14967

Merged
patak-cat merged 3 commits into
mainfrom
pluginContainer-addWatchFile-load
Nov 13, 2023
Merged

patak-cat merged 3 commits into
mainfrom
pluginContainer-addWatchFile-load

Conversation

@bluwy

@bluwy bluwy commented Nov 13, 2023

Copy link
Copy Markdown
Member

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

@patak-cat patak-cat left a comment

Copy link
Copy Markdown
Member

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

bluwy commented Nov 13, 2023

Copy link
Copy Markdown
Member Author

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-cat

Copy link
Copy Markdown
Member

/ecosystem-ci run

@vite-ecosystem-ci

Copy link
Copy Markdown

📝 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-cat
patak-cat merged commit a0ab85b into main Nov 13, 2023
@patak-cat
patak-cat deleted the pluginContainer-addWatchFile-load branch November 13, 2023 18:06
@patak-cat

Copy link
Copy Markdown
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