Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. From a repo (with uncompiled source) I'm trying to include files that end in .ts, except for ones that end in .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.

like image 889
BBaysinger Avatar asked Jan 25 '18 17:01

BBaysinger


People also ask

What is exclude in Tsconfig json?

json file. The exclude array contains filenames or patterns that should be skipped when resolving the include array. The exclude option changes what the include option finds, effectively filtering out some folders or files from compilation.

How do I ignore test files in TypeScript?

To exclude test files from compilation, but still have them type checked, create a second configuration file, e.g. tsconfig. build. json , which uses the excludes array to exclude your test files from compilation when running the tsc command.

What is Tsconfig spec json for?

The tsconfig. json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig. json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.

What can be defined in the Tsconfig json file?

json file is a file of JSON format which allows us to point the root level files and different compiler options to setup that require to compile a TypeScript based projects. The existence of this file in a project specifies that the given directory is the TypeScript project folder root.


2 Answers

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

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
like image 156
Fenton Avatar answered Sep 24 '22 18:09

Fenton


You can use

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

like image 31
Nimeshka Srimal Avatar answered Sep 27 '22 18:09

Nimeshka Srimal