英文:
Bundling component library for RSC (React Server Components) and Next.js App Router
问题
I work on a library of React components that will be used across various Next.js projects. I want components in this library to be written in RSC way, meaning the base would be server ones and only some nodes would have the "use client" directive on top of them, to provide interactivity.
Currently, I have problems with bundling such library in the way I would like.
I use Vite.js for making bundles, and since Vite is built on top of Rollup, there is a Rollup solution (https://github.com/stropho/rollup-plugin-banner2) to add the "use client" directive on top of the whole bundle, marking all the components there as "client" ones.
This indeed makes the components usable, but on the other hand abandons all the advantages of React Server Components.
This "all or nothing" solution seems not good for me.
What I have tried so far:
-
distribute the library source code rather than its bundle and use
transpilePackages: ['ui-library'],
in next.config.js.
This is not working due to different typescript absolute path settings in my next.js app and the UI library package. It does not seem like a solution. -
use Rollup plugin https://github.com/Ephem/rollup-plugin-preserve-directives and
preserveModules: true,
. While this works with pure Rollup, if I bundle the components using Vite.js the "use client" directives are still somehow removed out of their bundle files (no minification used).
It would be great to have the ability to bundle RSC library in Vite, without the need to dig into Rollup config.
What do you think would be the preferred solution? Is there some alternative in other bundlers like Webpack, Parcel, or others?
Some sources I have read about the subject:
英文:
I work on a library of React components that will be used across various Next.js projects. I want components in this library to be written in RSC way, meaning the base would be server ones and only some nodes would have the "use client" directive on top of them, to provide interactivity.
Currently, I have problems with bundling such library in the way I would like.
I use Vite.js for making bundles, and since Vite is build on top of Rollup, there is Rollup solution (https://github.com/stropho/rollup-plugin-banner2) to add "use client" directive on top of the whole bundle, marking all the components there as "client" ones.
This indeed makes the components usable, but on the other hand abandons all the advantages of React Server Components.
This "all or nothing" solution seems not good for me.
What I have tried so far:
-
distribute the library source code rather than its bundle and use
transpilePackages: ['ui-library'],
in next.config.js.
This is not working due to different typescript absolute path settings in my next.js app and the UI library package. It does not seem like a solution. -
use Rollup plugin https://github.com/Ephem/rollup-plugin-preserve-directives and
preserveModules: true,
. While this works with pure Rollup, if I bundle the components using Vite.js the "use client" directives are still somehow removed out of their bundle files (no minification used).
It would be great to have the ability to bundle RSC library in Vite, without need to dig into Rollup config.
What do you think would be the preferred solution ? Is there some alternative in other bundlers like Webpack, Parcel or others ?
Some sources I have read about the subject:
答案1
得分: 3
你可以使用Next.js与TurboRepo创建你的库。查看我之前发布的这个库 - https://github.com/mayank1513/sticky-section-header。通过这种方法,你可以根据需要在每个组件上定义“use client”指令。
更新:虽然我没有足够的时间进行详细的绝对路径研究,但以下是一些可能有帮助的资源。
- https://github.com/vercel/turbo/discussions/620#discussioncomment-2136195 这个讨论涉及到单仓库中的绝对路径。
- https://stackoverflow.com/a/59768302/9640177 这个讨论了如何定义路径以使其正常工作。
英文:
You can use Next.js with TurboRepo to create your library. Checkout this library that I published sometime back - https://github.com/mayank1513/sticky-section-header. With this approach, you can define the 'use client' directive as required on a per component basis.
Update: Though I do not have enough time to do much research for the absolute paths, following are some resources that may be helpful.
- https://github.com/vercel/turbo/discussions/620#discussioncomment-2136195 This discussion talks about absolute paths in monorepo
- https://stackoverflow.com/a/59768302/9640177 This talks about how to define path so that it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论