ECMAScript:Harmony / ES6 到 JavaScript 编译器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6506519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ECMAScript:Harmony / ES6 to JavaScript compiler
提问by Raynos
After reading Peter's article on JavaScriptI noticed
在阅读了彼得关于 JavaScript 的文章后,我注意到
Brendan Eich stated that one the goals for Harmony is to be a better target for to-JavaScript compilers.
Brendan Eich 表示,Harmony 的目标之一是成为 JavaScript 编译器的更好目标。
There are currently two popular compilers with some vague ES:Harmonycompliance:
目前有两种流行的编译器具有一些模糊的ES:Harmony合规性:
Although CoffeeScript has some compliance it's not designed to be an ES:Harmony compiler so it's not useful to this end.
尽管 CoffeeScript 具有一定的合规性,但它并非设计为 ES:Harmony 编译器,因此它对此没有用处。
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.
Tracuer 似乎更严格地遵守 ES:Harmony 规范,但我不知道它是否打算成为完整的 ES:Harmony 编译器。
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).
由于目标是将 ES6 编译为 ES3,因此还需要支持 ES5 功能(可能还需要切换是将 ES5 编译为 ES3 还是将 ES6 编译为 ES3)。
- Are there currently any other projects aiming to create a full ES:Harmony to ES3 compiler?
- Is it wise to start writing such a compiler knowing that the standard is young / unstable / in flux.
- Are there currently any ES5 -> ES3 compilers?
- 目前是否有其他项目旨在创建完整的 ES:Harmony to ES3 编译器?
- 知道标准很年轻/不稳定/不断变化,开始编写这样的编译器是否明智。
- 当前是否有任何 ES5 -> ES3 编译器?
I've left a question on the Traceur mailing list.
我在Traceur 邮件列表上留下了一个问题。
The aim of such a compiler would be backwards compatibility with ES3. Not full emulation of ES5 and ES6 in ES3.
这种编译器的目标是向后兼容 ES3。在 ES3 中没有完全模拟 ES5 和 ES6。
采纳答案by kybernetikos
回答by Mike Samuel
(shameless but relevant plug below)
(无耻但相关的插件如下)
Cajais reworking its ES5 support via ES5/3and 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.
Caja正在通过ES5/3重新设计其 ES5 支持,并将为 ES 和谐做同样的事情。因此,我们的结构将实现为一个 Harmony to ES3 层,可以跳过它以实现真正的和谐实现,然后是一个可分离的加载器,保留与 caja 相关的安全属性。
Like Traceur, members of the Caja team are part of TC39 (the committee defining ES Harmony).
与 Traceur 一样,Caja 团队的成员也是 TC39(定义 ES Harmony 的委员会)的一部分。
I don't know about Coffeescript's plans, but it was mentioned during discussions of Harmony modules. Module loaderswill 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.
我不知道 Coffeescript 的计划,但在 Harmony 模块的讨论中提到过。 模块加载器可能有能力拦截加载的源代码(通过eval hooks)并在模块初始化之前重写它,因此如果模块是用 CoffeeScript 编写的,则可以在初始化时调用运行时 CoffeeScript 重写器。这将允许应用程序由以多种语言编写的模块组成,这些模块在加载时编译为 Harmony。
One thing to note is that not everything in Harmony can be implemented easily via translation. For example, implementing weak mapscorrectly 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.
需要注意的一件事是,并非 Harmony 中的所有内容都可以通过翻译轻松实现。例如,正确实现弱映射需要在 JavaScript 中实现你自己的垃圾收集器,即使你这样做了,你也可能只会重新引入宿主对象/本机对象循环问题。
回答by orad
Check out TypeScript, Microsoft's new language based on ES6.
查看TypeScript,这是微软基于 ES6 的新语言。
回答by starbeamrainbowlabs
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+).
截至打字时,我们现在有Babel。它与许多不同的构建工具/系统集成,并将转译 ES6+ 以支持旧浏览器(它没有说明它的目标版本,但它确实说它的目标是 IE9+)。
To install it type npm install babel -g
.
要安装它,请键入npm install babel -g
.
Note that it has rather a lot of dependencies and when installed it is ~23.4 MB (2888 files).
请注意,它有相当多的依赖项,安装后约为 23.4 MB(2888 个文件)。
回答by Avindra Goolcharan
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.
Google Closure Compiler( Github) 是一个很棒的 ES6 编译工具。这是一个从命令行使用的简单 Java jar。还有其他选项,例如 API 服务和 GUI,但我发现最好设置一个连接到 Java JAR 的自动构建系统。它可以将您的 ES6 代码转换为 ES5 兼容代码。我开始使用它来压缩和混淆代码,但它也可以进行错误检查和我提到的 ES6 转译。
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.
请注意,ES6 功能被标记为实验性的。但我计划很快在生产中使用它们,因为我的测试非常可靠。
回答by Ilia Choly
There's also https://github.com/matthewrobb/six
还有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.
Six 是 JavaScript 的语言超集,它支持通过转译器在您今天的脚本中使用 ECMAScript 第 6 版中的新语法特性。
警告:仍处于非常早期的状态,请谨慎行事。
回答by Andrew Odri
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.
我不确定在什么情况下编译回 ES3 比 ES5 有价值,因为实现更改仅限于数组和对象辅助函数,并且ES5 支持如此普遍。
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
所以为了完整性,另一个 ES6 到 ES5 编译器是 Square 的 esnext 项目。它是许多模块的集合,旨在填充一个包中提供的各种 ES6 功能。这是包含的列表模块:https: //github.com/square/esnext#available