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

Vite plugins shouldn't have side effects #12973

Closed
aaronadamsCA opened this issue Feb 7, 2025 · 2 comments
Closed

Vite plugins shouldn't have side effects #12973

aaronadamsCA opened this issue Feb 7, 2025 · 2 comments
Labels

Comments

@aaronadamsCA
Copy link

I'm using React Router as a...

framework

Reproduction

I don't have one yet. I think a failing unit test will be the best way, but I will need feedback on how to write it first.

System Info

Affects all environments.

Used Package Manager

pnpm

Expected Behavior

If my Vite config contains the React Router Vite Cloudflare plugin, and I call Vite resolveConfig(), the plugin should not spawn a workerd process.

Actual Behavior

Apparently it does spawn a workerd process, as I reported to Nx here:

nrwl/nx#28892

Their reply sent me down a rabbit hole, and I think what I've learned is that you should not spawn a server in the Vite configureServer hook, because it looks like Vite's resolveConfig() API calls configureServer() internally.

So every time an outside tool merely resolves the Vite config, a Wrangler dev server starts and never exits.

In the case of the Nx Vite plugin, which regularly resolves the Vite config, this causes an unstable dev environment with frequent crashes, due to memory and port exhaustion.

My best idea so far is to write a test with a mocked Wrangler module that just throws an error, then call Vite's resolveConfig() API and make sure it resolves. Let me know if that sounds like the right idea or if there's a better way.

From there, a fix is "just" a matter of figuring out which Vite hook is the right one to use to start Wrangler. I think it might be buildStart but I'm not sure.

@jamesopstad
Copy link

Their reply sent me down a rabbit hole, and I think what I've learned is that you should not spawn a server in the Vite configureServer hook, because it looks like Vite's resolveConfig() API calls configureServer() internally.

I don't think resolveConfig calls configureServer internally. This is more likely because React Router calls vite.createServer in configResolved to create a child compiler.

viteChildCompiler = await vite.createServer({

This means that a dev server is spawned when resolving the config.

Copy link
Contributor

🤖 Hello there,

We just published version 7.2.0-pre.2 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

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

2 participants