Avoid precompiling asset partials in Rails (blacklist by Regex)

 ̄綄美尐妖づ 提交于 2019-11-28 06:17:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!