-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
When bundling, conditional exports in package.json are selected incorrectly #3772
Comments
I think this is the cause: First, vite always tries to import the browser version of what's in the vite/packages/vite/src/node/plugins/resolve.ts Lines 640 to 643 in fd4146b
Once that's fixed, something else goes wrong: When trying to import vite/packages/vite/src/node/plugins/resolve.ts Lines 433 to 435 in fd4146b
However It never passes the SSR flag. After a deep stack of calls (e.g., vite/packages/vite/src/node/plugins/resolve.ts Lines 221 to 226 in fd4146b
And the browser version of I'd be happy to send a PR, but I'm not sure how to pass the SSR flag deeply without littering methods with arguments, so |
@hgl great digging. |
@patak-js Thanks for the help. From my testing, it seems And from the code, it got the initial value from here: vite/packages/vite/src/node/build.ts Lines 223 to 225 in 3bff06e
and later an additional SSR flag is passed alongside it when the logic enters SSR: vite/packages/vite/src/node/ssr/ssrModuleLoader.ts Lines 66 to 68 in b1e7395
Maybe I missed the code where |
vite/packages/vite/src/node/build.ts Line 229 in 3bff06e
But you are right, this flag means you are compiling the ssr build, not that you are running the server. Looks like there isn't a way to know if you are doing ssr from the config. Maybe we could use |
Hm I'm not sure; I'm not all too familiar with the source code but yes seems like vite/packages/vite/src/node/server/index.ts Lines 116 to 119 in ff7284f
to be what we are looking for. So I guess SSR <=> |
Correct me if I'm wrong, but And for the SSR server, I don't think we can use
Even if it is recommended, the user may chose to have a full server, or use Vite in So looks like we need to pass around the Another idea would be to have a WeakMap: config -> ssr, and set when entering |
And these methods need to accept the new argument, if I'm not missing anything:
Will the PR be accepted if it needs to touch this amount of methods? |
I think we should check it in the PR review with other maintainers, as it will be easier to discuss seeing the changeset. At least to me, this sounds ok. |
This issue gets locked because it has been closed for more than 14 days. |
Describe the bug
When importing a module that has conditional exports specified (e.g., "browser", "import", "require" or "node"), Vite may select the incorrect export, depending on the order of exports in the module's package.json file.
For js/ts files that are strictly used for endpoints (e.g., target of a fetch request), this is problematic. The specific case is when importing "jose" to sign JWTs. In its package.json, you can see the "exports" , of which many have "browser", "import" and "require" entries. Example:
Resolving these exports is handled by lukeed/resolve.exports. The order is respected, so in the above case, "browser" will always be selected. Since the endpoint code is run via "node", the selected module fails -- in this case, the error is "vite_ssr_import_1.default.btoa is not a function".
This might be related to #3736, except that it involves Webpack, and that the culprit (okta-auth-js) doesn't have conditional exports in its package.json.
Reproduction
Here is the repo
After cloning, do an "npm i" and a "npm run dev"
This will capture the vite log into vite-debug.txt in the root directory
You'll see the error at the bottom, and if you do a search for "jose", you'll see that the "browser" version was imported.
System Info
Before submitting the issue, please make sure you do the following
The text was updated successfully, but these errors were encountered: