简体   繁体   English

为 monorepo 扩展“路径”tsconfig 文件

[英]Extend a "paths" tsconfig file for a monorepo

I've got a folder structure like so:我有一个像这样的文件夹结构:

- 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);

I'd like to be able to extend tsconfig.paths.json so that every package has correctly typed module imports for the others.我希望能够扩展tsconfig.paths.json ,以便每个 package 都为其他人正确键入模块导入。


Failed Attempt 1失败的尝试 1

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

Issue: can not find module "tool".问题:找不到模块“工具”。 The baseUrl added to the path leads to mono-repo/Website/src/Tool/src .添加到路径的 baseUrl 指向mono-repo/Website/src/Tool/src This is not a real path.这不是真正的路径。


Failed Attempt 2失败的尝试 2

// mono-repo/Website/tsconfig.json
{
  "extends": "../tsconfig.paths.json",
  "compilerOptions": {
    "baseUrl": "../",
  }
}

Issue: can not import test from "test".问题:无法从“测试”导入测试。 The baseUrl is not the project src. baseUrl 不是项目 src。 Anything but relative paths will be unexportable.除了相对路径之外的任何内容都将无法导出。


Functional but Ugly Attempt 3功能性但丑陋的尝试 3

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

Issue: works, but makes the assumption that the baseUrl of every tsconfig which extends tsconfig.paths.json will always be two directories below mono-repo .问题:有效,但假设每个扩展 tsconfig.paths.json 的 tsconfig 的 baseUrl 将始终是mono-repo下面的两个目录。 This is currently true for my project, but I'm hesitant to make this a standard.这目前对我的项目来说是正确的,但我犹豫是否要将其作为标准。


How does one set up an extendable "paths" tsconfig json for a monorepo?如何为 monorepo 设置可扩展的“路径”tsconfig json?

Something to know is that tsconfig extends is an override and not a merge .需要知道的是 tsconfig extends 是一个overridenot a merge With that information, you can understand that what you are trying to achieve by extending the base tsconfig can't work as you expecting it to do.有了这些信息,您就可以理解您试图通过扩展基本 tsconfig 来实现的目标无法像您期望的那样工作。

Also, using the paths options should always work as a pair with the baseUrl in order to solve the resolution problem.此外,使用paths选项应该始终与baseUrl一起工作,以解决解析问题。

N.netheless, here is a solution that you can use to solve that problem. N.netheless,这里有一个解决方案可以用来解决这个问题。

create a tsconfig.json at the root level of your project alongs the line of:在项目的根级别沿着以下行创建一个 tsconfig.json:

"paths": {
  "~/*": ["../../../mono-repo/*/src"]
}

Which means in each sub packages, you can extends that tsconfig file and then specify your src directory as a baseUrl .这意味着在每个子包中,您可以扩展该tsconfig文件,然后将您的src目录指定为baseUrl Then you will be able to import as follow:然后您将能够按以下方式导入:

import { tool } from "~/Tool";

Hope that it is clear, let me know if something is missing =)希望它是清楚的,让我知道是否缺少某些东西 =)

If I understand correctly it is about create-react-app .如果我理解正确,它是关于create-react-app If so then you need multiple folder like src in the repo root directory.如果是这样,那么您需要在 repo 根目录中使用多个文件夹,例如src This require configure webpack so to avoid eject use rewire and use alias for multiple top level directory:这需要配置 webpack 以避免弹出使用 rewire 并为多个顶级目录使用别名

// config-overrides.js:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM