javascript - Closure compiler export Typescript classes and functions -
i trying use closure compiler advanced mode on typescript generated classes no success. there has accomplished such things.
typescript class
class testdata { blogname: string; cachetimeout: number; copyrightholder: string; constructor(blogname: string, cachetimeout: number, copyrightholder: string) { this.blogname = blogname; this.cachetimeout = cachetimeout; this.copyrightholder = copyrightholder; } addblog(value: string): boolean { console.log('add blog'); return true; } validate(): boolean { console.log('all valid'); return true } } var mytestdata = new testdata("name",22,"cpyright");
generated code
var testdata = (function () { function testdata(blogname, cachetimeout, copyrightholder) { this.blogname = blogname; this.cachetimeout = cachetimeout; this.copyrightholder = copyrightholder; } testdata.prototype.addblog = function (value) { console.log('add blog'); return true; }; testdata.prototype.validate = function () { console.log('all valid'); return true; }; return testdata; })();var mytestdata = new testdata();
this compiles
new function() {};
i understand should provide exports, added
window['testdata'] = testdata; window['testdata'].prototype['addblog'] = testdata.prototype.addblog window['testdata'].prototype['validate'] = testdata.prototype.validate
my output closure compiler advanced compilation
var = function() { function b() { } b.prototype.a = function() { console.log("add blog"); return !0; }; b.prototype.b = function() { console.log("all valid"); return !0; }; return b; }(); window.testdata = a; window.testdata.prototype.addblog = a.prototype.a; window.testdata.prototype.validate = a.prototype.b; new a;
if see there still no constructor code left. gets worse when add inside module.
i tried use @export of google closure wiht no success
i see couple of pluggins can generate closure compiler annotations based on typescript, doesnt generate proper code.
i ran basic test of this. perhaps changed code , haven't re-tried.
if compile typescript in question, should result in following javascript:
var testdata = (function () { function testdata(blogname, cachetimeout, copyrightholder) { this.blogname = blogname; this.cachetimeout = cachetimeout; this.copyrightholder = copyrightholder; } testdata.prototype.addblog = function (value) { console.log('add blog'); return true; }; testdata.prototype.validate = function () { console.log('all valid'); return true; }; return testdata; })(); var mytestdata = new testdata("name", 22, "cpyright");
in particular, last line passes arguments testdata
constructor.
a quick run of results in (white-space mine) using @compilation_level simple_optimizations
:
var testdata=function(){ function a(a,b,c){ this.blogname=a;this.cachetimeout=b;this.copyrightholder=c } a.prototype.addblog=function(a){console.log("add blog");return!0}; a.prototype.validate=function(){ console.log("all valid");return!0 }; return }(),mytestdata=new testdata("name",22,"cpyright");
if use advanced optimizations on partial code, aggressive. need supply all of code closure compiler understand isn't used.
if example represents of code, you'll notice constructor along 3 properties (blogname, cachetimeout, , copyrightholder) genuinely never used, can removed without affecting behaviour of program.
Comments
Post a Comment