N. Taylor Mullen
N. Taylor Mullen

Reputation: 18311

Compile all TypeScript files into single TypeScript file

So I'm creating a TypeScript library and I can easily compile all generated JS files into a single file. Is there a way to compile all .ts and .d.ts into a single .ts file?

It'd be nice to support a pure TypeScript implemented lib (for those who want the nice intellisense and to be able to debug the core TS files) in addition to a JS supported version (lesser intellisense, can only debug generated files).

One solution would be to create a powershell script that concatenates all the files but I don't see a good way to do that dynamically (because of dependencies) without hard coding all of the file names in the correct order.

Upvotes: 3

Views: 5030

Answers (3)

basarat
basarat

Reputation: 276333

You can compile your files into a .d.ts and .js

If you have multiple typescript files and run:

tsc --out mylib.js --declaration first.ts second.ts 

You will get (mylib.js + mylib.d.ts) which you can then consume from typescript or javascript.

You can see an example here : https://github.com/basarat/ts-test/tree/master/tests/compileToSingle

Upvotes: 2

Grofit
Grofit

Reputation: 18465

I dont think there is a Typescript specific tool available which lets you basically munge all the files together, however if you have a build script just write a simple bit of code which locates all *.ts files within your project or desired scope, then just make a new file and keep appending these existing files to the new one. Then you will have a large my-project.ts which you can run through the tsc.exe with --declaration flag (or similar name) which would then output you a my-project.d.ts for it too.

If you are wanting to use it as a distributed library for others to use then you will have to package it in some way, be it one large .ts or some other mechanism... I asked a question along these lines before:

Can you create Typescript packages? like c# dlls

Upvotes: 0

Arihant Nahata
Arihant Nahata

Reputation: 1810


You have to use command line arguments of compiler

--out FILE Concatenate and emit output to single file

example

 tsc --out modules.js main.ts app.ts

The above answer is taken from this link

TypeScript compiling as a single JS file

Upvotes: 0

Related Questions