捆绑执行rake资产:预编译失败与“意外令牌” [英] bundle exec rake assets:precompile fails with `unexpected token`

查看:182
本文介绍了捆绑执行rake资产:预编译失败与“意外令牌”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我准备好将Rails 3.1应用程序部署到生产中,而且由于我使用了资产管道,我需要预编译我的资产。但是,当我尝试这个,我收到一个错误显然与编译jQuery相关:

I'm ready to deploy my Rails 3.1 app into production, and since I'm using the asset pipeline, I need to precompile my assets. However, when I try this, I get an error apparently related to compiling jQuery:

$ bundle exec rake --trace assets:precompile
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/home/adam/.rvm/rubies/ruby-1.9.3-p0/bin/ruby /home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
rake aborted!
399: unexpected token at '"/*!\u000a * jQuery JavaScript Library v1.7.1\u000a * http://jquery.com/\u000a *\u000a * Copyright 2011, John Resig\u000a * Dual licensed under the MIT or GPL Version 2 licenses.\u000a * http://jquery.org/license\u000a *\u000a * Includes Sizzle.js\u000a * http://sizzlejs.com/\u000a * Copyright 2011, The Dojo Foundation\u000a * Released under the MIT, BSD, and GPL Licenses.\u000a *\u000a * Date: Mon Nov 21 21:11:03 2011 -0500\u000a */\u000afunction addActiveScaffoldPageToHistory(a,b){if(typeof

[snip很多东西]

[snip lots of stuff]

  (in /data/music/RotC/eventbook/app/assets/javascripts/application.js)
/home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/gems/json-1.6.4/lib/json/common.rb:148:in `parse'
/home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/gems/json-1.6.4/lib/json/common.rb:148:in `parse'
/home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/gems/multi_json-1.0.4/lib/multi_json/engines/json_common.rb:9:in `decode'
/home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/gems/multi_json-1.0.4/lib/multi_json.rb:76:in `decode'
/home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/gems/execjs-1.2.13/lib/execjs/external_runtime.rb:61:in `extract_result'
/home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/gems/execjs-1.2.13/lib/execjs/external_runtime.rb:27:in `block in exec'
/home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/gems/execjs-1.2.13/lib/execjs/external_runtime.rb:40:in `compile_to_tempfile'
/home/adam/.rvm/gems/ruby-1.9.3-p0@rails-3.1/gems/execjs-1.2.13/lib/execjs/external_runtime.rb:26:in `exec'

[snip more more stuff]

[snip lots more stuff]

这是我的 application.js

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//
// N.B. jQuery requires have to come before this:
//= require active_scaffold

我正在使用默认压缩器,即 uglifier

I'm using the default compressor, i.e. uglifier.

我发现如果我更改 config .assets.compress false config / environments / production.rb 然后它工作好的,但当然我的应用程序会表现得更好,如果我可以找出一种方法来保持为 true

I've found that if I change config.assets.compress to false in config/environments/production.rb then it works fine, but of course my app would perform better if I could figure out a way to keep it as true.

我看过耙子资产:预编译不起作用(rails 3.1.1),我不认为这是重复的,因为错误是 undefined:Unexpected token:operator(<)

I've looked at rake assets:precompile doesn't work (rails 3.1.1) and I don't think it's a duplicate because the error is undefined: Unexpected token: operator (<).

任何想法?我对jQuery代码中的unicode字符含糊不清,但我不知道如何证明或反驳他们导致问题。

Any ideas? I'm vaguely suspicious of those unicode characters in the jQuery code, but I'm not sure how to prove or disprove that they are causing the problem.

推荐答案

我有这个确切的相同的问题,并解决了,这里有详细信息:

I had this exact same problem and solved it, here are the details:

当您包含一个或多个您的Rails 3.1环境中的javascript文件使用application.js和 // = require ... ,Rails将包含一个< ;脚本> ..filecontents ..< / script> 。您可以通过在开发模式下运行您的站点并执行查看来验证这一点来源。

When you include one or more javascript files in your Rails 3.1 environment using application.js and //= require ..., Rails will wrap the contents of your file with a <script> ..filecontents.. </script>. You can verify this by running your site in development mode and doing a View | Source.

问题在于,在开发模式下,如果您不是实际使用该脚本,那么可能会出现工作正常但是,基本上,你所拥有的是< script>< script> ..filecontents ..< / script>< / script>

The problem is, in development mode, if you are not actually using that script, it may appear to work fine. But, Essentially, what you have is <script><script> ..filecontents.. </script></script>.

但是,当您尝试PRECOMPILE资产时,那个编译过程(对不起 - 我不知道究竟是什么)在<在内部脚本令牌中。

However, when you attempt to PRECOMPILE the assets, something in that compiling process (sorry - I do not know what exactly) gags on the "<" in the inner script token.

仔细检查所包含的.js文件,如果它们中的任何一个被< script> ...< / script> 删除周围的标签。

Double-check your included .js files and, if any of them are wrapped by <script> ... </script> remove those surrounding tags.

您应该看到,在开发中,如果您查看页面来源。而且,当您预先编译资产时,错误应该会消失。

You should see that, in Development, everything still looks fine if you View Page Source. And, when you precompile your assets, the error should go away.

我遇到了Google推荐的Google Analytics(分析)JavaScript代码段的这个问题,并从包含的内容中删除了脚本包装文件解决了这个问题。

I ran into this problem with Google's recommended google-analytics javascript snippet and removing the script wrapper from the included file solved the problem.

这篇关于捆绑执行rake资产:预编译失败与“意外令牌”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆