简体   繁体   English

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

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

I'm trying to include some files in my tsconfig.json, but it's including files I don't want to be included.我试图在我的 tsconfig.json 中包含一些文件,但它包含了我不想包含的文件。 From a repo (with uncompiled source) I'm trying to include files that end in .ts , except for ones that end in .spec.ts .从存储库(带有未编译的源代码)中,我试图包含以.ts结尾的文件,.spec.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"],

(Then) new to Unix glob patterns/strings, I need ones to match and exclude the files correctly, and then how to add them to the config. (然后)Unix glob模式/字符串的新手,我需要正确匹配和排除文件,然后如何将它们添加到配置中。

TheTypeScript handbook has a good write up on tsconfig file . TypeScript 手册对 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)如果您使用的是旧版本的编译器(早期版本的 tsconfig 不允许使用通配符)
  4. You are compiling using tsc app.ts (or pass other arguments) - your tsconfig is ignored when you do this您正在使用tsc app.ts (或传递其他参数)进行编译 - 执行此操作时会忽略您的 tsconfig

您可以使用

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

Changing to:更改为:

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

will fix the problem.将解决问题。 From thedocumentation :文档

* 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. ** 的第二次使用导致出现过宽的包含。

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

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