`ssrLoadModule` does not load nested dependency
See original GitHub issueDescribe the bug
Original issue discovered: https://github.com/vitejs/vite/discussions/3464#discussioncomment-807918
I have a vite plugin that make use of the ssr
argument in resolveId hook. The main purpose of the plugin is to replace @shopify/polyfills/fetch
with @shopify/polyfills/fetch.node
when its on the server and @shopify/polyfills/fetch.browser
when it is on the client.
This plugins work on the client side, but break when ssrLoadModule
is use to load the server entry point.
Some investigations
I have tired to replace @shopify/polyfills/fetch
with a random virtual module (ie. virtual:fetch-polyfill
) and that work.
Reproduction
Use the branch https://github.com/michenly/app-proving-ground/tree/vite-ssr-dep
Case 1: client side only with a index file
- Run
yarn dev
- See the application working with a list displayed in the browser.
Case 2: ssr using vite middleware and ssrLoadModule
- Run
yarn run ssr
- See the application fail with the following error
Case 3: same as 2, but using virtual module
- Change
@shopify/polyfills/fetch
tovirtual:fetch-polyfill
in both the plugin and the client import in GraphQL.tsx - Run
yarn run ssr
- See the following error which mean the module is broken. But it is loaded on server side.
System Info
Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers
:
System:
OS: macOS 11.3.1
CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
Memory: 338.92 MB / 16.00 GB
Shell: 4.4.23 - /usr/local/bin/bash
Binaries:
Node: 16.1.0 - /usr/local/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 7.11.2 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Browsers:
Chrome: 90.0.4430.212
Firefox: 72.0.2
Safari: 14.1
npmPackages:
vite: ^2.2.3 => 2.2.3
Used package manager: yarn
Before submitting the issue, please make sure you do the following
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Provide a description in this issue that describes the bug.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9 (5 by maintainers)
It depends. To support the
exports
field, Vite uses theresolve.exports
package, which has aconditions
option for deciding which keywords to look for. Note that Vite SSR does not use that package, but this will change with #3951.I think the correct solution here is to use
exports
to load between node and browser, oralias
as a workaround for the package. Closing as it seems to be working as intended.