10

我準備將我的Rails 3.1應用程序部署到生產環境中,並且因爲我正在使用資產管道I need to precompile my assets。然而,當我嘗試,我得到明顯涉及到編制的jQuery的錯誤:bundle exec rake assets:預編譯失敗,出現'unexpected token'

$ 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/[email protected]/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 

[剪斷很多東西]

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

[剪斷其它更多的東西]

這裏是我的application.js

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

我使用的是默認壓縮器,即uglifier

我發現,如果我改變config.assets.compressconfig/environments/production.rbfalse然後正常工作,但當然,我的應用程序將有更好的表現,如果我能想出辦法來保持它作爲true

我看過rake assets:precompile doesn't work (rails 3.1.1),我不認爲它是重複的,因爲錯誤是undefined: Unexpected token: operator (<)

任何想法?我隱約懷疑jQuery代碼中的unicode字符,但我不知道如何證明或反駁他們導致問題。

+0

哪個js壓縮機是你用的?你的application.js中有什麼? – 2012-01-13 16:52:03

+0

問題現在更新了這些。 – 2012-01-13 18:43:20

回答

20

我有這個確切同樣的問題,解決它,下面是詳細信息:

當您在你的Rails的一個或多個JavaScript文件使用的application.js和//= require ... 3.1環境,Rails會包裹內容您的文件的一個<script> ..filecontents.. </script>。您可以通過在開發模式下運行您的站點並執行View |來驗證這一點資源。

的問題是,在開發模式下,如果你真的使用這個腳本是,它可能出現很好地工作。但是,基本上,你有什麼是<script><script> ..filecontents.. </script></script>

但是,當您嘗試將PRECOMPILE資產,該編譯過程中的某些內容(抱歉 - 我不知道究竟是什麼)在內部腳本標記中的「<」上惹上了一陣騷動。

仔細檢查您包含的.js文件,如果它們中的任何一個被<script> ... </script>包裹,請刪除那些周圍的標記。

你應該看到,在開發中,如果你查看頁面源代碼,一切看起來都很好。而且,當您預先編譯資產時,錯誤應該消失。

我遇到了這個問題與谷歌推薦的谷歌分析JavaScript代碼段並從包含的文件中刪除腳本包裝解決了問題。

+0

Dave非常感謝您抽出寶貴時間彙報這一點。實際上,在問這個問題之後的某個時候,這個問題對我來說已經神奇地固定下來了 - 也許是因爲我將Rails升級到了3.2或者其中一個寶石?不過,我接受你的答案,因爲對於仍然看到問題的人來說,這聽起來像是一個合理的解決方案。 – 2012-03-17 10:01:23

+0

和9個月後....它修復了我的問題! – Ryan 2012-12-10 23:38:18

+0

謝謝!我在rails 3.2中遇到過這個問題。超級有用! – EricC 2014-06-25 12:51:16

0

我有同樣的問題。

雖然我的JavaScript文件中沒有<script>標籤,但Dave的建議對我有很大的幫助!

我能夠發現在外部LIB評論我是裝在我的application.js

<!-- Hotjar Tracking Code for (...) ->

這一條意見是打破我的uglifier壓縮和我花了很長時間才找到它,甚至可能認爲這是罪魁禍首。

因此,也請注意在JS文件開頭的註釋,可能會打破你的壓縮。

+0

如果你想在你的JavaScript中放置註釋,使用'/ * * /''''''''' – meagar 2016-03-02 22:02:30

+0

感謝您的提示,它來自'hotjar'嵌入式代碼,一些開發人員在添加hotjar時複製。 – lucasarruda 2016-03-07 17:49:47

5

下面是我如何調試這個。

本地,運行此

RAILS_ENV=production bundle exec rake assets:precompile

嘗試找出它扼流圈(腳本就在接近窒息的是你要看看文件)。看看所有的要求。

在我的情況下,我有一個文件,在.js時它應該是.jsx,這就是修復它的原因。

相關問題