Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tsconfig.json - Only build ts files from folder

Tags:

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 attempt to set the only place it could get the source from but that didn't work. I have also tried putting 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 those not inside of www/app build :(

for now I've moved back to manually specifying the 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 restrict the source directories to be only www/app?

Thanks

like image 628
Michael Crook Avatar asked Dec 06 '15 12:12

Michael Crook


People also ask

Can a single TypeScript project can have multiple Tsconfig json files?

You could use multiple tsconfig files to solve some of those problems, but new ones would appear: There's no built-in up-to-date checking, so you end up always running tsc twice. Invoking tsc twice incurs more startup time overhead. tsc -w can't run on multiple config files at once.

What can be defined in the Tsconfig JSON file?

json file is a file of JSON format which allows us to point the root level files and different compiler options to setup that require to compile a TypeScript based projects. The existence of this file in a project specifies that the given directory is the TypeScript project folder root.


2 Answers

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.

like image 191
Yauhen Avatar answered Oct 21 '22 18:10

Yauhen


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.

like image 42
ford04 Avatar answered Oct 21 '22 19:10

ford04