windows 耙子资产:预编译不起作用(rails 3.1.1)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7845585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:18:40  来源:igfitidea点击:

rake assets:precompile doesn't work (rails 3.1.1)

windowsruby-on-rails-3herokurakeassets

提问by Nick Ginanto

I am deploying to heroku yet I saw that the css files aren't being served (they also cannot be found on heroku).

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

I read that I need to do rake assets:precompile locally at first yet when I do it I get:

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

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)

I have nothing in application.js so I don't understand where the error is..

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

application.js is

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 .

Thank you

谢谢

Update

更新

If removing a .js.erb file I get the following error

如果删除 .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)

Still have problems with erb css and js files not compiling...

仍然有 erb css 和 js 文件无法编译的问题...

This doesn't seem to end.. Thanks

这似乎还没有结束..谢谢

采纳答案by u445908

I think it is caused by an external javascript file which is not well-code-formatted. e.g.

我认为这是由代码格式不正确的外部 javascript 文件引起的。例如

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

when under the precompile, your code would put in 1 line, and since there's no necessary semicolon, your generated js file probably contains errors, like

在预编译下,您的代码将放在 1 行中,并且由于没有必要的分号,因此您生成的 js 文件可能包含错误,例如

"undefined: Unexpected token: operator (<)" 

or something.

或者其他的东西。

so, my suggestion is:

所以,我的建议是:

  1. don't compress the js file if it's not well code-formatted, by setting "config.assets.compress = false" in your config file, following @Mike's answer.

  2. use coffeescript as possible, it will help you generate very well formatted code. ( I am not a coffeescript guru, so please correct me if I am wrong )

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

  2. 尽可能使用coffeescript,它将帮助您生成格式良好的代码。(我不是咖啡大师,所以如果我错了,请纠正我)

回答by Mike

I've been struggling with this trying to deploy to a staging server. The solution that works for me is to make sure you have the following in your config/environments/[your_environment].rbfile:

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

config.assets.compress = false

By default, the compressors aren't available in environment other than production, which is why the precompile was failing.

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

回答by Carlosin

I have the same issue here! In my case, what causes this issue is that, I add a new JS file to javascript folder, and I got an undefined: Unexpected token: operator (<)error while I tried to run precompile command. So I look into the new js file and found there is a HTML style comment <!-- -->in that file. I remove it and life is good now!

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

So try to find out is there any HTML style comment <!-- -->in your js file and just remove those comments. This is especially true when some JS code is copied from html file!

所以试着找出<!-- -->你的 js 文件中是否有任何 HTML 样式的注释,然后删除这些注释。当一些 JS 代码从 html 文件中复制时尤其如此!

回答by rudygodoy

I was having the same issue and it turned out to be caused by the inclusion of a embed javascript which had comments in the format: <!-- comment -->I've removed them and it worked like a charm! Hopefully this helps.

我遇到了同样的问题,结果证明它是由包含格式注释的嵌入 javascript 引起的:<!-- comment -->我已经删除了它们,它就像一个魅力!希望这会有所帮助。

回答by Saurabh Nanda

I've spent the last 1 hour scratching my head after encountering the same bug. The problem is the following line in your application.js:

在遇到同样的错误后,我花了最后 1 个小时摸不着头脑。问题是 application.js 中的以下行:

//= require_tree .

This causes all files in your app/assets/javascripts/directory to get included and it couldbe that there is some sort of bug in another file in the directory. I removed that line and got my assets to precompile (I wasn't really using application.js). So, look for a bug in a file being included by application.js

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

回答by Tilo

one thing I noticed is that it should be:

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

RAILS_ENV=production bundle exec rake assets:precompile

the definition of the RAILS_ENV needs to go before the bundlecommand, because it's setting the shell (bash) environment variable for the shell that executes the bundlecommand.

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

Your problems seems to be related to this:

您的问题似乎与此有关:

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

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

See also:

也可以看看:

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

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

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

Heroku rails 3.1 应用程序 - 在本地编译资产与在 slug 编译期间编译资产

Error compiling Rails 3 CSS asset on Heroku

在 Heroku 上编译 Rails 3 CSS 资产时出错

回答by bbusche

I had a similar problem:

我有一个类似的问题:

Unexpected token: operator (<<)

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

This turned out to be a left over file from a merge conflict in Git. The conflict leaves a .orig file that contains "<<<<<<<<<<" wherever Git finds a block of code to be merged.

结果证明这是 Git 中合并冲突的遗留文件。冲突会在 Git 找到要合并的代码块的地方留下一个包含“<<<<<<<<<<<”的 .orig 文件。

Because of the asset pipeline directive

由于资产管道指令

//= require_tree .

//= require_tree .

in application.js, all files in the javascript folder (including .orig files) get precompiled on a push to servers like Heroku. The precompiler finds fault with the "<<<<<".

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

So my solution was to find all the .orig files and delete them from Git, using the 'git rm filename' method.

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