首页 > 解决方案 > TypeScript:同时监视和编译多个目录(monorepo)

问题描述

我一直在网上搜索解决方案,但没有发现任何有价值的东西。基本上,我正在寻找一种工具或某种方式来同时在多个目录中启动 TypeScript 文件监视/编译。

我有一个带有范围 NPM 包(@test/one、@test/two、@test/three 等)的 monorepo,我想同时观看/编译所有这些包。

看起来 TS 不支持这一点,也没有任何工具可以做到这一点。我发现的最接近的东西是 nodemon,我可以让它一次监视多个目录,但它只支持在单个位置执行脚本/二进制文件,而我需要它在每个监视目录中执行更改时执行“tsc”。

我可以用 nodemon 做的最好的事情是以下,但这当然不是一个很好的方法,因为它编译所有包,即使只有一个更改:

npx nodemon --watch test-shared-api --watch test-shared-redux --watch test-shared-types -e js,ts,jsx,tsx --exec "npx tsc --build test-shared-api/tsconfig.json && npx tsc --build test-shared-types/tsconfig.json && npx tsc -build test-shared-redux/tsconfig.json"

同时我可以在 package.json 中执行以下操作,这更好,但仍然可以做得更好:

  "scripts": {
"ts-watch-shared-types": "tsc -p packages/@test-shared/test-shared-types/tsconfig.json -w",
"ts-watch-shared-api": "tsc -p packages/@test-shared/test-shared-api/tsconfig.json -w",
"ts-watch-shared-redux": "tsc -p packages/@test-shared/test-shared-redux/tsconfig.json -w",
"ts-shared-watch": "concurrently -k -n w: npm:ts-watch-shared-*"
  },

有任何想法吗?

标签: typescriptnpmmonorepo

解决方案


您可以尝试将TypeScript 项目引用与 一起使用tsc -b --watch,尽管该功能非常新并且存在一些错误(截至 2018 年 9 月)。


推荐阅读