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
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)
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 ]