为什么我的Rails资产会被预编译两次?

时间:2013-03-13 19:10:33

标签: ruby-on-rails asset-pipeline activeadmin

我注意到我的资产似乎被编译了两次,这大大减慢了我的部署速度,因为这一步是最耗时的部分:

~/projects/rewportal(mapwidget ✔) rake assets:precompile
/home/ruy/.rvm/rubies/ruby-1.9.3-p194/bin/ruby /home/ruy/.rvm/gems/ruby-1.9.3-p194@rewportal/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
AssetSync: Syncing.
Using: Directory Search of /home/ruy/projects/rewportal/public/assets
Uploading: assets/application-5170f52c1dd49cb382d5135bee01d75e.js
[...]
Fetching files to flag for delete
Flagging 8 file(s) for deletion
Deleting: assets/active_admin-4ce46d089d4b0080e87c9abcb6fa6c97.css
[...]
AssetSync: Done.

这是正常的吗?

当我预编译到其他环境(非生产)时,我可以看到每个资产的详细编译两次:

~/projects/rewportal(mapwidget ✔) rake RAILS_ENV=qa assets:precompile --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/home/ruy/.rvm/rubies/ruby-1.9.3-p194/bin/ruby /home/ruy/.rvm/gems/ruby-1.9.3-p194@rewportal/bin/rake assets:precompile:all RAILS_ENV=qa 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
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
Compiled gmaps4rails/gmaps4rails.base.js  (141ms)  (pid 8480)
Compiled gmaps4rails/gmaps4rails.googlemaps.js  (148ms)  (pid 8480)
[...]
Compiled active_admin.css  (1299ms)  (pid 8480)
Compiled active_admin/print.css  (113ms)  (pid 8480)
** Invoke assets:precompile:nondigest (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:nondigest
Compiled gmaps4rails/gmaps4rails.base.js  (133ms)  (pid 8480)
Compiled gmaps4rails/gmaps4rails.googlemaps.js  (133ms)  (pid 8480)
[...]
Compiled active_admin.css  (1290ms)  (pid 8480)
Compiled active_admin/print.css  (116ms)  (pid 8480)
AssetSync: Syncing.
Using: Directory Search of /home/ruy/projects/rewportal/public/assets
Uploading: assets/active_admin-d05b61ab8366b74eabc9074d3e60fe82.css.gz
[...]
Fetching files to flag for delete
Flagging 6 file(s) for deletion
Deleting: assets/active_admin-ec90e7d9a9f45f14d1387f58fa1452b4.css
[...]
AssetSync: Done.

我的application.rb有以下内容:

config.assets.precompile += %w( active_admin/print.css active_admin.css active_admin.js admin.js admin.css html5shiv.js )

想法?

4 个答案:

答案 0 :(得分:12)

默认情况下,Rails 3编译一次以生成指纹资产,一次生成非指纹资产(指纹识别资产在文件名中包含MD5哈希值)。

您可以使用turbo-sprockets-rails3 gem从一个编辑中创建两个。

在Rails 4中,此功能被提取到sprockets-rails gem并且行为已更改,因此在Rails 4中不会发生双重编译。

答案 1 :(得分:1)

你在使用capistrano吗?如果是这样,您可以尝试在本地编译资产,然后使用像这样的任务上传到服务器

namespace :deploy do
  namespace :assets do
    desc "Precompile assets on local machine and upload them to the server."
    task :precompile, roles: :web, except: {no_release: true} do
      run_locally "bundle exec rake assets:precompile"
      find_servers_for_task(current_task).each do |server|
        run_locally "rsync -vr --exclude='.DS_Store' public/assets #{user}@#{server.host}:#{shared_path}/"
      end
    end
  end
end

答案 2 :(得分:0)

我在上传之前覆盖deploy.rb中的deploy_all任务进行编译:

task :remake_all do
  puts "precompiling assets"
  res = `env rake assets:precompile:all RAILS_ENV=precompile RAILS_GROUPS=assets 2>&1`

  $stderr.puts "assets res is: #{res}"
  if res =~ /aborted|don't|invalid|segmentation|bug/i
    puts "############  Unable to compile assets  #########" 
    exit
  end
end

我遇到的问题是开发环境没有正确编译,生产环境需要访问防火墙后面的mysql服务器,所以创建了一个名为:precompile

的新环境

答案 3 :(得分:0)

我的解决方案(Rails 3)是运行rake assets:precompile:primary而不是rake assets:precompile:all