-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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 doesn't support ESM packages with "exports" field in SSR #3953
Comments
The error message seems to indicate that vite/packages/vite/src/node/ssr/ssrExternal.ts Lines 11 to 12 in 887c247
And from the docs:
|
Core SvelteKit functionality cannot be used because of this issue: sveltejs/kit#2237. The |
Snowpack resolved this issue by abstracting to use import() by default: https://github.com/snowpackjs/snowpack/blob/d8cc54a7816faef9d98459a1e24f54912163868b/snowpack/assets/require-or-import.js |
This is now causing problems for SvelteKit in a second way. Svelte exports different code on the client and server, but we always get the client-side version because |
maybe this module could be of use for a implementation https://github.com/unjs/mlly |
https://www.npmjs.com/package/enhanced-resolve might be another alternative. It's used by webpack, so should be fairly battle-tested |
Here's a branch with It's a pretty small change and seems to basically work. Some of the tests are failing though. I don't know that I'll have time to look into it, but thought I'd share in case it helps anyone who might want to give it a try Edit: I didn't see, but it looks like there was also a proposal to make this change above. That version might work better, but I haven't tested yet: https://github.com/raythurnevoid/vite/commit/95a4f859fa31c4f052f2c3ab422b8f6e82154269 |
I just tested @raythurnevoid's example from the issue description and it works now with Vite 2.7 except for one issue. The code looks like:
To get it to work I had to change it to:
I think that's working as intended though and is a bug in @raythurnevoid's example: https://stackoverflow.com/a/64242299 |
This still doesn't work with https://github.com/Karlinator/reproduction-sveltekit-hooks when |
It looks like @raythurnevoid and I had figured out different pieces of this. I combined our two solutions and sent a PR for it: #5593 |
This would also be fixed by #3951 |
@aleclarson Can we close this one? 🤔 |
Describe the bug
Because of this browserify/resolve#222 issue, Vite won't parse package.json "exports" field and will throw this error for packages that doesn't use "main" field:
I tried to replace
resolve
packages with webpack'senhanced-resolve
: https://www.npmjs.com/package/enhanced-resolveTheir base code is extremely more hure and complicated than
resolve
but i've managed to make it work well in Vite: https://github.com/raythurnevoid/vite/commit/95a4f859fa31c4f052f2c3ab422b8f6e82154269Another upside of
enhanced-resolve
is that it already supports also "imports" field, that could be a good replace for the numerous "alias" fields of tools like Jest, Rollup, Vite itself and tsconfig paths:https://nodejs.org/api/packages.html#packages_imports
https://nodejs.org/api/packages.html#packages_subpath_imports
if this fix is feasible i'll open a PR with it.
Reproduction
To reproduce it's enough to import an ESM only package eg: https://github.com/raythurnevoid/strings-filter
Repo example: https://github.com/raythurnevoid/vite-esm-error-reproduction/tree/main
System Info
Output of
npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers
:Used package manager: npm
Logs
shows logs
Before submitting the issue, please make sure you do the following
The text was updated successfully, but these errors were encountered: