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?
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.
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).
To use TypeScript you need a build process to compile to JavaScript code.
As you know, TypeScript files can be compiled using the tsc <file name>. ts command.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With