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

如何在 monorepo 库中通过 ESLint 禁止 index.ts 内部导入?

禁止库内部导入index.ts(文件夹导入)

项目结构

project
|-- applications
|   |-- app1
|   |-- app2
|
|-- libraries
    |-- lib1
    |   |-- src
    |   |   |-- lib
    |   |   |   |-- dom-utils.ts
    |   |   |   |-- index.ts // exporting dom-utils
    |   |   |-- components
    |   |   |   |-- Foo.ts
    |   |   |   |-- index.ts // exporting Foo
    |   |   |-- index.ts // exporting * from components and lib
    |   |-- index.ts // export * from src
    |-- lib2

现有导入规则

  • 应用层(app1、app2)支持通过包名导入库模块,例如:
import { Foo } from 'lib1';

该方式运行正常。

  • 库内部模块(如lib1/src/components/Foo.ts)必须直接导入同库内的具体源文件路径,不能通过index.ts(即文件夹导入),否则极易引发循环依赖(比如Foo导入上级index,dom-utils也做同样操作时就会出现循环)。

需求

需要找到方法完全禁止库内部的所有index.ts导入,哪怕是不会产生循环依赖的情况也不允许。

已尝试方案

试过eslint-plugin-import的import/no-cycle规则,但未达到预期效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 14:52:40