Rails config.assets.precompile setting to process all CSS and JS files in app/assets

后端 未结 8 1598
粉色の甜心
粉色の甜心 2020-11-28 06:42

I wish to precompile all the CSS and JS files in my project\'s app/assets folder. I do NOT want to precompile everything in vendor/assets or lib/assets, only th

8条回答
  •  长情又很酷
    2020-11-28 07:11

    This snippet includes all js/css files, excluding gems, under: app/assets, vendor/assets, lib/assets, unless they are partial files (e.g. "_file.sass"). It also has a strategy for including assets from Gems which aren't included in every page.

        # These assets are from Gems which aren't included in every page.
        # So they must be included here
        # instead of in the application.js and css manifests.
        config.assets.precompile += %w(a-gem.css a-gem.js b-gem.js)
    
        # This block includes all js/css files, excluding gems,
        # under: app/assets, vendor/assets, lib/assets
        # unless they are partial files (e.g. "_file.sass")
        config.assets.precompile << Proc.new { |path|
          if path =~ /\.(css|js)\z/
            full_path = Rails.application.assets.resolve(path).to_path
            aps = %w( /app/assets /vendor/assets /lib/assets )
            if aps.any? {|ap| full_path.starts_with?("#{Rails.root}#{ap}")} &&
                !path.starts_with?('_')
              puts "\tIncluding: " + full_path.slice(Rails.root.to_path.size..-1)
              true
            else
              puts "\tExcluding: " + full_path
              false
            end
          else
            false
          end
        }
    

    Although, you probably wouldn't want to do this since you'd likely be precompiling gem assets twice (basically anything that is already \=require'd in your application.js or css). This snippet includes all js/css files, including gems, under: app/assets, vendor/assets, lib/assets, unless they are partial files (e.g. "_file.sass")

    # This block includes all js/css files, including gems, 
    # under: app/assets, vendor/assets, lib/assets
    # and excluding partial files (e.g. "_file.sass")
    config.assets.precompile << Proc.new { |path|
      if path =~ /\.(css|js)\z/
        full_path = Rails.application.assets.resolve(path).to_path
        asset_paths = %w( app/assets vendor/assets lib/assets)
        if (asset_paths.any? {|ap| full_path.include? ap}) && 
            !path.starts_with?('_')
          puts "\tIncluding: " + full_path
          true
        else
          puts "\tExcluding: " + full_path
          false
        end
      else
        false
      end
    }
    

提交回复
热议问题