The Javacript Compiler I Want

There are a number of compilers that in something and output Javascript. A few of the more well known ones seem to be CoffeeScript, Dart, and emscripten.

What I really want though is a Javascript to Javascript compiler that will run in Javascript. Here is the practical application. Top in mind is that I want to write code for ECMAScript 5 or even use Harmony features, and have it compile to output that will run on IE7 (which only supports ECMAScript 3). A few features that come to mind are generators (the yield keyword), array comprehensions, getters and setters, and block scoping. A lot of this stuff has been around since Firefox 2! I think that getters/setters have been around even longer.

Once we have an AST, I can picture how to implement some features (getters/settings, maybe block scoping).

Two pieces that I think could be useful, if someone were to implement this are esprima and escodegen . When looking at esprima, be sure to check out the harmony branch.

Another related project is Narcissus ( Narcissus is a meta-cirular evaluator rather than a compiler, which I take to mean that there will be a performance hit likely to make it unacceptable for use with IE 7 & 8. However, performance on IE isn’t relevant anyway since this implementation relies on features only found in recent SpiderMonkey engines (and thus also won’t run in Chrome be default).

While on the topic, I should probably mention re-using some of the shims and polyfills out there (such as harmony-collections), as well as Traceur. Traceur is ECMAScript 6 input and ECMAScript 5 output.

One question I have is should the compiled output include polyfills, which can effect other libraries you use and would be unsafe in facebook apps, or should it all hide behind a function like Facebook’s ES5 library.  As you can see in Facebook’s blog post, the ES5 wrapped code is less pleasant to read and they mention a performance hit.  I suppose for extra work, such a compiler could support both.

Leave a Reply