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

后端 未结 8 1537
粉色の甜心
粉色の甜心 2020-11-28 06:42

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 th

相关标签:
8条回答
  • 2020-11-28 07:20

    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)
    
    0 讨论(0)
  • 2020-11-28 07:26

    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 ]
    
    0 讨论(0)
提交回复
热议问题