rake 资产:预编译不起作用(rails 3.1.1)

发布于 2024-12-10 20:39:51 字数 1622 浏览 0 评论 0原文

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

我读到我需要先在本地进行 rake asset: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 文件未编译仍然存在问题...

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

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

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

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 is

// 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

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...

This doesn't seem to end..
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

淡墨 2024-12-17 20:39:51

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

config.assets.compress = false

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

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].rb file:

config.assets.compress = false

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

心如狂蝶 2024-12-17 20:39:51

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

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

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!

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!

巴黎盛开的樱花 2024-12-17 20:39:51

我认为这是由外部 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文件,按照@Mike的答案在配置文件中设置“config.assets.compress = false”。

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

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

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

"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 )

泅人 2024-12-17 20:39:51

我遇到了同样的问题,结果是由于包含嵌入的 javascript 引起的,该 javascript 的注释格式如下: 我已删除它们并它就像一个魅力!希望这有帮助。

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.

孤檠 2024-12-17 20:39:51

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

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 应用 - 本地编译资产与在 slug 编译期间编译资产

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

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 bundle command, because it's setting the shell (bash) environment variable for the shell that executes the bundle command.

Your problems seems to be related to this:

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

See also:

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

新一帅帅 2024-12-17 20:39:51

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

//= require_tree .

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

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:

//= require_tree .

This causes all files in your app/assets/javascripts/ directory to get included and it could be 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

眼眸里的那抹悲凉 2024-12-17 20:39:51

我有一个类似的问题:

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

这原来是 Git 中合并冲突留下的文件。冲突留下包含“<<<<<<<<<<<<<”的 .orig 文件只要 Git 找到要合并的代码块。

由于资产管道指令

//= require_tree 。

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

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

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.

Because of the asset pipeline directive

//= 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 "<<<<<".

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文