30

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.

BBaysinger
  • 6,614
  • 13
  • 63
  • 132

3 Answers3

66

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
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 2
    This isn't working. Still getting errors from files I'm trying to exclude. – BBaysinger Jan 25 '18 at 18:09
  • 1
    I've added some common reasons for this to my answer. I have created a sample project to check and it works for me. – Fenton Jan 25 '18 at 18:15
  • It would seem the first condition would be true then, since my includes (now) import `**/*.ts`. Can this be overcome? – BBaysinger Jan 25 '18 at 18:22
  • I asked my last question to you in more detail here: https://stackoverflow.com/questions/48449948/how-to-import-repo-with-uncompiled-typescript-in-angular-5-x – BBaysinger Jan 25 '18 at 19:02
  • 1
    Also, when you do that and you use aliases it will make them stop working. – Greg Wozniak Jan 30 '21 at 09:34
3

You can use

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

Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
  • I just tried this after checking out https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. It's not working. Still getting a slew of errors from the files I'm trying to ignore. – BBaysinger Jan 25 '18 at 18:01
  • Compiler errors. They look like: `node_modules/example/lib/app-body/example.spec.ts(6,1): error TS2304: Cannot find name 'describe'. node_modules/example/lib/app-body/example.spec.ts(7,3): error TS2304: Cannot find name 'beforeEach'.` – BBaysinger Jan 25 '18 at 18:56
0

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • I agree, this seems like it should work. It's not though. :/ – BBaysinger Jan 25 '18 at 18:07
  • 1
    Is it possible then that some of your included files are referencing your .spec.ts files? That will cause the ignore pattern to not be used. Alternatively is there a reason that you are specifying into the node_modules/dashboard directory? – Jason Aller Jan 25 '18 at 18:09
  • I searched the source for '.spec.ts', and it was only found in tsconfig.spec.json and tsconfig.app.json. I removed those just for fun, and it still includes the files. I'm importing TS from node_modules/dashboard because that is an internal repo with uncompiled TypeScript. It worked fine until upgrading to Angular 5.x, and now TS compiler doesn't allow uncompiled TS from a repo, which I understand. – BBaysinger Jan 25 '18 at 18:17
  • 1
    That is interesting. It feels like that would make a good separate question where you include that information and also the exact error messages you are getting and use the appropriate Angular tag on the question. – Jason Aller Jan 25 '18 at 18:23
  • Followup question: https://stackoverflow.com/questions/48449948/how-to-import-repo-with-uncompiled-typescript-in-angular-5-x – BBaysinger Jan 25 '18 at 18:59
  • @JasonAller your comment just saved my day. Rembmer this people! I was by mistake importing from a spec file, and boom nothing worked as expected – DauleDK May 30 '18 at 20:13
  • @e-cloud you'll have to be more specific. – Jason Aller Jun 13 '18 at 05:03