rake assets:precompile is slow

后端 未结 3 1957
太阳男子
太阳男子 2020-12-15 04:36

The command \"rake assets:precompile\" works very slow for me. Especially on my Amazon EC2 Micro production server which does not have a lot of processor resources. On EC2 I

3条回答
  •  渐次进展
    2020-12-15 05:05

    My solution is to exclude application.js .css and any other application related assets from from precompilation. So that i can use rake assets:precompile once to precompile engine related assets only.

    Then on each deploy i use a simple rake task to build any application related assets and merge them into manifest.yml:

    namespace :assets do
      task :precompile_application_only => :environment do
        require 'sprockets'
    
        # workaround used also by native assets:precompile:all to load sprockets hooks 
        _ = ActionView::Base
    
        # ==============================================
        # = Read configuration from Rails / assets.yml =
        # ==============================================
    
        env           = Rails.application.assets
        target        = File.join(::Rails.public_path, Rails.application.config.assets.prefix)
        assets        = YAML.load_file(Rails.root.join('config', 'assets.yml'))
        manifest_path = Rails.root.join(target, 'manifest.yml')
        digest        = !!Rails.application.config.assets.digest
        manifest      = digest
    
    
        # =======================
        # = Old manifest backup =
        # =======================
    
        manifest_old = File.exists?(manifest_path) ? YAML.load_file(manifest_path) : {}
    
        # ==================
        # = Compile assets =
        # ==================
    
        compiler = Sprockets::StaticCompiler.new(env,
                                                target,
                                                assets,
                                                :digest => digest,
                                                :manifest => manifest)
        compiler.compile
    
        # ===================================
        # = Merge new manifest into old one =
        # ===================================
    
        manifest_new  = File.exists?(manifest_path) ? YAML.load_file(manifest_path) : {}
    
        File.open(manifest_path, 'w') do |out|
           YAML.dump(manifest_old.merge(manifest_new), out)
        end
    
      end
    end
    

    To specify which assets to compile i use a YAML configuration file (config/assets.yml):

    eg.

    ---
    - site/site.css
    - admin/admin.css
    - site/site.js
    - admin/admin.js
    

提交回复
热议问题