top | item 13116526

Closure Compiler in JavaScript

228 points| espeed | 9 years ago |developers.googleblog.com

104 comments

order
[+] jashkenas|9 years ago|reply

    > How does this work?
    > 
    > This isn't a rewrite of Closure in JavaScript. Instead, we compile 
    > the Java source to JS to run under Node, or even inside a plain old
    > browser. Every post or resource you see about Closure Compiler will
    > also apply to this version.
Pretty wild.
[+] Cyph0n|9 years ago|reply
I'm really interested in how they did that. Do they have an internal Emscripten equivalent for Java bytecode? Or would it be more like a source code conversion compiler a la Nim or Haxe?
[+] johnhenry|9 years ago|reply
This is Google's JavaScript to JavaScript compiler, originally written in Java, but compiled with their Java to JavaScript compiler so that it runs in JavaScript... fair enough...
[+] paulddraper|9 years ago|reply
I'm not sure this is what Gosling had in mind when he said "compile once, run anywhere" :P
[+] minxomat|9 years ago|reply
We need to go deeper...
[+] strictfp|9 years ago|reply
You forgot to include V8!
[+] manishsharan|9 years ago|reply
Can anyone comment on the state of Typescript - Google Closure integration ?
[+] kylecordes|9 years ago|reply
This is a great question, and to clarify for anyone not following along closely:

Some of us using TypeScript dream of a day when TypeScript emits JavaScript already annotated with type meditations which express as much of the typescript type system as possible in a way that Closure can understand, thereby making it possible for Closure to perform maximally efficient tree shaking and other magic with code that started as TypeScript. Such a thing as necessary and helpful because the addition of this detailed type data makes it possible for Closure to do a much better job that it can do with "just" untyped JavaScript.

[+] msoad|9 years ago|reply
Tsickle isn't that great, Webpack plugings are not not letting CC do the bundling. In general it's not good!
[+] technion|9 years ago|reply
I just tested moving my Typescript/React template to Google Closure using a webpack plugin. It came in at 148KB with UglifyJS, and 102KB with Google Closure running after Typescript did the ES5 conversion.

This may not be the "integration" some are seeking but it's sure a step ahead.

[+] nojvek|9 years ago|reply
What do you mean by Typescript Google closure integration?

Typescript understands most of closure comment syntax. They should throw typechecking errors which they currently aren't doing.

[+] xntrk|9 years ago|reply
Does anyone know what this means for clojurescript?
[+] swannodette|9 years ago|reply
Nothing. Zero benefits here for typical usage of ClojureScript. Potentially interesting for 3rd party bootstrap efforts like Planck, Lumo, etc.
[+] yegortimoshenko|9 years ago|reply
It makes a completely self-hosted ClojureScript compiler possible.
[+] tomseldon|9 years ago|reply
So this is a shameless, inexcusable plug but as it's relevant to the blog post...

If you're using Google Closure with Gulp in your work flow, you may find gulp-gjslint useful (https://github.com/TomSeldon/gulp-gjslint).

It's a gulp wrapper around gjslint, I. E. a Google Closure linter. :)

[+] nhatbui|9 years ago|reply
Thanks! I'm fairly new to JS. I introduced a new dependency on a node module and for some reason gulp-uglify isn't working on production mode. I'm going to give this a try.
[+] sigjuice|9 years ago|reply
What's with all these similar and confusing names; Closure Compiler, Clojure, Clozure CL?
[+] focusaurus|9 years ago|reply
So "Closure" is a real comp sci word used to describe variable scope lifetimes in languages includes JavaScript and others. So when google named their project, they basically took a word from JavaScript/CompSci that starts with the same letter as "Compiler" (plus other similar sounds). I don't think there's much more to it than that.

"Clojure" is a programming language again based on the regular word "closure" but with some twists: The "j" is a tip of the hat to Java and the JVM where clojure is designed to run. This also has the nice property that the "zh" sound of the "s" in closure is also used with "j" sometimes (I think due to French). Thirdly I believe the "CL" letters of clojure are a tip to Common Lisp.

I'm not familiar with "Clozure CL" but the history is here: http://ccl.clozure.com/history.html

[+] nothrabannosir|9 years ago|reply
This is fantastic. I never thought I'd be celebrating yet another "now-in-JS" release, but clojure being Java was a constant pain in the CI gonads. Wonderful to finally be able to include this in generic npm packages without any crazy extraneous build steps.
[+] jjdveifb|9 years ago|reply
Also probably 10x slower, at least! Still cool because JavaScript I guess
[+] victorvation|9 years ago|reply
Interesting to see what the use case for this will be - will removing the JVM dependency really outweigh the increase in compilation time?
[+] sunkencity|9 years ago|reply
Wow! The center of the maze. Now it's just a matter of time before clojurescript is self hosted and the Eschaton is Immanentized.
[+] paulddraper|9 years ago|reply
Earlier post: https://news.ycombinator.com/item?id=12415512

(PS Is it possible to have a biased account? I see a lot of articles I had posted, later posted by someone else make it to the front page.)

[+] grzm|9 years ago|reply
I think it's mostly just a timing issue. To make it to the front page a submission needs to garner enough upvotes at a specific rate, and it's competing with other submissions at the same time.
[+] wehadfun|9 years ago|reply
offtopic: googleblog.com vs blog.google?
[+] triangleman|9 years ago|reply
could be a security measure.
[+] antouank|9 years ago|reply

    > As this compiler runs in pure JavaScript, the compiler cannot load or save files from your filesystem directly.
Why? Node.js has the fs library for that. Why do I have to use gulp?
[+] jeremiep|9 years ago|reply
They also support webpack out of the box. You can most likely add build tools by doing your own integration as well.
[+] nfriedly|9 years ago|reply
Closure compiler is awesome, and the need to spin up a JVM to run it is really my only knock against it. This should lead to a nice boost in its usage.
[+] austincheney|9 years ago|reply
Bah. It still requires a build step. I will stick with Pretty Diff and its "correct" option.
[+] leeoniya|9 years ago|reply
performance is significantly worse for the simple example, anecdotally feels maybe 300%-400% slower.

not sure if it's just bootup time or actual compilation passes, hopefully the former and does not explode exponentially (or even linearly) with code size.

[+] paulddraper|9 years ago|reply
I'm sure it's just the Java-on-Node overhead. 3-4x isn't awful considering.
[+] SpencerWood|9 years ago|reply
Why would I use this over babel?
[+] wildpeaks|9 years ago|reply
Perhaps you meant UglifyJS or Babili instead?

Babel can be used in the same toolchain as Closure Compiler, they're not mutually exclusive or redundant as they perform different tasks.

[+] icholy|9 years ago|reply
They do different things.
[+] untog|9 years ago|reply
It's not the same as Babel. You'd use it instead of Webpack, for example.
[+] agumonkey|9 years ago|reply
afaik babel isn't about performance, but linguistic traits.