【问题标题】:rake assets:precompile doesn't work (rails 3.1.1)rake assets:预编译不起作用(rails 3.1.1)
【发布时间】:2011-10-21 06:08:34
【问题描述】:

我正在部署到 heroku,但我看到 css 文件没有被提供(它们也无法在 heroku 上找到)。

我读到我需要做 rake assets:首先在本地预编译,但当我这样做时,我得到:

C:\project>bundle exec rake assets:precompile --trace

** Invoke assets:precompile (first_time)
** Execute assets:precompile
rake aborted!
undefined: Unexpected token: operator (<)
  (in C:/project/app/assets/javascripts/application.js)

Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

我在 application.js 中什么都没有,所以我不明白错误在哪里..

application.js 是

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

谢谢

更新

如果删除 .js.erb 文件,我会收到以下错误

C:\project>bundle exec rake assets:precompile RAILS_ENV=production --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
rake aborted!
706: unexpected token at 'C:\Users\me\AppData\Local\Temp\execjs20111021-6448-ei2nm3.js(2, 3) Microsoft JScript runtime error: Out of memory

'
  (in C:/project/app/assets/javascripts/application.js)

Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

erb css和js文件无法编译还是有问题...

这似乎没有结束.. 谢谢

【问题讨论】:

  • 您的javascripts 目录中是否有任何javascript 文件?语句//= require_tree 尝试读取...中的整个目录
  • Heroku 会在发现您尚未完成时为您预编译资产 - devcenter.heroku.com/articles/…
  • 是的 - 我有一个 global.js.erb,里面有一个对 的调用,并且该文件位于 javascripts 目录中
  • 好的不知道出了什么问题..从gem中删除了execjs、uglifer和coffeescript,做了捆绑安装并再次添加了它们,然后从资产中删除了erb文件(js和css)并rake预编译有效.. 那么如何在 css 和 erb 中使用 erb 呢?

标签: windows ruby-on-rails-3 heroku rake assets


【解决方案1】:

我一直在努力尝试部署到临时服务器。对我有用的解决方案是确保您的 config/environments/[your_environment].rb 文件中有以下内容:

config.assets.compress = false

默认情况下,压缩器在生产环境以外的环境中不可用,这就是预编译失败的原因。

【讨论】:

    【解决方案2】:

    我也有同样的问题!就我而言,导致此问题的原因是,我在 javascript 文件夹中添加了一个新的 JS 文件,并且在尝试运行预编译命令时出现undefined: Unexpected token: operator (&lt;) 错误。所以我查看了新的 js 文件,发现该文件中有一个 HTML 样式注释&lt;!-- --&gt;。我删除它,现在生活很好!

    所以试着找出你的 js 文件中是否有任何 HTML 样式注释 &lt;!-- --&gt; 并删除那些 cmets。当从 html 文件中复制一些 JS 代码时尤其如此!

    【讨论】:

      【解决方案3】:

      我认为这是由没有良好代码格式的外部 javascript 文件引起的。例如

      function say_hi(){
        // NOTICE that there's no semi-colon there!
        name = "Jim"
        alert("hi" + name )
      }
      

      在预编译下,你的代码会放在1行,由于没有必要的分号,你生成的js文件可能包含错误,比如

      "undefined: Unexpected token: operator (<)" 
      

      什么的。

      所以,我的建议是:

      1. 如果 js 文件的代码格式不正确,请不要压缩 js 文件,方法是按照 @Mike 的回答在配置文件中设置“config.assets.compress = false”。

      2. 尽可能使用coffeescript,它将帮助您生成格式良好的代码。 (本人不是coffeescript大师,如有错误请指正)

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,结果证明是由于包含了一个嵌入的 javascript,它的 cmets 格式为:&lt;!-- comment --&gt; 我已经删除了它们,它就像一个魅力!希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          我注意到的一件事是它应该是:

          RAILS_ENV=production bundle exec rake assets:precompile
          

          RAILS_ENV 的定义需要放在bundle 命令之前,因为它为执行bundle 命令的shell 设置了shell (bash) 环境变量。

          您的问题似乎与此有关:

          https://github.com/bradphelan/jasminerice/issues/21

          另见:

          http://guides.rubyonrails.org/asset_pipeline.html

          Heroku rails 3.1 app - compiling assets locally vs compiling assets during slug compilation

          Error compiling Rails 3 CSS asset on Heroku

          【讨论】:

            【解决方案6】:

            在遇到同样的错误后,我在过去 1 小时里摸不着头脑。问题是您的 application.js 中的以下行:

            //= require_tree .
            

            这会导致您的app/assets/javascripts/ 目录中的所有文件都被包含在内,并且可能目录中的另一个文件中存在某种错误。我删除了该行并让我的资产进行预编译(我并没有真正使用 application.js)。因此,在 application.js 包含的文件中查找错误

            【讨论】:

              【解决方案7】:

              我也有类似的问题:

              意外令牌:运算符 (

              这原来是 Git 中合并冲突的遗留文件。在 Git 找到要合并的代码块的任何地方,冲突都会留下一个包含“

              因为资产管道指令

              //= 要求树。

              application.js 中,javascript 文件夹中的所有文件(包括 .orig 文件)都会在推送到 Heroku 等服务器时进行预编译。预编译器发现“

              所以我的解决方案是使用“git rm 文件名”方法找到所有 .orig 文件并从 Git 中删除它们。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-03-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多