Suggestion: minification
TypeScript should support emitting minified JavaScript.
There are several different things we could support:
- Just remove whitespace
- Minify unobservable identifiers
- Remove provably dead code
- Whole-program minification (i.e. closure compiler)
- (Others?)
I'll be posting the minification feature to TsProject this coming week. I am working out the last bits of the implementation for ES5 vs ES6 ( block scoped identifiers using let ).
Here is an example of minifier in TsProject ( whitespace elimination turned off ). Source:
export class TestClass {
private gerb1: number = 1;
private gerb2: number = 2;
private gerb3: number = 3;
private gerb4: number = 4;
public fPublic(): number {
let result = this.f1();
return result;
}
private f1(): number {
let result = this.f2( 5, 2 ) + this.f3( 19 );
return result;
}
private f2( parm1: number, parm2: number ): number {
let result = parm1 + parm2;
return result;
}
private f3( parm1: number ): number {
return parm1 + this.f2( 1, 2 );
}
}
var test = new TestClass();
var t1 = test.fPublic();
console.log( t1 );
min.js output...
var TestClass = (function () {
function TestClass() {
this.a = 1;
this.b = 2;
this.c = 3;
this.d = 4;
}
TestClass.prototype.fPublic = function () {
var a = this.e();
return a;
};
TestClass.prototype.e = function () {
var a = this.f(5, 2) + this.g(19);
return a;
};
TestClass.prototype.f = function (a, b) {
var c = a + b;
return c;
};
TestClass.prototype.g = function (a) {
return a + this.f(1, 2);
};
return TestClass;
})();
exports.TestClass = TestClass;
var a = new TestClass();
var b = a.fPublic();
console.log(b);
@ToddThomson Can you compare and post the minified (without whitespace elimination) gzipped file size with the normal gzipped size? Gzip stores a long identifier name that is used multiple only once, so I suspect that mangling private identifiers won't make a big difference.
@ivogabe I'll post statistics when the minifier feature is production ready. I suspect that there won't be great gains over post compilation gulp build tools like uglifyjs. However, my goal is to be able to realize some gains by shortening private identifiers and knowing that the resulting transformed sourcefile will work 100% of the time.
Entropy can be reduced by mangling the names of private variables.
If you have 25 instances of separate long names, this will be 25 different new symbols that need to be put into the Huffman table.
Comparatively, 25 instances of this.a would only result in one key being added to the Huffman table (a),
I expect the compression of the latter case would be much higher.
This is great work Todd!
@ToddThomson Glad to hear you're working on this! A couple of quick questions:
- If I understand correctly, this minification pass is meant to operate on a single compilation unit. Is that correct?
- Is that branch public, or likely to be soon?
@joelgwebber Yes, the main reason I use TsProject is to produce a single bundled output for multiple ES6 modules in separate source files. Yes, it is a public GitHub repository. There's a link above to the TsProject bundle minification issue that will take you to the repository.
Thanks, @ToddThomson. Got a 404 for that branch, but I see now that it's been merged.
@joelgwebber , @bryanerayner I've published TsProject 1.2.0-rc.3 to NPM. It now includes proper handling of class inheritance. I've also added a sample minified typescript app to the GitHub repository.
@RyanCavanaugh I'm not sure how my basic support for identifier minimizing and whitespace elimination can be brought to Typescript. It appears to me that what I've provided in TsProject would be easily achievable within Typescript, but it is something that should be designed and implemented by the core group.
Last post concerning minification in TsProject. With the latest NPM release, I've built TsProject.min.js with full whitespace elimination. I also took the non-minified tsproject.js and ran it through the online JSCompress tool: Here are the results: TsProject.js 110,908 TsProject.min.js 42,125 TsProject.js through jscompress online: 47,940. TsProject.js through Uglify online: 42,722 Not startling results, but a pretty good start.
Final update - With non-external functions, classes and import namespace aliases minified:
TsProject.js 132,963 TsProject.min.js 40,389
TsProject.min.js is a node package built using TsProject.min.js.
Just to pile on to this issue... We are currently starting a new JS project and are primarily trying to decide between TypeScript and the Google Closure Compiler. Both offer the ability to add static typing to JavaScript (ish) applications. In my view, TypeScript has much better tooling and is in general a more pleasant development experience. It's integration with IDE's is good, the compiler is much faster than closure, the syntax is better (less verbose), and to tool chain is better.
However, we really do like the ADVANCED_COMPILATION mode. It can:
- More aggressively minify based on type information.
- Essentially make private methods unusable through renaming
- Makes it harder to reverse engineer
It does significantly better than using something like UglifyJS on transpiled TypeScript. The files output by the closure compiler are significantly smaller. Although I a no expert in how this is achieved, as others have mentioned it seems to be that the fact that the "minifier" in closure is type-aware allows it to do more aggressive minification. UglifyJS (and the like) do not have this benefit. Doing optimizations and minification inside the type script transpiler should allow it to outperform "bolt on" minifiers.
I am not sure if we will use Closure or TypeScript, but I can say that this particular issue is one of the main decision points for us.
@mmacfadden, I do not see this as an either/or. I know of many teams including Google Angular 2 team that use Typescript + Google Closure Compiler advanced optimizations and are satisfied by the results. You will need something like https://github.com/angular/sickle to convert TS types into JSDoc comments.
@mhegazy, thanks for the reply we had seen a few TS -> closure projects out there but they seemed very out of date. Although I had searched, I had not come across this project before. It may be a viable option. I have two comments:
- I do get worried with projects like this only because there are now three things that have to work well together in order for my build to work. If type script adds a new (relevant) feature, it then needs to be added to sickle. It also then needs to be something that actually is possible to do in closure. The Sickle developers are (perhaps) not on the TypeScript team nor the Closure team, so there is some risk there that at some point in the future the two will not play nice together. Or that the Sickle tool becomes unmaintained. There is just some risk there.
- From a build complexity standpoint I now have two things I need to wrangle instead of one. Not a big deal, but still a small consideration.
So if I had a magic wand I would love to see this capability right in TypeScript. I do realize, being a developer myself) that the TS team needs to focus on their sweet spot of functionality and that building a Type Aware minifcation system probably is slightly out of their sweet spot. I am totally willing to take a look at Sickle and if it works... great, I think my immediate issue will be solved. But again, assuming a magic wand, I would still love to see this in TS directly.
Again thanks for pointing this project out. I will definitely take a look.
type aware minification is not all what advanced optimizations in closure compiler. if that is the only thing you need @ToddThomson's work should do the trick.
You are correct. I am looking for more than just Type Aware minification. Being type aware does let you do some other interesting optimizations, but you are correct there are more things that it does. I am also interested in what @ToddThomson is doing.
Hi @mhegazy, would like to know how the angular 2 team use the TypeScript together with Google Closure Compiler. Looks that the uglify is used in the angular2 github repository https://github.com/angular/angular/blob/4a414420e940bf4a5eea2c870ddff6e33f0343b9/gulpfile.js#L1273.
//CC @mprobst from the angular team to answer this question.,
@mmacfadden @raphaelfeng we're still experimenting around, there's no definite answer from our side yet. We're using uglify and seeing good results with that.
We're also building sickle and a compilation toolchain that pipes from TypeScript through Closure Compiler. We hope to possibly give better optimization results from that than uglify itself - mostly due to property renaming, dead code elimination, but also inlining, type specialization, and a host of other tricks. However it is true that it will cause a more complicated build setup expect (e.g. users will need to install Java), and issues when the two tools might not agree. It's hard to know if the end result will be nice to use before trying.
We don't quite know yet where this will lead, but will keep people posted. Keep an eye on https://github.com/angular/sickle for our progress.
@mprobst, thanks! I am optimistic that something like sickle could be more successful because the angular team and the typescript teams are working closely together and also because Closure is a Google product. So it's not as though some independent and unrelated folks are trying to do the work. Definitely interested in sickle and will monitor the project.