Support new Function in @cloudflare/vite-plugin dev module runner#13905
Support new Function in @cloudflare/vite-plugin dev module runner#13905arthurfiorette wants to merge 4 commits into
new Function in @cloudflare/vite-plugin dev module runner#13905Conversation
🦋 Changeset detectedLatest commit: cf9d55f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
jamesopstad
left a comment
There was a problem hiding this comment.
Hi @arthurfiorette. Thanks for looking into this!
Luckily, my problem is with AJV that uses
new Function(and noteval). So i can use the same hacky workaroundvitest-pool-workersdoes withensurePatchedFunction.
The problem with this approach is that it also permits using new Function() within a request context, which is forbidden and will error in production. new Function() should only be permitted during the startup phase of a Worker and this functionality is enabled by the compatibility_date or allow_eval_during_startup compatibility flag. As you've realised, this doesn't currently work in vite dev. I think it would be worse to fix this in dev though by allowing features that would break in production.
I'm for sure not an expert in this topic, but it seems the proper fix here would be to architecturally change vite’s development server to feed transformed modules into workerd as real modules instead. I think I can assume it would be a hell of a change, and I KNOW that HMR is not going to like that lol
Yeah, the problem with this is that you would lose a lot of the benefits of Vite i.e. HMR and not needing to restart workerd on every code change.
We are planning to fix the inaccuracies that come from using Vite's transformed modules. #9489 is another related issue. It's quite complicated though and will most likely require some changes to workerd to enable emulating real modules more accurately in dev.
IM NOT AN AI :)
Hey! I'm in the process of making fastify work in Cloudflare Workers!
A lot of things are working already (fastify startup & plugin lifecycles, schema compilation, I/O serialization...), but that forced me to patch a lot of things on fastify itself, which then forced me to ditch
wrangler devin favor of a full robust vite setup using@cloudflare/vite-plugin.For my surprise, as soon as I changed to vite, everything blew up again, after some debugging, I discovered the fault was vite: eval at startup is not working during
vite dev.wrangler devworks,wrangler deploy& deployed url works,vite build && wrangler deployalso works, but in vite dev it didn't.After checking out this repo and clauding around, I realized vite dev server runs userland modules with
ModuleRunnerinstead of passing them directly to workerd native module startup aswrangler devdoes.Luckily, my problem is with AJV that uses
new Function(and noteval). So i can use the same hacky workaroundvitest-pool-workersdoes withensurePatchedFunction.Obviously
evalis not going to be fixed with this PR, If I'm not wrong, eval depends on the local lexical scope, so patching it globally could cause problems. Tbh I don't think a lot of libraries out actually depend onevaland not onnew Function.I'm for sure not an expert in this topic, but it seems the proper fix here would be to architecturally change vite’s development server to feed transformed modules into workerd as real modules instead. I think I can assume it would be a hell of a change, and I KNOW that HMR is not going to like that lol
Tests should be passing. I also removed the fix, reran the tests, and confirmed that they broke again.
A picture of a cute animal (not mandatory, but encouraged)