tsconfig.json - Only build ts files from folder

前端 未结 4 2082
慢半拍i
慢半拍i 2020-12-20 11:24

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

4条回答
  •  天涯浪人
    2020-12-20 12:03

    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.

    Emit all files into one single file

    ✅ 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.

    Only emit files from a certain directory

    ✅ Use files/include/exclude in tsconfig

    ❌ Don't use rootDir

    Explanation:

    The compiler finds all input files by

    • looking at file/include/exclude properties
    • following import statements
    • following /// (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 imported 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

    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.

    sourceRoot

    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.

提交回复
热议问题