为monorepo扩展“路径” tsconfig文件

时间:2019-12-09 20:48:26

标签: typescript tsconfig monorepo

我有一个像这样的文件夹结构:

inputEl?.current?.focus(); // skips the call when inputEl or inputEl.current is null or undefined
- 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); ,以便每个包都为其他包正确键入模块导入。


尝试失败1

tsconfig.paths.json

问题:找不到模块“工具”。添加到路径的baseUrl指向// mono-repo/Website/tsconfig.json { "extends": "../tsconfig.paths.json", "compilerOptions": { "baseUrl": "./src", } } 。这不是真实的道路。


尝试失败2

mono-repo/Website/src/Tool/src

问题:无法从“测试”中导入测试。 baseUrl不是项目src。除了相对路径外,任何其他内容都将无法导出。


功能正常但尝试失败3

// mono-repo/Website/tsconfig.json
{
  "extends": "../tsconfig.paths.json",
  "compilerOptions": {
    "baseUrl": "../",
  }
}
// mono-repo/tsconfig.paths.json
{
  "compilerOptions": {
    "paths": {
      "tool": ["../../Tool/src"],
    }
  }
}

问题:有效,但假设扩展tsconfig.paths.json的每个tsconfig的baseUrl始终位于// mono-repo/Website/tsconfig.json { "extends": "../tsconfig.paths.json", "compilerOptions": { "baseUrl": "./src", } } 下的两个目录中。目前,这对于我的项目是正确的,但是我很犹豫将其作为标准。


如何为Monorepo设置可扩展的“路径” tsconfig json?

1 个答案:

答案 0 :(得分:0)

如果我正确理解,那是关于create-react-app的。如果是这样,则在回购根目录中需要多个文件夹,例如src。这需要配置webpack,以便避免弹出使用重新连接并将alias用于多个顶级目录:

// config-overrides.js:

const {alias, configPaths} = require('react-app-rewire-alias')

module.exports = function override(config) {
  alias(configPaths())(config)
  return config
}