避免在Rails中预编译资产部分(Regex列入黑名单) [英] Avoid precompiling asset partials in Rails (blacklist by Regex)
问题描述
我已经看到了一些有关避免在使用Rails管道时预编译各种资产的问题/答案;但是,我想通过一个正则表达式数组有效地将其列入黑名单,以将路径名匹配排除在预编译之外.对于我来说,最常见的是,这通常是一组部分组件,无论如何它们都将无法进行预编译.
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.
推荐答案
首先-keithgaputis已熟练地在此处回答了部分问题,但这并不是上述问题的答案.阅读并投票支持他的答案,然后在下面看到我的补充内容:
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) )
首先将消除以下划线开头的项目(这还不是完美的Regex,可与rubular一起玩),其次将消除与黑名单表达式匹配的任何内容.编码愉快!
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!
这篇关于避免在Rails中预编译资产部分(Regex列入黑名单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!