问题
I've seen a few questions/answers around avoiding precompiling various assets while using the Rails pipeline; however, I want to effectively blacklist via an array of Regex's for pathname matches to exclude from precompilation. Most often for me, this is often a set of partials that will fail precompilation anyway.
回答1:
First off -- keithgaputis has expertly answered a part of this here but it's not quite an answer to the above question. Read and vote up his answer and then see my additions to his below:
Rails.application.config.assets.precompile << Proc.new { |path|
blacklist = [
/nvd3\/src\/intro.js$/,
/nvd3\/src\/outro.js$/,
/^.*\.less$/,
/admin\/modules/,
/admin\/themes/,
/admin\/responsive\..*css/
]
full_path = Rails.application.assets.resolve(path)#.to_path
puts "path: #{path}\nfull_path: #{full_path}" if BLACK_MAGIC[:assets][:debug]
if (path =~ /(^[^_\/]|\/[^_])[^\/]*$/) and (path !~ Regexp.union(blacklist) )
puts "including asset: " + full_path if BLACK_MAGIC[:assets][:debug]
true
else
puts "excluding asset: " + full_path if BLACK_MAGIC[:assets][:debug]
false
end
}
You can add all of your regular expressions to the blacklist array for exclusion and then the two part if condition
if (path =~ /(^[^_\/]|\/[^_])[^\/]*$/) and (path !~ Regexp.union(blacklist) )
will first eliminate items beginning with underscore (this isn't quite a perfect Regex yet, play with rubular) and secondly will eliminate anything that matches the blacklisted expressions. Happy coding!
来源:https://stackoverflow.com/questions/34801798/avoid-precompiling-asset-partials-in-rails-blacklist-by-regex