-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Preserve 'use client' directives #5161
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 5c925aa:
|
Codecov ReportPatch coverage:
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more Additional details and impacted files@@ Coverage Diff @@
## main #5161 +/- ##
==========================================
+ Coverage 91.90% 92.22% +0.31%
==========================================
Files 111 111
Lines 4188 4229 +41
Branches 1083 1099 +16
==========================================
+ Hits 3849 3900 +51
+ Misses 318 308 -10
Partials 21 21
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report in Codecov by Sentry. |
I've checked the output of the codesandbox preview build:
This looks like what we want 👍 . Shipping it 🚢 |
I'm seeing a lot of warnings like this:
Occurs when bundling client output with Vite. Not sure what I need to change in my setup, to support the "use client" directive? |
I'm seeing the same warnings in rollup:
it does make sense because when we bundle together into one file, we shouldn't keep |
Won't that always be the case when consuming |
I don't know. I guess |
Great - Not just me that's confused about this new Is this the right solution for the the react-query library? If the Next.js 13 app folder is still beta, should the ecosystem figure out the right way of handling |
@thebuilder It is confusing, and sorry for adding a bunch of warnings to your build! I suspect you are not using Server Components at the moment, so you can safely just ignore these warnings. If you want to hide the annoying output, you can add a Rollup onwarn filter to hide them. I get why Rollup warns for this, but now that directives is becoming more common, it's also unfortunate. The reason you are seeing this is because you are using the unbundled Thing is though, Server Components can use this This is all based on a specification any bundler can implement too, so it's not Next.js specific even if they were first to implement it. It's definitely something that is up to us as an ecosystem to figure out, and different libraries might have different tradeoffs. |
Oh, and just to answer your question specifically to avoid confusion:
Since you are not using Server Components, you don't need to "support" the directive! Your bundler is warning because it doesn't know what to do with this. It's at the top of a file it wants to include in a bundle, and it can't safely hoist that directive to the entire bundle, so it's removing it instead, which is fine since you don't need it. It doesn't want to just remove things without warning though, which is why you are seeing this. |
Thanks for the explanation. Since RSC is still only supported in a few select frameworks, like Next, it obviously is not something we can use with Vite or other frameworks. I feel like this is something that Vite/Rollup should handle out of the box. My build output is going to be filled with warnings, especially if more libraries start adding 'use client'. Until that happens, it seems a bit premature to start adding it to library code? |
I think about it the other way around though. Unless libraries start using this, what's the incentive to change/fix the behaviour? 😃 Again, sorry about the noisy warnings, adding an |
Maybe it should be added directly to the https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react package instead? |
Absolutely! I only suggest adding the |
same as |
Here is the import { defineConfig } from "vite";
// https://vitejs.dev/config/
export default defineConfig({
build: {
rollupOptions: {
/**
* Ignore "use client" waning since we are not using SSR
* @see {@link https://github.com/TanStack/query/pull/5161#issuecomment-1477389761 Preserve 'use client' directives TanStack/query#5161}
*/
onwarn(warning, warn) {
if (
warning.code === "MODULE_LEVEL_DIRECTIVE" &&
warning.message.includes(`"use client"`)
) {
return;
}
warn(warning);
},
},
},
}); |
Fixes #4933
rollup-plugin-preserve-directives
and adds it to all un-bundled builds to preserve 'use client' directives'use client'
to all files that should have it (I think)This makes everything work as documented with Next app router, that is, developers should no longer need to:
"use client"
themselves (just import and useHydrate
directly for example)@tanstack/query-core
in RSCs (can now import directly from@tanstack/react-query
)I tested it like this (basically quick version of the docs):