typescript tsconfig.json include exclude not working

Zakk picture Zakk · Oct 15, 2017 · Viewed 10.8k times · Source

I have a web project

I have folders

src/app/shared/models
src/app/shared/services
src/app/shared/types

Each one of those is subfolder that have folders or files inside it, I want to exclude those folders, So i tried:

  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    "src/app/shared/**/*"
  ]

and its not working, even "src/app/shared/*" not working, How I can do it?

My tsconfig.json now:

{
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2015",
      "dom"
    ]
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    "src/app/shared/**/*"
  ]
}

Thanks

Answer

Benjamin Vincent picture Benjamin Vincent · Mar 6, 2019

The first thing to consider, if you decide to use the “exclude” keyword to list files is that, in order to remove them from the compilation process, you will need also to exclude any other files, which by using either a triple-slash directive "/// <reference path="..." />" or an "import" statement refer to a file in your excluded list.

tsconfig.json turns a folder into a “project”. Without specifying any “exclude” or “files” entries, all files in the folder containing the tsconfig.json and all its sub-directories are included in your compilation.

tsconfig.json automatic inclusion does not embed module resolution. If the compiler identified a file as a target of a module import, it will be included in the compilation regardless if it was excluded in the previous steps.

The include and exclude properties take a list of glob-like file patterns. The supported glob wildcards are:

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

When using the files property it takes a list of relative or absolute file paths.

Setting "baseUrl" informs the compiler where to find modules. All module imports with non-relative names are assumed to be relative to the baseUrl.

Files included using "include" can be filtered using the "exclude" property. However, if you include files explicitly using the "files" property they would be always included regardless of "exclude".