4 ms·
Have a look at the advanced compilation option: https://developers.google.com/closure/compiler/docs/api-tutorial3?hl=en https://developers.google.com/closure/co
by mythz 11y ago
Have a look at the advanced compilation option: https://developers.google.com/closure/compiler/docs/api-tutorial3?hl=en https://developers.google.com/closure/compiler/docs/api-tuto...
Basically when enabled it will rewrite normal property access, e.g: `myObj.foo` to something like `a.b` but wont shorten string literal access, e.g `myObj['foo']` just becomes `a.foo` which you would use for any API's you want to export.
For a real-world example I use string literals for declaring jquip's public API:
p['removeAttr'] = function(name){
return this['each'](function(){
if (this.nodeType == 1) this.removeAttribute(name);
});
};
Which closure advanced rewrites to:
q.removeAttr=function(a){return this.each(function(){this.removeAttribute(a)})};
Original: https://github.com/mythz/jquip/blob/master/dist/jquip.all.js https://github.com/mythz/jquip/blob/master/dist/jquip.all.js
Compiled: https://github.com/mythz/jquip/blob/master/dist/jquip.all.closure-advanced.js https://github.com/mythz/jquip/blob/master/dist/jquip.all.cl...
- nunull 11y agoMay i ask why you are using this notation? I honestly can't think of any benefits. Isn't it more convenient and readable to use `p.removeAttr`?
- mythz 11y agoMy comment explains the entire purpose for using it - if I didn't, it would've got rewritten into `a.b` and none of the public API's would work!
- eschutte2 11y agoI wish there were a better way to indicate which properties were renameable, because I need this too and I'm not willing to go as far as you did.
- Shebanator 11y agothere is a much better way to deal with this: https://developers.google.com/closure/compiler/docs/api-tutorial3#externs https://developers.google.com/closure/compiler/docs/api-tuto...
- eschutte2 11y agoThanks - I remember seeing that when looking through the docs, but I don't really like that either, unfortunately. I'm using Uglify right now, and I was thinking of adding a pre-minify step that walks the AST for a JSDoc-style annotation on either the properties themselves or a containing object, and pulls out the list of property names to preserve that way.
- greggman 11y agoYou can tell it which properties to keep with `goog.exportSymbol()`
- sanderjd 11y agoHow did it know it could leave out the `this.nodeType` check?