rake assets:precompile无法正常工作(Rails 3.1.1)

7

我正在部署到Heroku,但是我发现CSS文件没有被加载(在Heroku上也找不到)。

我看到需要先在本地运行rake assets:precompile,但是当我运行它时,出现以下错误:

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 尝试读取整个目录中的文件... - mliebelt
如果Heroku发现您尚未预编译资产,则会为您预编译它们 - http://devcenter.heroku.com/articles/rails31_heroku_cedar#the_asset_pipeline - John Beynon
是的 - 我有一个 global.js.erb 文件,其中包含对 <%= asset_path 'pic.jpg' %> 的调用,该文件位于 javascripts 目录中。 - Nick Ginanto
1
好的,不知道出了什么问题...我从gem中删除了execjs、uglifer和coffeescript,执行了bundle install并重新添加它们,然后从assets中删除了erb文件(js和css),rake precompile就可以工作了。那么我如何在css和erb中使用erb呢? - Nick Ginanto
7个回答

5

我一直在努力尝试将其部署到一个暂存服务器。对我有效的解决方案是确保您的config/environments/[your_environment].rb文件中包含以下内容:

config.assets.compress = false

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


5

我这里也有同样的问题!在我的情况下,导致出现这个问题的原因是我向javascript文件夹中添加了一个新的JS文件,在尝试运行“precompile”命令时,我遇到了一个“undefined:Unexpected token:operator(<)” 的错误。所以我查看了新的js文件,并发现其中有一个HTML风格的注释<!-- -->。我将其删除后问题就解决了!

所以请尝试查找你的js文件中是否存在任何HTML风格的注释<!-- -->,并将这些注释删除。特别是当某些JS代码是从html文件中复制过来时。


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文件没有经过很好的代码格式化,请在你的配置文件中设置"config.assets.compress = false",按照@Mike的答案。不要压缩js文件。

  2. 尽可能地使用coffeescript,它将帮助您生成非常良好格式化的代码。(我不是coffeescript专家,如果我有误请指正)


3

我曾经遇到同样的问题,原来是由于嵌入了一个带有注释格式的嵌入式JavaScript代码造成的:<!-- comment -->。我删除了这些注释后,问题得到解决!希望能对你有所帮助。


1
我已经花了最近1个小时来解决同一个bug。问题出在你的application.js文件中的以下代码行:
//= require_tree .

这会导致您 app/assets/javascripts/ 目录中的所有文件都被包含在内,可能是由于该目录中的另一个文件存在某种错误。我删除了那行代码,并成功预编译了我的资产(实际上我并没有真正使用application.js)。所以,请查找被 application.js 引入的文件中是否存在错误。


1

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

RAILS_ENV=production bundle exec rake assets:precompile

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

你的问题似乎与此有关:

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

另请参阅:

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

Heroku Rails 3.1 应用程序 - 本地编译资源与在 slug 编译期间编译资源的比较

在 Heroku 上编译 Rails 3 CSS 资源时出现错误


0

我曾经遇到过类似的问题:

意外的标记:运算符(<<)

这个问题最终被证明是Git合并冲突留下的文件。冲突会在Git找到需要合并的代码块时,生成一个包含"<<<<<<<<<<"的.orig文件。

由于在application.js中有资产管道指令

//= require_tree .

所有javascript文件夹中的文件(包括.orig文件)都会在推送到像Heroku这样的服务器上进行预编译。预编译器会因为"<<<<<<<<<<"而出现错误。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接