如何在ruby on rails应用程序中使用ES6 (ES2015) ?

时间:2021-08-31 14:44:52

I have a 4.2 rails app and am considering slowly converting the JS to ES6. Right now I'm using the standard rails manifest file from the asset pipeline to pre-process my js.

我有一个4.2 rails应用程序,正在考虑将JS慢慢转换为ES6。现在,我正在使用来自资产管道的标准rails清单文件来预处理我的js。

I'm not sure how to go about starting to use ES6 (adding a transpile step) in my rails app.

我不知道如何开始在我的rails应用程序中使用ES6(添加一个透明的步骤)。

Is there a built-in method, or a recommended tool or workflow?

是否有内置的方法,或推荐的工具或工作流?

1 个解决方案

#1


16  

At present unfortunately there isn't really a 'standard' way of doing this - a lot of it depends on the requirements of your app and if you are able to upgrade Sprockets and/or Rails.

不幸的是,目前还没有一个真正的“标准”方法来实现这一点——这在很大程度上取决于你的应用程序的需求,以及你是否能够升级spro佝偻病和/或Rails。

Option 1: Stay on Rails 4.2 and Sprockets 3, then use this gem to add ES6 support and gradually migrate to ES6 modules: https://github.com/rmacklin/sprockets-bumble_d

选项1:继续使用Rails 4.2和spro3,然后使用该gem添加ES6支持,并逐渐迁移到ES6模块:https://github.com/rmacklin/sprockets-bumble_d。

Option 2: Upgrade to Sprockets 4 (still in beta), then use either https://github.com/fnando/babel-schmooze-sprockets or https://github.com/babel/ruby-babel-transpiler to add babel for ES6 support. Both have pretty solid documentation.

选项2:升级到spro佝偻病4(仍在测试中),然后使用https://github.com/fnando/babel- schmooze-spro佝偻病或https://github.com/babel/ruby-babel-transpiler添加babel以支持ES6。两者都有非常可靠的文档。

Option 3: Use webpack either instead of or alongside sprockets. A google search will reveal some approaches for this. Rails 5.1 (still in beta) will introduce native webpack (and thus babel) support via the webpacker gem. Admittedly this may be the more difficult option for an existing application. There is a good article about it here: https://medium.com/statuscode/introducing-webpacker-7136d66cddfb#.cb4sixyah

选项3:使用webpack而不是spro佝偻病。谷歌搜索将揭示这方面的一些方法。Rails 5.1(仍在测试中)将通过webpacker gem引入本地webpack(因此babel)支持。诚然,对于现有的应用程序来说,这可能是一个更困难的选择。这里有一篇很好的文章:https://medium.com/statuscode/- webpacker-7136d66cddfb#.cb4sixyah

#1


16  

At present unfortunately there isn't really a 'standard' way of doing this - a lot of it depends on the requirements of your app and if you are able to upgrade Sprockets and/or Rails.

不幸的是,目前还没有一个真正的“标准”方法来实现这一点——这在很大程度上取决于你的应用程序的需求,以及你是否能够升级spro佝偻病和/或Rails。

Option 1: Stay on Rails 4.2 and Sprockets 3, then use this gem to add ES6 support and gradually migrate to ES6 modules: https://github.com/rmacklin/sprockets-bumble_d

选项1:继续使用Rails 4.2和spro3,然后使用该gem添加ES6支持,并逐渐迁移到ES6模块:https://github.com/rmacklin/sprockets-bumble_d。

Option 2: Upgrade to Sprockets 4 (still in beta), then use either https://github.com/fnando/babel-schmooze-sprockets or https://github.com/babel/ruby-babel-transpiler to add babel for ES6 support. Both have pretty solid documentation.

选项2:升级到spro佝偻病4(仍在测试中),然后使用https://github.com/fnando/babel- schmooze-spro佝偻病或https://github.com/babel/ruby-babel-transpiler添加babel以支持ES6。两者都有非常可靠的文档。

Option 3: Use webpack either instead of or alongside sprockets. A google search will reveal some approaches for this. Rails 5.1 (still in beta) will introduce native webpack (and thus babel) support via the webpacker gem. Admittedly this may be the more difficult option for an existing application. There is a good article about it here: https://medium.com/statuscode/introducing-webpacker-7136d66cddfb#.cb4sixyah

选项3:使用webpack而不是spro佝偻病。谷歌搜索将揭示这方面的一些方法。Rails 5.1(仍在测试中)将通过webpacker gem引入本地webpack(因此babel)支持。诚然,对于现有的应用程序来说,这可能是一个更困难的选择。这里有一篇很好的文章:https://medium.com/statuscode/- webpacker-7136d66cddfb#.cb4sixyah