Avoid precompiling asset partials in Rails (blacklist by Regex)

前端 未结 1 476
夕颜
夕颜 2020-12-20 05:31

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

相关标签:
1条回答
  • 2020-12-20 05:51

    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!

    0 讨论(0)
提交回复
热议问题