Coffeescript 未在 rails 中使用 async 和 await 语法正确编译
Coffeescript is not being compiled correctly in rails with async and await syntax
我的 assets/javascript
目录中有类似这样的咖啡脚本
#COFFEE SCRIPT CODE
class TestClass
speak: ()->
response = await fetch(location.url)
console.log(response)
它在 coffee-script's official playground
中使用正确的 async/await
语法正确编译
# COMPILED JS FROM COFFEESCRIPT OFFICIAL PLAYGROUND
var TestClass;
TestClass = class TestClass {
async speak() {
var response;
response = (await fetch(location.url));
return console.log(response);
}
};
但是当我将其写入文件并通过资产管道对其进行编译时,它编译错误
# COMPILED JS FROM COFFEESCRIPT ASSESTS PIPELINE
TestClass = (function() {
function TestClass() {}
TestClass.prototype.speak = function() {
var response;
response = await(fetch(location.url));
return console.log(response);
};
return TestClass;
})();
我在 rails v5.2 和 ruby v2.6.4 在 macOS v10.14
$ bundle info coffee-script
* coffee-script (2.4.1)
Summary: Ruby CoffeeScript Compiler
Homepage: http://github.com/josh/ruby-coffee-script
Path: /Users/<username>/.rvm/gems/ruby-2.6.4/gems/coffee-script-2.4.1
为什么会发生这种情况以及如何解决它。
我需要通过我的资产管道使用正确的 async/await 语法
不幸的是,coffee-rails
gem 没有使用最新版本的 CoffeeScript,因此缺少一些 ES2017 功能,例如 await
寻找。
coffee-rails
本身取决于 coffee-script-source
gem which you can see is still using CoffeeScript v1.12.6:
# https://github.com/jessedoyle/coffee-script-source/blob/master/src/js/coffee-script.js
/**
* CoffeeScript Compiler v1.12.6
* http://coffeescript.org
*
* Copyright 2011, Jeremy Ashkenas
* Released under the MIT License
*/
coffee-script-source
gem 已过时。对于较新的 js 设置,建议使用 webpacker
而不是资产管道。但这可能是一次大规模的重组。
解决方法
有 pull request 建议使用 coffeescript 2 的解决方法:
you can use this library with other versions of CoffeeScript by setting the COFFEESCRIPT_SOURCE_PATH
environment variable, but we recommend using WebPacker for CoffeeScript 2 and up.
export COFFEESCRIPT_SOURCE_PATH=/path/to/coffee-script/extras/coffee-script.js
您将需要安装并找到您的 coffee-script 2 可执行文件(如果在全球范围内可以通过 which coffee
找到它,如果在本地通过 npm
找到它,它将是 ./node_modules/.bin/coffee
).我不确定您的 gem 是否包含正确的 coffee-script 版本,所以我建议改用 npm 或其他节点包管理器。
在 Heroku 上
How to 运行 on heroku based on this Github comment
下载 standalone coffeescript compiler 并将其保存在您的 rails 项目中(例如,在项目根目录中创建一个名为 tools
的新目录
为 heroku 设置环境变量,使 coffee-script-source gem 使用正确的编译器:
heroku config:set COFFEESCRIPT_SOURCE_PATH=/app/tools/coffeescript.js
(通常/app
是heroku上的项目目录,但你可能需要改变这个)
我的 assets/javascript
目录中有类似这样的咖啡脚本
#COFFEE SCRIPT CODE
class TestClass
speak: ()->
response = await fetch(location.url)
console.log(response)
它在 coffee-script's official playground
中使用正确的async/await
语法正确编译
# COMPILED JS FROM COFFEESCRIPT OFFICIAL PLAYGROUND
var TestClass;
TestClass = class TestClass {
async speak() {
var response;
response = (await fetch(location.url));
return console.log(response);
}
};
但是当我将其写入文件并通过资产管道对其进行编译时,它编译错误
# COMPILED JS FROM COFFEESCRIPT ASSESTS PIPELINE
TestClass = (function() {
function TestClass() {}
TestClass.prototype.speak = function() {
var response;
response = await(fetch(location.url));
return console.log(response);
};
return TestClass;
})();
我在 rails v5.2 和 ruby v2.6.4 在 macOS v10.14
$ bundle info coffee-script
* coffee-script (2.4.1)
Summary: Ruby CoffeeScript Compiler
Homepage: http://github.com/josh/ruby-coffee-script
Path: /Users/<username>/.rvm/gems/ruby-2.6.4/gems/coffee-script-2.4.1
为什么会发生这种情况以及如何解决它。
我需要通过我的资产管道使用正确的 async/await 语法
不幸的是,coffee-rails
gem 没有使用最新版本的 CoffeeScript,因此缺少一些 ES2017 功能,例如 await
寻找。
coffee-rails
本身取决于 coffee-script-source
gem which you can see is still using CoffeeScript v1.12.6:
# https://github.com/jessedoyle/coffee-script-source/blob/master/src/js/coffee-script.js
/**
* CoffeeScript Compiler v1.12.6
* http://coffeescript.org
*
* Copyright 2011, Jeremy Ashkenas
* Released under the MIT License
*/
coffee-script-source
gem 已过时。对于较新的 js 设置,建议使用 webpacker
而不是资产管道。但这可能是一次大规模的重组。
解决方法
有 pull request 建议使用 coffeescript 2 的解决方法:
you can use this library with other versions of CoffeeScript by setting the
COFFEESCRIPT_SOURCE_PATH
environment variable, but we recommend using WebPacker for CoffeeScript 2 and up.export COFFEESCRIPT_SOURCE_PATH=/path/to/coffee-script/extras/coffee-script.js
您将需要安装并找到您的 coffee-script 2 可执行文件(如果在全球范围内可以通过 which coffee
找到它,如果在本地通过 npm
找到它,它将是 ./node_modules/.bin/coffee
).我不确定您的 gem 是否包含正确的 coffee-script 版本,所以我建议改用 npm 或其他节点包管理器。
在 Heroku 上
How to 运行 on heroku based on this Github comment
下载 standalone coffeescript compiler 并将其保存在您的 rails 项目中(例如,在项目根目录中创建一个名为
的新目录tools
为 heroku 设置环境变量,使 coffee-script-source gem 使用正确的编译器:
heroku config:set COFFEESCRIPT_SOURCE_PATH=/app/tools/coffeescript.js
(通常/app
是heroku上的项目目录,但你可能需要改变这个)