Thomas Taylor Thomas Taylor - 1 month ago 15
Sass (Sass) Question

Compress non digested SASS assets rails

I have some assets that I need to keep out of the asset pipeline but still need to be compressed, specifically some SASS files. I have achieved this for my non digested JS which looks something like this:

Dir["#{Rails.root}/app/assets/javascripts/non_digest/*"].each do |asset|
asset_name = File.basename(asset)
asset_output = "#{Rails.root}/public/external/#{asset_name}"
asset_uglified = Uglifier.compile(File.read(asset))
File.open(asset_output, 'w') {|a| a.write(asset_uglified) }
end


This is included in my initializers.

Is it possible to use Sprockets to do the same for my non digested SASS files? I've taken a quick look at
Sprockets::SassCompressor
but haven't been able to get it to work so far.

Answer

If you have the sass gem installed you can do the following:

Dir["#{Rails.root}/app/assets/stylesheets/non_digest/*"].each do |asset|
  asset_name = File.basename(asset)
  asset_output = "#{Rails.root}/public/external/#{asset_name.gsub('.scss', '.css')}"
  asset_minified = Sass::Engine.new(File.read(asset), syntax: :scss, style: :compressed).render
  File.open(asset_output, 'w') {|a| a.write(asset_minified) }
end