简体   繁体   English

如何将 TypeScript tsconfig 放在另一个位置

[英]How to put TypeScript tsconfig in another location

I am trying to create a repo that houses our company's common linting rules in our locally hosted github instance.我正在尝试创建一个存储库,该存储库在我们本地托管的 github 实例中包含我们公司的通用 linting 规则。 This way we all have a common set of rules we can share easily.这样,我们就有了一套可以轻松共享的通用规则。 There are also configs for our typescript definitions, and PrettierJs rules.我们的 typescript 定义和 PrettierJs 规则也有配置。

The file structure looks like:文件结构如下:

/lint-common
  /config
    .eslintrc.js
    .pretterrc
    tsconfig.json
  package.json 

When installed in another project it looks like this:当安装在另一个项目中时,它看起来像这样:

/node_modules
  /lint-common
    /config
      .eslintrc.js
      .pretterrc
      tsconfig.json
    package.json 
package.json <-- Consuming package for the project that lint-common has been installed into

From my consuming project that has lint-common installed, I have scripts in the package.json that looks like this:从我安装了lint-common的消费项目中,我在 package.json 中有如下脚本:

"scripts": {
  "lint": "eslint -c /node_modules/lint-common/config/.eslintrc.js --ext .js src",
  "pretty": "prettier --write --config /node_modules/lint-common/config/.prettierrc src/**/*.js"
}

This works fine so far.到目前为止,这工作正常。 But when I try to do this for TypeScript , it behaves quite differently.但是当我尝试为TypeScript执行此操作时,它的行为完全不同。 Where lint and pretty process from the src directory I specify, the tsconfig insists on processing files from its own location.在我指定的src目录中进行lintpretty处理的地方,tsconfig 坚持从自己的位置处理文件。

For TypeScript, I tried to add a script like this:对于 TypeScript,我尝试添加如下脚本:

"tsc": "tsc --p ../../config/tsconfig.json",

The --p is the --project option - which allows me to stipulate where the tsconfig file is. --p--project选项——它允许我规定 tsconfig 文件的位置。 Inside the tsconfig I have:在 tsconfig 里面我有:

"include": ["src/**/*]

However, I only get the following error:但是,我只收到以下错误:

No inputs were found in config file ...

I can fix this by changing it to:我可以通过将其更改为来解决此问题:

"include": ["../../../src/**/*]

The include option inside the tsconfig is searching for the files relative to the location of the tsconfig - not from where I am running the npm command from. tsconfig 中的include选项正在搜索相对于 tsconfig 位置的文件——而不是从我运行npm命令的位置。

Is there a way I can get around this?有没有办法解决这个问题? I have tried using the rootDir option, but that doesn't seem to have any effect.我曾尝试使用rootDir选项,但这似乎没有任何效果。

Is there a way I can get around this?有没有办法解决这个问题?

The documentation doesn't mention a way, and AFAIK, there isn't.该文档没有提到一种方法,而AFAIK则没有。

I think a workable solution would be to include an npm [post-install] script which creates a tsconfig.json file, at the top level of a new project, which utilizes the extends property to reference the config nested within node_modules .我认为一个可行的解决方案是包含一个 npm [安装后] 脚本,该脚本在新项目的顶层创建一个tsconfig.json文件,该项目利用extends属性来引用嵌套在node_modules中的配置。

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

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