Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to compile TypeScript into minified code?

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?

like image 866
LordZardeck Avatar asked Oct 01 '12 22:10

LordZardeck


People also ask

Can TypeScript be compiled?

Compiling TypeScript. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It offers classes, modules, and interfaces to help you build robust components.

Is Minified code faster?

Minification does improve performance for two reasons: Reduced file-size (because it removes comments and unnecessary white spaces), so your script loads faster. Even if it is embedded into the <head> . It is parsed faster, since comments and white spaces don't have to be explicitly ignored (since they're not there).

Does TypeScript need to be compiled?

To use TypeScript you need a build process to compile to JavaScript code.

Which command is used to compile TypeScript?

As you know, TypeScript files can be compiled using the tsc <file name>. ts command.


3 Answers

The TypeScript compiler does not support the generation of minifies 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

like image 131
mohamed hegazy Avatar answered Oct 17 '22 01:10

mohamed hegazy


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.

like image 26
Sagi Avatar answered Oct 17 '22 02:10

Sagi


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
like image 1
Zamicol Avatar answered Oct 17 '22 01:10

Zamicol