Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude specific files in typescript only for the build?

Is it possible to exclude all test files only for the build but use them with nodemon to run tests locally? When I exclude test files within the tsconfig.json file I get a typescript error that it can't find the types of the testing library like jest in my case.

Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.ts(2582)

{
  "compilerOptions": {},
  "exclude": [
    "**/*.test.ts"
  ]
}

I am wondering since I guess the temporary transpiling is put into another folder than the build folder.

like image 369
thiloilg Avatar asked Oct 19 '19 07:10

thiloilg


People also ask

How do I ignore files in TypeScript?

Use // @ts-ignore to ignore the type checking errors on the next line in a TypeScript file. If you use a linter, you might have to add a comment to also suppress linting errors when using ts-ignore - // eslint-disable-next-line @typescript-eslint/ban-ts-comment .

What option should we use in the Tsconfig file to exclude files from TypeScript compilation?

Use the exclude option in your tsconfig. json file to exclude a folder from compilation in TypeScript. The exclude option changes what the include setting finds and defaults to node_modules and bower_components .

What is Tsconfig build json?

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 is include and exclude in Tsconfig json?

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)


1 Answers

One possible solution would be to use two different tsconfig files, one for the tests and one for the production build.

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "./build",
    "baseUrl": ".",
    "paths": {
      "*": ["types/*"]
    },
    "strict": true,
  }
}

tsconfig.prod.json

{
  "extends": "./tsconfig",
  "exclude": ["**/*.test.ts", "**/*.mock.ts"]
}

Then point tsc to the new config when running tsc -p tsconfig.prod.json

like image 100
thiloilg Avatar answered Sep 16 '22 15:09

thiloilg