-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
[SSR] Stop resolving externalized dependencies #4340
Comments
After some digging the root cause of the At least Vite doesn't do dep transpilation here, but it still does dep resolving. (I.e. Vite uses it's owner node resolver instead of using Node.js' built-in Vite doing dep resolving is the root cause of the Vuetify error. |
I'm not getting the same error when running your reproduction at vuetifyjs/vuetify#13936:
So it seems like
I don't think it does? But not clear on your definition of "touches" here. |
@aleclarson Yes that's because of this commit brillout/vite-plugin-ssr-vuetify@9ed87bb as it helps Vite's custom resolver.
I wasn't sure but actually Vite doesn't seem to transpiled externalized deps (correctly as expected). But it does, however, try to resolve which sometimes fails, e.g. Vuetify. I reverted the commit at https://github.com/brillout/vuetify3 to reproduce:
|
That seems like a reason to fix the resolution algorithm, rather than treat externalized deps differently when resolving them. Have you seen if #3951 fixes it? |
My points here are:
|
Once #3951 is merged, modules loaded with const loadModule = Module.createRequire(importer || resolveOptions.root + '/')
No reason currently, I think. Vite could just use |
I'd argue that Vite should not try to resolve externalized dependencies then. This is what I expect as a user, and also what the ecocystem at large expects.
👍 Neat. Although I'm still wondering; why doesn't Vite use Node.js's built-in |
One thing I'll mention that is somewhat SvelteKit-specific is that we need to bundle all third-party Svelte components because they are distributed as source and need to be run through the Svelte compiler. By far the biggest issue we have is that Svelte components typically get distributed as ESM, but if you encounter one with a CJS dependency it will fail. If we can stop doing |
@benmccann Your comment seems out of place here, since this thread is about module resolution, not module loading. Nonetheless, have you tried using
@brillout It's not really an issue if Vite handles it properly. Besides, that would prevent |
Like patching Vite to do that? What would get compiled? The ESM to CJS? I think getting that heavy into Vite's internals is far beyond my knowledge of Vite. But why would that be required? I think that if we use Node for the resolving logic we should consider using Node for the loading logic as well because Node has built-in logic to handle it already, so why recreate it?
I'm not incredibly familiar with Vite's internals, but in my mind, the two are highly tied together because the reason you need to do module resolution is for module loading. E.g. Vite can't load ESM modules today because it always tries to
I'm relatively sure that it does: https://nodejs.org/api/esm.html#esm_interoperability_with_commonjs. It's the #1 thing that users report as far as SSR issues go, so I have to imagine Node supports it based on how many reports of it I've seen. E.g. I just saw that the other day with the |
ESM PlanThe fact that Node.js forces a user to migrate her code from CJS to ESM when she adds even only a single ESM-only dependency is quite brutal. I don't think it's a Node.js design flaw, quite the contrary actually. For example, it's been a month that I was actualy skeptical that ESM will ever get to mass adoption for Node.js user-code, but I'm now confident ESM will spread everywhere even for Node.js user-code. We only need a couple of widely used packages to become ESM-only, and ESM will soon become the de-facto standard. Let's imagine I've got a Node.js app written with CJS using
That's a good thing and that's the whole plan here: ESM is spreading. Vite should respect that clever plan. So, the only thing Vite should do here is to check whether the user-code's If a Vite user wants to use Bottom line: Vite doesn't need to resolve externalized SSR dependencies, even for ESM. Not resolving externalized SSR dependencies will workBut, most importantly: why am I confident that Vite shouldn't resolve externalized SSR dependencies? Today, the vast majority of SSR users are using Node.js servers without a server-side bundler. This means that Node.js is the de-facto standard server-side environment. That's why if Vite stays out of the way and simply lets Node.js do its job, then things will just work. We can safely make Vite only touch user-code and not dig into |
It doesn't quite require it. CJS can do dynamic imports of ESM. It just can't do static imports of ESM
Just FYI, SvelteKit has adapters for different environments. The Node adapter runs its code through ESBuild after doing the Vite build. People have also build adapters for other platforms like Cloudflare Workers and Deno. Does Vue by default do only client-side rendering? I had the sense the SvelteKit might be one of the primary ways people use SSR, but I don't know whether that's true. I don't quite understand how whether a server-side bundler is being used would affect this decision though, so none of that contradicts the idea that we might not need to resolve external dependencies. |
SvelteKit projects all have |
If the user has This means that SveleKit users will be able to use The neat thing here is: Vite doesn't need to resolve externalized SSR dependencies. Vite just looks at the user's To sum up:
I believe these three proposals will solve many (if not most...) SSR problems mentioned in #4230. Let me know if there are doubts/questions, I'm happy to elaborate further. |
Describe the bug
AFAICT, Vite should completely ignore externalize SSR dependencies, including:
Not read the dependency's code at all(edit: this seems to be the case ✅ )E.g. vuetifyjs/vuetify#13936; I don't see a reason why Vite should try to resolve the module
vuetify/lib/components
.Proposal: Vite should completely ignore externalized deps.
cc @GrygrFlzr
@aleclarson Do you know why Vite touches externalized deps?
@patak-js I'm thinking maybe we should cue in Evan about this at some point.
Reproduction
Vuetify repro: https://github.com/brillout/vuetify3-ssr
The text was updated successfully, but these errors were encountered: