Say I put my code under src
and tests under spec
:
+ spec
+ --- classA.spec.ts
+ src
+ --- classA.ts
+ --- classB.ts
+ --- index.ts
+ tsconfig.json
I want to only transpile src
to the dist
folder. Since index.ts
is the entry point of my package, my tsconfig.json
look like this:
{
"compileOptions": {
"module": "commonjs"
"outDir": "dist"
},
"files": {
"src/index.ts",
"typings/main.d.ts"
}
}
However, this tsconfig.json
does not include the test files so I could not resolve dependencies in them.
On the other hand, if I include the test files into tsconfig.json
then they are also transpiled to dist
folder.
How do I solve this problem?
I ended up defining multiple config files and use
extends
to simplify them.Say I have two files:
tsconfig.json
andtsconfig.build.json
This way, I can have fine control on what to build (using
tsc -p tsconfig.build.json
) and what thets language service
(IDE) handles.UPDATE: now as my projects grow, I ended up having more config files. I use the "extend" feature that is now available in TypeScript:
This is somewhat dependent on whatever testing framework you're using but I like to use ts-node to compile my test files. Using mocha, your
npm test
script might look like:In your tsconfig.json, make sure to remove the
rootDir
option.When you try to run typescript with
rootDir
set tosrc
or whatever the base folder for your application code is, it'll disallow any compilation in a directory that sits outside, such atests
. Usingts-node
, you can easily keep everything separate without having to have separate TypeScript configuration files.I think you should not use 'files' option in your config. Instead you can exclude unwanted files and have it like this:
This will preserve your original structure in the 'dist' folder without mixing tests and app js files: