Reputation: 1481
i have a similar coffee-script like this in my assets/javascript
directory
#COFFEE SCRIPT CODE
class TestClass
speak: ()->
response = await fetch(location.url)
console.log(response)
its being compiled correctly with proper async/await
syntax in coffee-script's official playground
# COMPILED JS FROM COFFEESCRIPT OFFICIAL PLAYGROUND
var TestClass;
TestClass = class TestClass {
async speak() {
var response;
response = (await fetch(location.url));
return console.log(response);
}
};
but when i write this in a file and make it compile through assets pipeline it get compiled incorrectly
# 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;
})();
i am on rails v5.2 and ruby v2.6.4 on 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
why is this happening and how to fix it.
i need the proper async/await syntax via my assests pipeline
Upvotes: 0
Views: 1163
Reputation: 6548
The coffee-script-source
gem is out of date. It is suggested to use webpacker
instead of the asset pipeline for newer js setups. But that could be a large restructure.
There was a pull request suggesting a workaround to use 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
You will need to install & locate your coffee-script 2 executable (if globally it would be found with which coffee
, and if locally with npm
, it would be ./node_modules/.bin/coffee
). I'm not sure if your gem will include the correct coffee-script version, so I would suggest using npm or another node package manager instead.
How to run on heroku based on this Github comment
Download and save the standalone coffeescript compiler in your rails project (eg. make a new directory in the project root called tools
Set the env var for heroku so the coffee-script-source gem uses the correct compiler:
heroku config:set COFFEESCRIPT_SOURCE_PATH=/app/tools/coffeescript.js
(usually /app
is the project directory on heroku, but you may have to change this)
Upvotes: 1
Reputation: 1033
Unfortunately the coffee-rails
gem doesn't use the most up-to-date version of CoffeeScript and is thus missing some of the ES2017 features like await
that you're looking for.
coffee-rails
itself depends on the 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
*/
Upvotes: 2