user6794421
user6794421

Reputation:

How can I compile different files to different folders, in TypeScript?

Picture this: I've got main.ts and sw.ts.

How can I compile main.ts to js/ folder and sw.ts to root directory? Can you give me an example?

Upvotes: 4

Views: 2788

Answers (2)

user6794421
user6794421

Reputation:

I've found the solution with the help of this page: Projects References.

Step-by-step

First

What worked for me was creating a new tsconfig.json file for each directory. After that, you should use this, in the main tsconfig.json:

"references": [
    { "path": "PATH/TO/EACH/OTHER/TSCONFIG.JSON/FILES" }
]

Second

On top of that, you also must add this option to the compilerOptions of each secondaries tsconfig.json files:

"composite": true

Third

Also, use "include" to point to each .ts files that that particular tsconfig.json will use:

"include": [
    "./homeWork.ts",
    "./myGame.ts"
]

or

"include": [
    "./*.ts" //Points to every .ts file inside that directory.
]

Fourth

Now, you should be able compile it all, at once, using any of these commands from the same directory as the main tsconfig.json:

  • tsc --build;
  • tsc -b //Alias for the above command;
  • tsc -w -b //Above command, including the option -w for watching changes recursively.

Considerations

  • Note that, using these commands, the compiler will go through each reference that each tsconfig.json may have;
  • This might be a good way to avoid colison between the DOM and WebWorker libraries;
  • The references always generate declaration files;
  • Compile time gets slower, though I don't know if it is my mistake or a natural thing.

Upvotes: 9

artem
artem

Reputation: 51809

It's impossible within a single compilation.

You will have to run tsc twice with different tsconfig.json files or -outDir options.

Upvotes: 0

Related Questions