【发布时间】:2020-04-02 23:58:05
【问题描述】:
我有一个这样的文件夹结构:
- mono-repo
tsconfig.paths.json
- Website
tsconfig.json
- src
test.ts
index.ts
- Tool
- src
index.ts
// mono-repo/tsconfig.paths.json
{
"compilerOptions": {
"paths": {
"tool": ["Tool/src"],
}
}
}
// mono-repo/Website/src/index.ts
import { test } from "test";
import { tool } from "tool";
test(tool);
我希望能够扩展 tsconfig.paths.json,以便每个包都有正确类型的模块导入。
尝试 1 失败
// mono-repo/Website/tsconfig.json
{
"extends": "../tsconfig.paths.json",
"compilerOptions": {
"baseUrl": "./src",
}
}
问题: 找不到模块“工具”。添加到路径中的 baseUrl 指向mono-repo/Website/src/Tool/src。这不是真正的路径。
尝试 2 失败
// mono-repo/Website/tsconfig.json
{
"extends": "../tsconfig.paths.json",
"compilerOptions": {
"baseUrl": "../",
}
}
问题: 无法从“测试”导入测试。 baseUrl 不是项目 src。除了相对路径之外的任何内容都无法导出。
实用但丑陋的尝试 3
// mono-repo/tsconfig.paths.json
{
"compilerOptions": {
"paths": {
"tool": ["../../Tool/src"],
}
}
}
// mono-repo/Website/tsconfig.json
{
"extends": "../tsconfig.paths.json",
"compilerOptions": {
"baseUrl": "./src",
}
}
问题: 有效,但假设扩展 tsconfig.paths.json 的每个 tsconfig 的 baseUrl 将始终是 mono-repo 下的两个目录。目前我的项目确实如此,但我犹豫是否将其作为标准。
如何为 monorepo 设置可扩展的“路径”tsconfig json?
【问题讨论】:
-
我一直在使用功能性但丑陋的方式。所以它确实起作用了。
标签: typescript tsconfig monorepo