typescript 如何在 tsconfig.json 中排除以“.spec.ts”结尾的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48449107/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:11:41  来源:igfitidea点击:

How to exclude files ending in '.spec.ts' in tsconfig.json

javascriptjsonangulartypescript

提问by BBaysinger

I'm trying to include some files in my tsconfig.json, but it's including files I don't want to be included. (I see asterisks used in search syntax everywhere, but I'm not sure what this syntax is from, and where to learn more about it.) From a repo (with uncompiled source) I'm trying to include files that end in .ts, except for ones that end in .spec.ts.

我试图在我的 tsconfig.json 中包含一些文件,但它包含了我不想包含的文件。(我看到在搜索语法使用,随处可见的星号,但我不知道这句法是什么,在哪里可以更多地了解它。)从回购(有未编译源)我想包括文件结束.ts,除了以.spec.ts.

The following includes the files I want, but doesn't successfully exclude the files I don't want.

以下包括我想要的文件,但没有成功排除我不想要的文件。

  "include": ["node_modules/dashboard/**/*.ts"],
  "exclude": ["node_modules/dashboard/**/*.spec.ts"],

How do I exclude these files?

如何排除这些文件?

EDIT: At first was using **/**.tsand **/**.spec.ts. This was incorrect, but now that I've corrected it, I'm having the same issue.

编辑:起初是使用**/**.ts**/**.spec.ts。这是不正确的,但现在我已经更正了,我遇到了同样的问题。

回答by Fenton

The TypeScript handbook has a good write up on tsconfig file.

打字稿手册对tsconfig文件的好写了

The example in the handbook is:

手册中的例子是:

"exclude": [
    "node_modules",
    "**/*.spec.ts"
]

Note the use of **for any folder, and *(a single asterisk) for the file name wildcard.

请注意**对任何文件夹的使用,以及*(单个星号)用于文件名通配符。

You don't usually need to be any more specific, as I imagine you would want to exclude "all spec files", not just files in a particular sub-directory.

您通常不需要更具体,因为我想您会希望排除“所有规范文件”,而不仅仅是特定子目录中的文件。

When This Won't Work

当这行不通时

There are cases when this won't work.

有些情况下这是行不通的。

  1. If you have also include the file in both include and exclude sections - include wins
  2. If you are importing the excluded file into an included file.
  3. If you are using an old version of the compiler (early versions of tsconfig didn't allow the wildcards)
  4. You are compiling using tsc app.ts(or pass other arguments) - your tsconfig is ignored when you do this
  1. 如果您还在包含和排除部分中包含该文件 - 包含获胜
  2. 如果您将排除的文件导入到包含的文件中。
  3. 如果您使用的是旧版本的编译器(早期版本的 tsconfig 不允许使用通配符)
  4. 您正在使用tsc app.ts(或传递其他参数)进行编译- 执行此操作时会忽略您的 tsconfig

回答by Nimeshka Srimal

You can use

您可以使用

"exclude": ["node_modules/dashboard/**/*.spec.ts"]

"exclude": ["node_modules/dashboard/**/*.spec.ts"]

回答by Jason Aller

Changing to:

更改为:

"include": ["node_modules/dashboard/**/*.ts"],
"exclude": ["node_modules/dashboard/**/*.spec.ts"],

will fix the problem. From the documentation:

将解决问题。从文档

* matches zero or more characters (excluding directory separators)
? matches any one character (excluding directory separators)
**/ recursively matches any subdirectory

* 匹配零个或多个字符(不包括目录分隔符)
?匹配任意一个字符(不包括目录分隔符)
**/递归匹配任意子目录

It is the second use of ** that was causing too wide an inclusion to occur.

这是 ** 的第二次使用导致出现过宽的包含。