Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tsconfig.json for project with `src` and `tests`

Tags:

typescript

I've got a (desired) structure like this:

- tsconfig.json - src     - app.ts - tests     - appTest.ts     - appTest.js - dist     - app.js 

If there was no tests folder, a tsconfig.json like this would work fine:

{     "compilerOptions": {         "outDir":"dist"     },     "include" :[         "src/**/*.ts"     ] } 

However, if I add tests/**/*.ts to the include element, it also compiles my test files into dist and changes its folder structure (understandably, but undesirably).

Can I tell TypeScript compiler to include test files in the project to support things like refactoring but omit them from output to dist? Specifically, I'd like the .js to be compiled in the tests directory as suggested in the structure above.

like image 451
Borek Bernard Avatar asked Oct 29 '16 22:10

Borek Bernard


People also ask

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.

Where do I put Tsconfig json?

The tsconfig. json is generally put in the root folder of the project.

Does TS node use Tsconfig json?

ts-node automatically finds and loads tsconfig. json . Most ts-node options can be specified in a "ts-node" object using their programmatic, camelCase names. We recommend this because it works even when you cannot pass CLI flags, such as node --require ts-node/register and when using shebangs.


1 Answers

You may use rootDirs option within tsconfig.json such as:

{   "compilerOptions": {     "rootDirs": [       "src",       "tests"     ]   } } 

This can be looked up at Typescript documents, on this page (search for Virtual Directories with rootDirs subtitle): Module Resolution

like image 97
mcku Avatar answered Oct 20 '22 01:10

mcku