资产:使用 uglify 预编译失败
Posted
技术标签:
【中文标题】资产:使用 uglify 预编译失败【英文标题】:assets:precompile with uglify fails 【发布时间】:2015-09-28 22:21:15 【问题描述】:知道如何解决这个错误吗?
环境
服务器:EC2(OpsWorks Rails 应用) 操作系统:亚马逊 2014.09 Ruby:ruby 2.0.0p645(2015-04-13 修订版 50299)[x86_64-linux] 导轨:4.0.8 bower-rails: 0.9.2 (我也试过master分支的最新代码,但没有用) npm: 1.4.28 凉亭:1.4.1 ExecJS.runtime:我尝试过 RubyRacerRuntime 和 Node.js,但它们都不能在我的服务器上运行注意
assets:precompile
在我的本地机器 (mac) 上运行良好
我发现让asset:precompile 在我的服务器上工作的唯一方法是注释掉config.assets.js_compressor = :uglifier
。 (但我想丑化)
bower_components/jquery/src/intro.js
中的预编译总是失败(如果我手动修复 intro.js 中的语法错误,则获取 outro.js 的错误)
错误
I, [2015-07-10T06:54:41.017580 #21463] INFO -- : Writing /srv/www/app/releases/20150710064958/public/assets/jquery/src/event-53bd66b3ef4e0030febbf883c4ccab1e.js
I, [2015-07-10T06:54:41.034631 #21463] INFO -- : Writing /srv/www/app/releases/20150710064958/public/assets/jquery/src/event/ajax-2728c2aeb59d870e60923e2a75492611.js
I, [2015-07-10T06:54:41.057426 #21463] INFO -- : Writing /srv/www/app/releases/20150710064958/public/assets/jquery/src/event/alias-b8026dcda8af25a845c9aaf6b812a0c3.js
I, [2015-07-10T06:54:41.073036 #21463] INFO -- : Writing /srv/www/app/releases/20150710064958/public/assets/jquery/src/event/support-7331020932640489d80faa12a4510a96.js
I, [2015-07-10T06:54:41.093488 #21463] INFO -- : Writing /srv/www/app/releases/20150710064958/public/assets/jquery/src/exports/amd-4a5f196c0022a5f22de3dd642af82512.js
I, [2015-07-10T06:54:41.131496 #21463] INFO -- : Writing /srv/www/app/releases/20150710064958/public/assets/jquery/src/exports/global-fa942be8ff1d0bcc250cff98e5c45509.js
rake aborted!
ExecJS::ProgramError: Unexpected token: eof (undefined) (line: 46, col: 0, pos: 1394)
Error
at new JS_Parse_Error (<eval>:2357:10736)
at js_error (<eval>:2357:10955)
at croak (<eval>:2357:18667)
at token_error (<eval>:2357:18804)
at unexpected (<eval>:2357:18892)
at block_ (<eval>:2357:23918)
at ctor.body (<eval>:2357:23572)
at function_ (<eval>:2357:23637)
at expr_atom (<eval>:2357:26669)
at maybe_unary (<eval>:2357:29262)
(in /srv/www/app/releases/20150710064958/vendor/assets/bower_components/jquery/src/intro.js)/home/deploy/.bundler/app/ruby/2.0.0/gems/execjs-2.0.1/lib/execjs/ruby_racer_runtime.rb:34:in `rescue in block in eval'
/home/deploy/.bundler/app/ruby/2.0.0/gems/execjs-2.0.1/lib/execjs/ruby_racer_runtime.rb:28:in `block in eval'
/home/deploy/.bundler/app/ruby/2.0.0/gems/execjs-2.0.1/lib/execjs/ruby_racer_runtime.rb:80:in `block in lock'
/home/deploy/.bundler/app/ruby/2.0.0/gems/execjs-2.0.1/lib/execjs/ruby_racer_runtime.rb:78:in `call'
/home/deploy/.bundler/app/ruby/2.0.0/gems/execjs-2.0.1/lib/execjs/ruby_racer_runtime.rb:78:in `Locker'
/home/deploy/.bundler/app/ruby/2.0.0/gems/execjs-2.0.1/lib/execjs/ruby_racer_runtime.rb:78:in `lock'
/home/deploy/.bundler/app/ruby/2.0.0/gems/execjs-2.0.1/lib/execjs/ruby_racer_runtime.rb:27:in `eval'
/home/deploy/.bundler/app/ruby/2.0.0/gems/execjs-2.0.1/lib/execjs/ruby_racer_runtime.rb:19:in `exec'
/home/deploy/.bundler/app/ruby/2.0.0/gems/uglifier-2.2.1/lib/uglifier.rb:176:in `really_compile'
/home/deploy/.bundler/app/ruby/2.0.0/gems/uglifier-2.2.1/lib/uglifier.rb:100:in `compile'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/uglifier_compressor.rb:25:in `evaluate'
/home/deploy/.bundler/app/ruby/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/context.rb:197:in `block in evaluate'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/context.rb:194:in `each'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/context.rb:194:in `evaluate'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/bundled_asset.rb:25:in `initialize'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/base.rb:377:in `new'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/base.rb:377:in `build_asset'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/index.rb:94:in `block in build_asset'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/caching.rb:58:in `cache_asset'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/index.rb:93:in `build_asset'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/base.rb:287:in `find_asset'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/index.rb:61:in `find_asset'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:211:in `block in find_asset'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:257:in `benchmark'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:210:in `find_asset'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:119:in `block in compile'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:118:in `each'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:118:in `compile'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-rails-2.1.3/lib/sprockets/rails/task.rb:61:in `block (3 levels) in define'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-2.11.0/lib/rake/sprocketstask.rb:146:in `with_logger'
/home/deploy/.bundler/app/ruby/2.0.0/gems/sprockets-rails-2.1.3/lib/sprockets/rails/task.rb:60:in `block (2 levels) in define'
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
---- End output of bundle exec rake assets:precompile ----
Ran bundle exec rake assets:precompile returned 1
[2015-07-10T06:54:44+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
【问题讨论】:
【参考方案1】:我最终使用了asset_sync,因为assets:precompile
在我的服务器上不起作用。
【讨论】:
【参考方案2】:让我猜猜:您使用的是 Capistrano? (这就是我找到这个页面的方式)
在这种情况下(对于那些使用 Capistrano 的人):当我尝试使用默认配置时,我遇到了这个问题。每当我执行cap production deploy:assets:precompile
时,它都会抛出上面的错误。执行bundle exec rake assets:precompile
仍然可以在部署机器上运行(如果手动执行)。
我的解决方法是覆盖 Capistrano 作业:
Rake::Task["deploy:assets:precompile"].clear_actions
class PrecompileRequired < StandardError; end
namespace :deploy do
namespace :assets do
desc "Precompile assets if changed"
task :precompile do
on roles(:app) do
#invoke 'deploy:assets:precompile_changed'
within release_path do
execute <<-EOCOMMAND
(cd #release_path/vendor/assets
bundle exec rake assets:precompile)
EOCOMMAND
end
end
end
end
end
从这一刻起,它就奏效了。我的猜测是,Capistrano 默认使用不同版本的 rake 或 uglify(可能是全局安装的)。
【讨论】:
感谢您的回答。据我记得,即使我在没有 Capistrano 的情况下手动运行assets:precompile
命令时也遇到了错误。 (我一直在使用asset_sync,所以让我稍后再试一次。)以上是关于资产:使用 uglify 预编译失败的主要内容,如果未能解决你的问题,请参考以下文章