124

Is there an option to compile TypeScript code's output as minified? Or are we left to deal with that in a separate process? And does obfuscation affect the answer?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
LordZardeck
  • 7,953
  • 19
  • 62
  • 119

3 Answers3

95

The TypeScript compiler does not support the generation of minified or obfuscated code. you will need to use another tool against the JavaScript output of the compiler.

There's an open feature request: microsoft/TypeScript#8

Josh M.
  • 26,437
  • 24
  • 119
  • 200
mohamed hegazy
  • 9,191
  • 4
  • 30
  • 21
  • 1
    You can use YUI Compressor, JSMin or Closure Compiler to minify JS code. – niutech Oct 09 '12 at 14:17
  • 1
    that's what I ended up doing. I added a post-compile command passing the app.js file to the YUI compressor so that I essentially got compilation-time minification. – LordZardeck Nov 22 '12 at 06:06
  • 13
    Web Essentials can do it automatically from your ts'es. – joeriks Nov 22 '12 at 08:28
  • 1
    as @joeriks says, download WebEssentials2012, it can automatically create a min.js version of your file – JasonS Jan 07 '13 at 11:54
  • 29
    This is so bad. It sounds a very reasonable to produce minified code with the appropriate .map, if the all infrastructure is given in the TypeScript compiler. Also in VS case it could be a fully integrated solution, even a configuration setting. – g.pickardou Jun 18 '15 at 07:18
29

I am the author of typescript-closure-compiler.
Basically it is a tool that emits files that are compatible with google closure compiler, which is probably the best minifier/optimizer/obfuscator out there.
I've also created a playground that can show you the generated output.
You can take a look at an example project which uses this tool.
The minification process is done using gulp.

Sagi
  • 8,972
  • 3
  • 33
  • 41
6

The industry standard for minification is esbuild. (link)

Call esbuild after compiling to Javascript

esbuild your_file.js --minify --outfile=your_file.min.js

An example one liner:

tsc --strict --lib DOM,DOM.Iterable,ES6 --target ES6 your_file.ts && esbuild your_file.js --minify --outfile=your_file.min.js
Zamicol
  • 4,626
  • 1
  • 37
  • 42