News for hackers. Stuff that matters.
Re:Closure Compiler: a tool for making JavaScript download a... by Drakim on Monday August 31, 2015 @12:04PM (#10147435)
How can it determine dead code for such a dynamic language like JavaScript?

Is it limited to obvious stuff like things after a "return;" statement that cannot run? Because I don't see how it could remove unused functions when you can invoke them in such indirect ways: myobj['f'+'oo']();

Re:Closure Compiler: a tool for making JavaScript download a... by mythz on Monday August 31, 2015 @12:20PM (#10147517)
Have a look at the advanced compilation option: 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

Compiled: https://github.com/mythz/jquip/blob/master/dist/jquip.all.cl...

Re:Closure Compiler: a tool for making JavaScript download a... by sanderjd on Monday August 31, 2015 @01:08PM (#10147813)
How did it know it could leave out the `this.nodeType` check?
Re:Closure Compiler: a tool for making JavaScript download a... by tomjen3 on Monday August 31, 2015 @01:51PM (#10148070)
If it can prove that it will always be 1 (ie by code not shown) it will be removed.
Re:Closure Compiler: a tool for making JavaScript download a... by sanderjd on Monday August 31, 2015 @04:37PM (#10148950)
Yeah, I was curious how it could prove that in this specific case. Very neat, in any case.
Re:Closure Compiler: a tool for making JavaScript download a... by nunull on Monday August 31, 2015 @12:32PM (#10147595)
May 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`?
Re:Closure Compiler: a tool for making JavaScript download a... by mythz on Monday August 31, 2015 @12:44PM (#10147660)
My 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!
Re:Closure Compiler: a tool for making JavaScript download a... by eschutte2 on Monday August 31, 2015 @12:49PM (#10147684)
I 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.
Re:Closure Compiler: a tool for making JavaScript download a... by greggman on Monday August 31, 2015 @01:15PM (#10147860)
You can tell it which properties to keep with `goog.exportSymbol()`
Re:Closure Compiler: a tool for making JavaScript download a... by Shebanator on Monday August 31, 2015 @01:13PM (#10147854)
there is a much better way to deal with this:

https://developers.google.com/closure/compiler/docs/api-tuto...

Re:Closure Compiler: a tool for making JavaScript download a... by eschutte2 on Monday August 31, 2015 @11:41PM (#10150598)
Thanks - 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.
Re:Closure Compiler: a tool for making JavaScript download a... by sgrove on Monday August 31, 2015 @12:08PM (#10147457)
You have to restrict your code to a certain style (and there are warnings for whenever you're defeating the DCE, I believe). You'll particularly see this when you're accessing properties via array-index-like lookup (your myObj['f'+'oo']) example) instead of e.g. myObj.foo under advanced compilation, where the property name will have been munged and doesn't exist.

Like anything else, there are tradeoffs, and Closure asks that you restrict yourself to a more predictable set of patterns in return for the benefits it offers.

Re:Closure Compiler: a tool for making JavaScript download a... by lclemente on Monday August 31, 2015 @12:07PM (#10147449)
Re:Closure Compiler: a tool for making JavaScript download a... by dahart on Monday August 31, 2015 @12:12PM (#10147477)
You tell the compiler a file that it's not allowed to dead-strip. From there, it then computes the transitive closure of the rest of your code that is referenced from the "entry point", and removes everything else. It is, of course, possible to reference things in indirect ways that the compiler can't see, so they provide some mechanisms to force inclusion if necessary.
Re:Closure Compiler: a tool for making JavaScript download a... by rav on Monday August 31, 2015 @12:09PM (#10147461)
This kind of indirection is specifically not supported even with simple optimizations: https://developers.google.com/closure/compiler/docs/limitati...
Re:Closure Compiler: a tool for making JavaScript download a... by reitzensteinm on Monday August 31, 2015 @12:14PM (#10147486)
You have to follow a set of rules - your example would not be allowed.

One of the nice things about ClojureScript is that the compiler will output compatible code, so you don't even really have to think about it (except when doing JS interop). If you write valid ClojureScript, it Just Works.

Re:Closure Compiler: a tool for making JavaScript download a... by teamhappy on Monday August 31, 2015 @12:15PM (#10147491)

    function add(a,b) { return a+b }
    console.log(add(1,2))
Compiles to

    console.log(3)
I'm not sure about your example, but I think it would work. (Apparently not)