I am currently trying to build my ts files into a single ts files. The issue I\'m getting is that my code below isn\'t doing what I thought it would. I used sourceRoot to at
This question is a bit older, I know. But some of the TypeScript compiler options mentioned here are not necessarily helpful to solve the question. For people searching rootDir
or similar (like me), it may be helpful to clarify the mentioned and the solution-relevant options.
✅ Use outFile
❌ Don't use out
(deprecated)
Background:
If you want to to build to a destination directory, choose outDir
. See compiler options for further infos.
✅ Use files
/include
/exclude
in tsconfig
❌ Don't use rootDir
Explanation:
The compiler finds all input files by
file
/include
/exclude
propertiesimport
statements ///
(should not matter so much anymore)If those options are not specified, all files in your TypeScript project root (given by tsconfig.json) will be included. The import
ed modules are automatically included by the compiler, regardless of file
/include
/exclude
- have a look at their FAQ. All input files combined are the envelope of files it will build. How to configure file
/include
/exclude
, see tsconfig.json docs, consider also @TSV's answer.
rootDir
controls the output directory structure alongside with outDir
, it is not used to specify the compiler input. Its usage is (quote):
For every input file (i.e. a .ts/.tsx file) it needs to generate an matching output (a .js/.jsx file). To figure out the file path of the generated output file it will chop off the "rootDir" from the input, then prepend the "outDir" to it.
Consequently rootDir
needs a directory that includes all your input sources from above, otherwise you get
error TS6059: File is not under 'rootDir' .. 'rootDir' is expected to contain all source files
If rootDir
is omitted, the compiler automatically calculates a suitable directory by considering all input files at hand. So it doesn't necessarily have to be set.
This option is only relevant with sourcemaps/debugging and can be omitted for the scope of the question. It is used, when your sources files are at a different location at runtime than at design time. The compiler will adjust the paths to the sources in the sourcemap file to match the paths at runtime (compiler options).
Hope, that clarifies things a bit.