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

如何修复Yarn Workspace中Rollup.Js的未解析依赖问题

解决Yarn Workspace中Rollup未解析工作区依赖的问题

我在Yarn Workspace环境下维护两个包:React TypeScript组件库@oneface-react/components和类型定义包@oneface-react/types。组件库依赖该types包,但使用Rollup构建时出现未解析依赖警告:

(!) Unresolved dependencies
@oneface-react/types (imported by "src/components/ComponentA/ComponentA.tsx")

由于该依赖是工作区内的包,不会存在于组件库的node_modules中,可通过以下步骤解决:

1. 修正tsconfig的paths配置

当前组件库tsconfig中的paths指向错误,需要改为指向工作区中types包的实际源码路径:

{
  "compilerOptions": {
    // 保留原有其他配置
    "paths": {
      "@oneface-react/types": ["../types/src/index.ts"] // 根据types包的源码位置调整
    }
  }
}

2. 配置Rollup识别Yarn Workspace

修改@rollup/plugin-node-resolve的配置,开启工作区解析能力:

// Rollup配置中的resolve插件部分
resolve({
  preserveSymlinks: true,
  workspaces: true // 开启后Rollup会自动识别工作区内的包
})

3. 确保rollup-plugin-typescript2同步路径配置

在rollup-plugin-typescript2的配置中,明确指定路径映射,避免编译时路径解析失败:

typescript({
  tsconfig: "./tsconfig.json",
  clean: true,
  tsconfigOverride: {
    compilerOptions: {
      paths: {
        "@oneface-react/types": ["../types/src/index.ts"]
      }
    }
  }
})

4. 预构建types包(可选)

如果types包需要编译输出,先在项目根目录执行构建命令,确保组件库能访问到已编译的类型文件:

yarn workspace @oneface-react/types build

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 17:57:02