Bundling component library for RSC (React Server Components) and Next.js App Router.

huangapple go评论67阅读模式
英文:

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:

  1. 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.

  2. 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:

  1. 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.

  2. 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”指令。

更新:虽然我没有足够的时间进行详细的绝对路径研究,但以下是一些可能有帮助的资源。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月14日 19:11:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定