Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tsconfig - How to set correct compiler output location for multiple directories (Atom)

Tags:

typescript

Suppose I have this directory structure:

public/     js/         lib/ test/     ts/ lib/     ts/ 

How would I configure it to compile lib/ts/*.ts to public/js/lib/*.js and test/ts/*.ts to public/js/*.js?

I've tried setting up a separate tsconfig.json in each ts directory with the desired outDir, but as soon as I add a ///<reference to a file, the compiler outputs an unwanted directory tree (on save and on build.)

like image 701
pixelmike Avatar asked Apr 28 '15 00:04

pixelmike


People also ask

Which compiler option is best suited to read the TS files from the folder while Transpiling?

By default, TS compiler outputs transpiled files to the same directory where the original TS files is found. However, this can be changed using outDir compiler option.

Where do I put Tsconfig json?

The tsconfig. json is generally put in the root folder of the project.

How does the TypeScript compiler know how do you find your source files?

When you run tsc command in a directory, TypeScript compiler looks for the tsconfig. json file in the current directory and if it doesn't find one, then it keeps looking up the directory tree until it finds one. The directory where the tsconfig. json is located is considered as the root of the project.

What is Rootdir in Tsconfig?

Default: The longest common path of all non-declaration input files. If composite is set, the default is instead the directory containing the tsconfig. json file. When TypeScript compiles files, it keeps the same directory structure in the output directory as exists in the input directory.


2 Answers

I ended up getting what I wanted with this layout:

public/     js/         lib/         test/ src/     ts/         lib/         test/ 

In src/ts/test/tsconfig.json:

"outDir": "../../../public/js" 

In src/ts/lib/tsconfig.json:

"outDir": "../../../public/js/lib" 

In src/ts/test/test.ts:

/// <reference path="../lib/CoolStuff.ts" /> 

In Atom, if you're working in src/ts/lib, building will compile those files into public/js/lib.

If you're working in src/ts/test, the build will compile *.ts in test - as well as all files referenced. I don't see a way to prevent referenced file compilation, but at least with this layout they go to the same location.

like image 118
pixelmike Avatar answered Sep 21 '22 11:09

pixelmike


How would I configure it to compile lib/ts/.ts to public/js/lib/.js and test/ts/.ts to public/js/.js?

If you want to compile test and public in a single compilation context then effectively your ts tree is :

test/     ts/ lib/     ts/ 

Therefore if you use an outDir of ./public/js you will get:

public/     js/         test/             ts/         lib/             ts/ 

This is because the relative nature of lib/ts to test/ts needs to be preserved by outDir. This is a limation in how you are trying to organize your project.

Reorganize your project as

ts/     test/     lib/ 
like image 30
basarat Avatar answered Sep 23 '22 11:09

basarat