如何使用 tsc 使用路径编译器选项编译特定文件 [英] How to compile a specific file with tsc using the paths compiler option

查看:111
本文介绍了如何使用 tsc 使用路径编译器选项编译特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个文件要使用脚本编译.这些文件依赖 paths 编译选项来解析其模块.因为我想专门针对这些文件,所以我必须将它们提供给 tsc(因为我不想为此创建一个单独的 tsconfig.json 来针对这些文件任务)

I have several files I want to compile using a script. These files rely on the paths compile option to resolve its modules. As I want to target these files specifically I am bound to supplying them to tsc (as I don't want to create a separate tsconfig.json that targets these files for this task)

我查看了将 --path 参数传递给 tsc 的选项,但这是不允许的(错误 TS6064: Option 'paths'只能在 'tsconfig.json' 文件中指定.)

I've looked at the option to pass the --path parameter to tsc, but this is not allowed (error TS6064: Option 'paths' can only be specified in 'tsconfig.json' file.)

我可以在使用 paths 选项时以某种方式只编译特定的 .ts 文件吗?

Can I somehow only compile specific .ts files while using the paths option?

更新 (22-06-17)

根据要求提供一些具体示例:

As per request some specific examples:

tsconfig.json文件中的相关设置如下:

The relevant settings in the tsconfig.json file are the following:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "Components/*": [
        "./src/Components/*"
      ]
    }
  }
}

所以相关部分是路径设置,您可以在其中创建速记以从某个目录导入文件.所以你可以导入一个文件,比如 import {header} from 'Components/header.ts' 而不是 import {header} from '../../Components/header.ts'

so the relevant part is the paths setting, where you can create shorthands to import files from a certain directory. so you can import a file like import {header} from 'Components/header.ts' in stead of import {header} from '../../Components/header.ts'

但我需要从命令行编译特定文件.但如果我尝试:

But I need to compile specific files from the command line. But if I try:

tsc --project tsconfig.json entryFile.ts

它会给我错误:

error TS5042: Option 'project' cannot be mixed with source files on a command line.

如果我尝试为 cli 提供 paths 选项,我会收到以下错误:

and if I try to provide the paths option to the cli I get the following error:

error TS6064: Option 'paths' can only be specified in 'tsconfig.json' file.

推荐答案

在命令行上指定输入文件会使 tsconfig.json 无效.

Specifying the input files on the command line voids your tsconfig.json.

在命令行中指定输入文件时,tsconfig.json 文件将被忽略.

When input files are specified on the command line, tsconfig.json files are ignored.

来自 TypeScript 文档.

因此,除了使用具有正确包含"(或排除)文件规范的 tsconfig.json 之外,别无选择.好消息是,在相同的文档中,您会发现:

So, there's no other option than to use a tsconfig.json with the proper 'include' (or exclude) specification of the files. The good news is that in the same docs you will find:

tsconfig.json 文件可以使用 extends 属性从另一个文件继承配置.

A tsconfig.json file can inherit configurations from another file using the extends property.

所以你可以做的是用这样的东西覆盖你的 tsconfig 的包含属性:

So what you can do is overwrite the include property of your tsconfig with something like this:

{
  "extends": "./tsconfig.json",
  "include": [
      "src/root/index.ts"
  ]
}

所以你可以制作一个脚本:

So you could make a script that:

  1. 使用所需的include"属性创建一个临时的extends tsconfig"文件
  2. 使用指定为临时 tsconfig 的 --project 调用 tsc

有人可能会争辩说这非常复杂,并质疑他们为什么决定不正确支持这种情况.然而,问题仍然是为什么人们只想编译一个项目的一个文件.在开发过程中,您可以使用监视 (-w) 选项来编译更改文件,并且在构建完整项目时,您可能希望准确地编译完整项目.

One could argue that this is very complicated and question why they made the decision to not support this scenario properly. However, the question remains why one would like to compile just one file of a project. While developing, you can use the watch (-w) option to compile changes files, and upon building a full project you'll probably want to compile exactly that, the full project.

这篇关于如何使用 tsc 使用路径编译器选项编译特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆