apollo-client-nextjs
apollo-client-nextjs copied to clipboard
Error while building using vite
HI! I'm creating a library to wrap some precofigured hooks/funcs for my application. Building it using vite I have this error while trying to import registerApolloClient:
RollupError: src/client/ssr.ts (2:9): "registerApolloClient" is not exported by "node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]/node_modules/@apollo/experimental-nextjs-app-support/dist/empty.js", imported by "src/client/ssr.ts".
Do you have any idea how to solve it? (I get that the error is related to the fact that the library exposes exports using reasct-server
)
I get that the error is related to the fact that the library exposes exports using react-server
Yup, that's the problem. These functions do, in fact, not exist in the node
or browser
environments, which vite
seems to assume here.
My first idea would be to tell vite in some way that @apollo+experimental-nextjs-app-support
is external and should not be packaged. That might already keep it from looking at it and complaining about it.
If that's not working, you might have to compile the file in question with a react-server
condition. If you have code that relies on node
or browser
features, that means that you'll have to split it up and compile it twice.
I believe the way of doing that would be a vite config like this:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
resolve: {
conditions: ["react-server"],
},
});