tsconfig.json - Only build ts files from folder

Yes,it is possible. Please use rootDir like 'rootDir': 'app', if www is your root dir of your application.

rootDir description from typescript compiler options:

Specifies the root directory of input files.


The rootDir is just used to control the output directory structure.

The output directory structure will be similar to your rootDir's directory. U can use the glob-like file patterns to restrict your source directories:

* matches zero or more characters (excluding directory separators)
? matches any one character (excluding directory separators)
**/ recursively matches any subdirectory

According to the tsconfig schema:

"If no 'files' property is present in a tsconfig.json, the compiler defaults to including all files the containing directory and subdirectories. When a 'files' property is specified, only those files are included."

"If no 'files' property is present in a tsconfig.json, but an 'exclude' property is present, the compiler will exclude the files and folders specified in the 'exclude' property."

According to the description:

The "files" property cannot be used in conjunction with the "exclude" property. If both are specified then the "files" property takes precedence.

The issue 'support globs in tsconfig.json files property (or just file/directory exclusions)' reflects current situation.


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 ///<reference .. /> (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.

Tags:

Typescript