4

Is there any way how to compile TypeScript source files into single JavaScript bundle file?

We've build tool on node.js and TypeScript compilation is performed by

var ts = require('typescript');
...
var program = ts.createProgram(this.getAllInputFileNames(), this.typeScriptOptions, host);
var output = program.emit();

Currently, the typeScriptOptions is:

{ target: 1, module: 2 }

I was trying to add out into the typeScriptOptions so it was

{ target: 1, module: 2, out: 'bundle.js' }

But no luck, the compiler still generates a lot of .js files ...

What option is needed to force TypeScript compiler to compile all input .ts files into single file?

EDIT:

With the above options the compiler generates a lot of .js files as described above. Also bundle.js is created but it is empty file.

2
  • Don't you have on .ts file that essentially has reference to the other files? If so, you can build just that .ts file and forget about the other ones. Commented Jul 27, 2015 at 14:31
  • It doesn't help, it still generates single .js files. The point is whether the out option is correct... Commented Jul 27, 2015 at 15:11

1 Answer 1

3

tsc is a compiler/transpiler of TypeScript code into JavaScript and that's it. Sure it has some options in terms of the output of what it produces but essentially its job is to create output JavaScript.

What you do with it from there is up to you.

My suggestion to bundle things up is to use browserify to bundle your code.

In short, run it pointing at your outputted JavaScript main file and supply an output bundle file and it will create a single bundle file with all the JavaScript in the appropriate order based upon dependencies.

Sign up to request clarification or add additional context in comments.

7 Comments

TypeScript does have a compiler option (out) to bundle all output into one file. It just seems it does not work for the op when invoked via API.
Understood, I was presenting a different option to get to the desired results as there are many who do not care for using --out ref: github.com/TypeStrong/atom-typescript/blob/master/docs/out.md
Thanks for browserify as another option to solve this issue. I'm still looking for solution with tsc compiler because it's the feature of it and perhaps the simplest solution...
O.K. there are guys who say --out is bad. But surely we need a bundle in the production. The link you provided above recommends that bundle should be created by some build tool (as you say too). But why to have another tool to create bundle.js when we have tsc compiler and bundling definitely has something to do with compilation? Moreover, tsc still has the option --out, but not working. It's nothing else than regression bug in TSC 1.5.
The argument against doing that is compartmentalization. Do one thing and do it well, so tsc compiles and lets other tools put Humpty Dumpty together into a bundle.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.