【问题标题】:Coffeescript is not being compiled correctly in rails with async and await syntaxCoffeescript 没有在 Rails 中使用 async 和 await 语法正确编译
【发布时间】:2020-02-26 22:20:21
【问题描述】:

我的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;

  })();

我在 ma​​cOS v10.14

上使用 rails v5.2ruby v2.6.4
$ 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

my gemlock file

为什么会发生这种情况以及如何解决。

我需要通过我的资产管道使用正确的 async/await 语法

【问题讨论】:

    标签: ruby-on-rails coffeescript ruby-on-rails-5 asset-pipeline


    【解决方案1】:

    不幸的是,coffee-rails gem 没有使用最新版本的 CoffeeScript,因此缺少一些 ES2017 功能,例如您正在寻找的 await

    coffee-rails 本身依赖于 coffee-script-source gem,你 can see 仍然是 使用 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
     */
    

    【讨论】:

    【解决方案2】:

    coffee-script-source gem 已过期。对于较新的 js 设置,建议使用 webpacker 而不是资产管道。但这可能是一次大规模的重组。


    解决方法

    有一个pull request 建议使用coffeescript 2 的解决方法:

    可以通过设置 COFFEESCRIPT_SOURCE_PATH 环境变量将此库与其他版本的 CoffeeScript 一起使用,但我们建议将 WebPacker 用于 CoffeeScript 2 及更高版本。

    export COFFEESCRIPT_SOURCE_PATH=/path/to/coffee-script/extras/coffee-script.js
    

    您需要安装并定位您的 coffee-script 2 可执行文件(如果全局使用 which coffee 找到它,如果使用 npm 在本地找到它,它将是 ./node_modules/.bin/coffee)。我不确定你的 gem 是否会包含正确的咖啡脚本版本,所以我建议使用 npm 或其他节点包管理器。


    在 Heroku 上

    如何基于这个Github comment在heroku上运行

    1. 下载standalone coffeescript compiler并将其保存到您的rails项目中(例如,在项目根目录中创建一个名为tools的新目录

    2. 为 heroku 设置环境变量,以便 coffee-script-source gem 使用正确的编译器:

      heroku 配置:设置 COFFEESCRIPT_SOURCE_PATH=/app/tools/coffeescript.js

    (通常/app是heroku上的项目目录,但你可能要改一下)

    【讨论】:

    • 它是否足够稳定,可以部署在生产环境中?如果是这样,如何让它在 heroku 上工作?
    • @NijeeshJoshy 因为它被编译成 JS,所以对生产没有影响。您可能需要查看浏览器支持,因为 coffeescript 2 被编译成 es6,因此如果您必须支持旧版浏览器,您可能需要使用 babel 或类似工具。你是在本地还是在 Heroku 上编译资产?
    • 我们在heroku上编译资产。我们不支持 ie11,所以我认为这也不是问题。
    猜你喜欢
    • 2021-04-29
    • 2017-11-23
    • 1970-01-01
    • 2018-09-28
    • 2013-08-14
    • 1970-01-01
    相关资源
    最近更新 更多