typescript 如何使用 tsc 使用路径编译器选项编译特定文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44676944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile a specific file with tsc using the paths compiler option
提问by Jasper Schulte
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)
我有几个要使用脚本编译的文件。这些文件依赖于paths
编译选项来解析其模块。因为我想专门针对这些文件,所以我必须将它们提供给tsc
(因为我不想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.
)
我查看了将--path
参数传递给 的选项tsc
,但这是不允许的 ( error TS6064: Option 'paths' can only be specified in 'tsconfig.json' file.
)
Can I somehow only compile specific .ts
files while using the paths
option?
我可以.ts
在使用该paths
选项时以某种方式只编译特定文件吗?
Update (22-06-17)
更新 (22-06-17)
As per request some specific examples:
根据要求,一些具体的例子:
The relevant settings in the tsconfig.json
file are the following:
tsconfig.json
文件中的相关设置如下:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"Components/*": [
"./src/Components/*"
]
}
}
}
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'
所以相关部分是路径设置,您可以在其中创建速记以从某个目录导入文件。所以你可以导入一个文件import {header} from 'Components/header.ts'
,而不是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
it will give me the error:
它会给我错误:
error TS5042: Option 'project' cannot be mixed with source files on a command line.
and if I try to provide the paths
option to the cli I get the following error:
如果我尝试为paths
cli提供选项,我会收到以下错误:
error TS6064: Option 'paths' can only be specified in 'tsconfig.json' file.
回答by Bram
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.
在命令行上指定输入文件时,将忽略 tsconfig.json 文件。
From the TypeScript docs.
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 之外别无选择。好消息是,在相同的文档中,您会发现:
A tsconfig.json file can inherit configurations from another file using the extends property.
tsconfig.json 文件可以使用 extends 属性从另一个文件继承配置。
So what you cando is overwrite the include property of your tsconfig with something like this:
因此,您可以做的是使用以下内容覆盖 tsconfig 的 include 属性:
{
"extends": "./tsconfig.json",
"include": [
"src/root/index.ts"
]
}
So you could make a script that:
所以你可以制作一个脚本:
- Creates a temporary 'extends tsconfig' file with the desired 'include' property
- Calls
tsc
with--project
specified as that temporary tsconfig
- 使用所需的“包含”属性创建一个临时的“扩展 tsconfig”文件
- 调用
tsc
与--project
指定为临时tsconfig
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 whyone 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.
有人可能会争辩说这非常复杂,并质疑他们为什么决定不正确支持这种情况。然而,问题仍然是为什么人们只想编译一个项目的一个文件。在开发时,您可以使用 watch ( -w
) 选项来编译更改文件,并且在构建完整项目时,您可能希望准确地编译整个项目。
回答by prograhammer
Just to build upon @Bram's answer...
只是为了建立在@Bram 的回答之上......
What I do in my project is have two tsconfigs:
我在我的项目中做的是有两个 tsconfigs:
project/
dist/
docs/
src/
package.json
tsconfig.json
tsconfig-build.json
So I have a tsconfig-build.json
(because I don't want docs/
included in my build):
所以我有一个tsconfig-build.json
(因为我不想docs/
包含在我的构建中):
// tsconfig-build.json
{
"extends": "./tsconfig.json",
"include": [
"src/**/*"
]
}
Then you could have a line in package.json
:
然后你可以有一行package.json
:
// package.json
...
"scripts": {
...
"build": "tsc --p tsconfig-build.json"
},
回答by Sampo
I was in a similar need for making sure the project will compile using lint-staged. If the combination of tsconfig.json
and file paths were allowed, you could use "tsc"
directly as a lint-staged command.
我也有类似的需求,需要确保项目将使用lint-staged 进行编译。如果tsconfig.json
允许 和 文件路径的组合,则可以"tsc"
直接用作 lint-staged 命令。
As a workaround I created the following helper script scripts/tsc-lint.sh
:
作为一种解决方法,我创建了以下帮助脚本scripts/tsc-lint.sh
:
#!/bin/bash -e
TMP=.tsconfig-lint.json
cat >$TMP <<EOF
{
"extends": "./tsconfig.json",
"include": [
EOF
for file in "$@"; do
echo " \"$file\"," >> $TMP
done
cat >>$TMP <<EOF
"unused"
]
}
EOF
tsc --project $TMP --skipLibCheck --noEmit
Then the lint-staged config contains:
然后 lint-staged 配置包含:
"lint-staged": {
"{src,tests}/**/*.ts": [
"scripts/tsc-lint.sh"
]
},
The file .tsconfig-lint.json
is added to .gitignore
.
该文件.tsconfig-lint.json
被添加到.gitignore
.
回答by basarat
Can I somehow only compile specific .ts files while using the paths option
我可以在使用路径选项时以某种方式只编译特定的 .ts 文件吗
You don't need paths for that. Simply pass in the file e.g.
你不需要路径。只需传入文件,例如
tsc src/foo.ts