【问题标题】:Extend a "paths" tsconfig file for a monorepo为 monorepo 扩展“路径”tsconfig 文件
【发布时间】: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


【解决方案1】:

如果我理解正确,它是关于create-react-app。如果是这样,那么您需要在 repo 根目录中有多个文件夹,例如 src。这需要配置 webpack 以避免弹出使用 rewire 并为多个顶级目录使用 alias

// config-overrides.js:

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

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

【讨论】:

  • 与反应无关,但希望这对某人有所帮助。
猜你喜欢
  • 2020-10-28
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多