Skip to content

Vite plugins shouldn't have side effects #12973

Closed
@aaronadamsCA

Description

@aaronadamsCA

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions