ECMAScript:Harmony / ES6 to JavaScript compiler

守給你的承諾、 提交于 2019-11-27 09:19:57

问题


After reading Peter's article on JavaScript I noticed

Brendan Eich stated that one the goals for Harmony is to be a better target for to-JavaScript compilers.

There are currently two popular compilers with some vague ES:Harmony compliance:

  • Traceur
  • CoffeeScript

Although CoffeeScript has some compliance it's not designed to be an ES:Harmony compiler so it's not useful to this end.

Tracuer seems to be sticking more rigorously to the ES:Harmony specification but I don't know whether it intends to become a full ES:Harmony compiler.

Since the aim is to to compile ES6 down to ES3 it would also need to support ES5 features (and probably a switch whether to compile ES5 to ES3 or ES6 to ES3).

  1. Are there currently any other projects aiming to create a full ES:Harmony to ES3 compiler?
  2. Is it wise to start writing such a compiler knowing that the standard is young / unstable / in flux.
  3. Are there currently any ES5 -> ES3 compilers?

I've left a question on the Traceur mailing list.

The aim of such a compiler would be backwards compatibility with ES3. Not full emulation of ES5 and ES6 in ES3.


回答1:


Continuum has implemented most of the relevant features and should run in es3 browsers (like older IEs).




回答2:


(shameless but relevant plug below)

Caja is reworking its ES5 support via ES5/3 and will do the same for ES harmony. So our structure would be implemented as a Harmony to ES3 layer which can be skipped for real harmony implementations, and then a separable loader that preserves the security properties that concern caja.

Like Traceur, members of the Caja team are part of TC39 (the committee defining ES Harmony).

I don't know about Coffeescript's plans, but it was mentioned during discussions of Harmony modules. Module loaders will likely have the ability to intercept loaded source code (via eval hooks) and rewrite it before module initialization, so if a module is written in CoffeeScript, a runtime CoffeeScript rewriter could be invoked at initialization time. This would allow apps to be composed of modules written in multiple languages that compile down to Harmony at load time.

One thing to note is that not everything in Harmony can be implemented easily via translation. For example, implementing weak maps correctly would require implementing your own garbage collector in JavaScript and even if you did that you would probably just reintroduce the host object/native object cycle problem.




回答3:


Check out TypeScript, Microsoft's new language based on ES6.




回答4:


Mascara is probably what you're looking for.




回答5:


As of the time of typing, we now have Babel. It integrates with many different build tools/systems, and will transpile ES6+ in order to support legacy browsers (it doesn't state which version it targets, but it does say that it targets IE9+).

To install it type npm install babel -g.

Note that it has rather a lot of dependencies and when installed it is ~23.4 MB (2888 files).




回答6:


Google Closure Compiler (Github) is a great tool for ES6 compilation. It's a simple Java jar that is used from the command line. There are other options such as API services and GUIs, but I find that it was best to set up an automatic build system hooking into the Java JAR. It can transpile your ES6 code into ES5 compatible code. I started using it for compressing and obfuscating code, but it can also do error checking and the ES6 transpilation as I mentioned.

Note that the ES6 features are marked as experimental. But I'm planning on using them in production soon, since my tests were rock solid.




回答7:


There's also https://github.com/matthewrobb/six

Six is a language super-set of JavaScript that enables new syntactic features from the 6th edition of ECMAScript to be used, through a transpiler, in your scripts today.

WARNING: Still in a very early state, proceed with caution.




回答8:


I'm not sure in what instance compilation back to ES3 would valuable as opposed to ES5, seeing that implementation changes are limited to array and object helper functions, and ES5 support is so prevalent.

So for completeness, another ES6 to ES5 compiler is the esnext project by Square. It is a collection of a number of modules designed to polyfill various ES6 features provided in one package. Here is the list modules included: https://github.com/square/esnext#available



来源:https://stackoverflow.com/questions/6506519/ecmascriptharmony-es6-to-javascript-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!