apollographql / apollo-client-nextjs

Apollo Client support for the Next.js App Router
https://www.npmjs.com/package/@apollo/experimental-nextjs-app-support
MIT License
358 stars 25 forks source link

Error while building using vite #278

Open andrea-benato-deltatre opened 1 month ago

andrea-benato-deltatre commented 1 month ago

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/@apollo+experimental-nextjs-app-support@0.10.0_@apollo+client@3.9.10_next@14.1.4_react@18.2.0/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 )

phryneas commented 1 month ago

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"],
  },
});