You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Turbo monorepo中打包UI库?配置遇阻求解决方案

Turbo Monorepo 中UI库打包配置问题

花了一周时间折腾这个问题,完全没进展。用create-turbo@latest初始化了Turbo monorepo项目,默认的ui/package可以正常工作,但不想像下面这样在package.json里手动逐个导出组件:

{
  "name": "@repo/ui",
  "version": "0.0.0",
  "private": true,
  "exports": {
    "./button": "./src/button.tsx",
    "./card": "./src/card.tsx",
    "./code": "./src/code.tsx"
  },
  "scripts": {
    "lint": "eslint . --max-warnings 0",
    "generate:component": "turbo gen react-component"
  },
  "devDependencies": {
    "@repo/eslint-config": "workspace:*",
    "@repo/typescript-config": "workspace:*",
    "@turbo/gen": "^1.12.4",
    "@types/node": "^20.11.24",
    "@types/eslint": "^8.56.5",
    "@types/react": "^18.2.61",
    "@types/react-dom": "^18.2.19",
    "eslint": "^8.57.0",
    "react": "^18.2.0",
    "typescript": "^5.3.3"
  }
}

参考Dub.co项目的配置,尝试用TSUP编译UI库,完全照搬了它的TSUP和tsconfig配置,但Web应用一直报UI库未找到的错误(虽然组件能正常使用),而且部分UI组件出现异常——这些组件在其他项目用了好几年都没问题,只有在这个monorepo里失效。

后来又尝试修改ui库的package.json,把入口指向统一导出的src/index.tsx,文件内容如下:

import "./globals.css"

export * from "./tw-indicator"

// Shad components
export * from "./ui"

// Folders
export * from "./forms"
export * from "./layouts"
export * from "./typography"
export * from "./icons"

结果触发了这个错误:

TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_0__.createContext) is not a function
This error happened while generating the page. Any console logs will be displayed in the terminal window.

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
eval
..\..\node_modules\.pnpm\@radix-ui+react-direction@1.0.1_@types+react@18.2.75_react@18.2.0\node_modules\@radix-ui\react-direction\dist\index.mjs (4:82)
(rsc)/../../node_modules/.pnpm/@radix-ui+react-direction@1.0.1_@types+react@18.2.75_react@18.2.0/node_modules/@radix-ui/react-direction/dist/index.mjs
file:/C:/Code/Test%20Project/testproject/apps/web/.next/server/vendor-chunks/@radix-ui+react-direction@1.0.1_@types+react@18.2.75_react@18.2.0.js (30:1)

不管怎么调整ui库的package.json、tsconfig或者TSUP配置,都会出现一堆错误。试了好几种打包方式都失败了,有没有人成功在Turbo monorepo里实现无报错的UI库打包?

内容的提问来源于stack exchange,提问作者cording12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 22:07:37