Tsconfig.json - create only ts files from a folder

I am currently trying to create ts files in a single ts file. The problem I am getting is that my code below does not do what I thought. I used sourceRoot to try to establish the only place from where it could get the source code, but that didn't work. I also tried to put a. infront of the directory, but it still pulls from everywhere :(

{ "compilerOptions": { "target": "ES5", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, "out": "www/js/app.js", "sourceMap": true, "sourceRoot": "www/app" } } 

all files, including not inside www / app build :(

now I'm back to manually specifying files:

 { "compilerOptions": { "target": "ES5", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, "out": "www/js/app.js", "sourceMap": true }, "files": [ "./www/app/app.ts", "./www/app/menu/menuController.ts", "./www/app/playlists/playlistsController.ts" ] } 

Is it possible to limit the source directories to only www / app?

thanks

+12
typescript
source share
4 answers

Yes it is possible. Please use rootDir as 'rootDir': 'app' if www is your root directory for your application.

rootDir description from typescript compiler options :

Defines the root directory of input files.

+15
source share

According to the tsconfig schema :

"If the 'files' property is not in the tsconfig.json file, the compiler by default includes all files containing the directory and subdirectories. When the 'files' property is specified, only these files are included."

"If the 'files' property is not in the tsconfig.json file, but the 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 with the exclude property. If both are specified, the "files" property takes precedence.

the problem 'globes support in the tsconfig.json file property (or just the exclusion of files / directories)' reflects the current situation.

+11
source share

rootDir used only to control the structure of the output directory.

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

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

This question is a little older, I know. But some of the TypeScript compiler options listed here do not necessarily help resolve this issue. For people looking for rootDir or similar (like me), it may be helpful to clarify the options mentioned and related to the solution.

Extract all files into one file

✅ Use outFile

❌ Do not use out (deprecated)

Reference Information:

If you want to create a destination directory, select outDir . See compiler options for more information.

List files only from a specific directory

✅ Use files / include / exclude in tsconfig

❌ Do not use rootDir

Explanation:

The compiler finds all input files by

  • view file / include / exclude properties
  • following import statements
  • after ///<reference .. /> (should no longer matter)

If these parameters are not specified, all files in the root of the TypeScript project (provided by tsconfig.json) will be included. Import import automatically enabled by the compiler, regardless of file / include / exclude - take a look at their FAQ . All input files together are an envelope of files that it will create. How to configure file / include / exclude , see Tsconfig.json docs , also see @TSV answer.

Roottir

rootDir manages the structure of the output directory with outDir , it is not used to specify the input to the compiler. Its use ( quote ):

For each input file (i.e. .ts / .tsx file), you must create the corresponding output file (.js / .jsx file). To find out the path to the file of the generated output file, it will cut "rootDir" from the input and then add "outDir" to it.

Therefore, rootDir needs a directory that includes all of your input sources at the top, otherwise you will get

error TS6059: file is not located in the "rootDir" section. "RootDir" is expected to contain all source files

If rootDir omitted, the compiler automatically calculates the appropriate directory, taking into account all the input files at hand. So it does not have to be installed.

sourceRoot

This option is only relevant for source maps / debugging and may be omitted for the scope of the question. It is used when your source files are in a different place at runtime than during development. The compiler adjusts the source paths in the source map file to match the paths at runtime ( compiler options ).

Hope this clarifies things a bit.

+1
source share

All Articles