简体   繁体   中英

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

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 the dependencies of my files as needed.

I tried the following wildcard setting, but it incorrectly precompiles everything. This results in lots of extra work and even causes a compilation failure when using bootstrap-sass.

config.assets.precompile += ['*.js', '*.css']

What is my best bet to only process my files in app/assets ? Thanks!

config.assets.precompile = ['*.js', '*.css']

That will compile any JavaScript or CSS in your asset path, regardless of directory depth. Found via this answer .

This task is made more difficult by the fact that sprockets works with logical paths that do not include where the underlying, uncompiled resourced is located.

Suppose my project has the JS file "/app/assets/javascripts/foo/bar.js.coffee".

The sprockets compiler will first determine the output file extension, in this case ".js", and then the evaluate whether or not to compile the logical path "foo/bar.js". The uncompiled resource could be in "app/assets/javascripts", "vendor/assets/javascripts", "lib/assets/javascripts" or a gem, so there is no way to include/exclude a particular file based on the logical path alone.

To determine where the underlying resource is located, I believe it is necessary to ask the sprockets environment (available via the object Rails.application.assets) to resolve the real path of the resource given the logical path.

Here is the solution that I am using. I am fairly new to Ruby so this is not the most elegant code:

# In production.rb
config.assets.precompile << Proc.new { |path|
  if path =~ /\.(css|js)\z/
    full_path = Rails.application.assets.resolve(path).to_path
    app_assets_path = Rails.root.join('app', 'assets').to_path
    if full_path.starts_with? app_assets_path
      puts "including asset: " + full_path
      true
    else
      puts "excluding asset: " + full_path
      false
    end
  else
    false
  end
}

With sprockets > 3.0, this will not work in production because Rails.application.assets will be nil (assuming default: config.assets.compile = false).

To workaround you replace the full_path assignment with:

@assets ||= Rails.application.assets || Sprockets::Railtie.build_environment(Rails.application)
full_path = @assets.resolve(path)

See also: https://github.com/rails/sprockets-rails/issues/237

A slight tweak to techpeace's answer:

config.assets.precompile = ['*.js', '*.css', '**/*.js', '**/*.css']

I would've added a comment to his answer but I don't have enough reputation yet. Give me an upvote and I'll be there!

NOTE: this will also precompile all the CSS/JavaScript included via rubygems.

I found this in the rails code:

@assets.precompile               = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) },
                                     /(?:\/|\\|\A)application\.(css|js)$/ ]

Which is backed up with the rails guide:

The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files

This default is not reset if you use += , so you need to override it with a = instead of += . Note that, apparently, you can pass a Proc or a regex to precompile as well as an extension. I believe, if you want to preompile only files in the top level directory, you will have to create a regex like:

config.assets.precompile = [ /\A[^\/\\]+\.(ccs|js)$/i ]

I'm revisiting this post at the year 2017.

Our product is still heavily using RoR, we've been continuously modifying our precompile configurations by appending Rails.application.config.assets.precompile as we're adding new modules. Recently I was trying to optimize this by adding a regex pattern, I found that the following glob pattern works:

Rails.application.config.assets.precompile += %w(**/bundle/*.js)

However I still need to exclude certain modules, so I tried hard to use regex instead of glob.

Until I looked into sprockets source code: https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L108 , I found that they're already using regex:

app.config.assets.precompile +=
  [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]

So I change my code into:

Rails.application.config.assets.precompile +=
  [/^((?!my_excluded_module)\w)*\/bundle\/\w*\.js$/]

Which works well.

This will get all .css .scss and .js including all files in subdirectories.

js_prefix    = 'app/assets/javascripts/'
style_prefix = 'app/assets/stylesheets/'

javascripts = Dir["#{js_prefix}**/*.js"].map      { |x| x.gsub(js_prefix,    '') }
css         = Dir["#{style_prefix}**/*.css"].map  { |x| x.gsub(style_prefix, '') }
scss        = Dir["#{style_prefix}**/*.scss"].map { |x| x.gsub(style_prefix, '') }

Rails.application.config.assets.precompile = (javascripts + css + scss)

I wanted all assets from both /app and /vendor to be compiled, except partials (which name starts from underscore _). So here is my version of an entry in application.rb:

config.assets.precompile << Proc.new { |path|
  if path =~ /\.(css|js)\z/
    full_path = Rails.application.assets.resolve(path).to_path
    app_assets_path = Rails.root.join('app', 'assets').to_path
    vendor_assets_path = Rails.root.join('vendor', 'assets').to_path

    if ((full_path.starts_with? app_assets_path) || (full_path.starts_with? vendor_assets_path)) && (!path.starts_with? '_')
      puts "\t" + full_path.slice(Rails.root.to_path.size..-1)
      true
    else
      false
    end
  else
    false
  end
}

Additionally it outputs list of files being compiled for debugging purposes...

This snippet includes all js/css files, excluding gems , under: app/assets, vendor/assets , lib/assets , unless they are partial files (eg "_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 (eg "_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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM