Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the random string appended to sprite filename with Compass/Sass

I've recently been using Compass with Sass to do some CSS spriting, as it's extremely useful.

However, the filename is always appended with a random string. E.g. icons-s5eb424578c.png. And I don't want this random string to be appended, because it means I'm required to upload both the new CSS file & the new sprite image every time there's a change.

So, does anyone know which Ruby or other config file within the Compass gem directory, that is appending this random string? Then I can just comment the code out for that bit. Unless I'm missing an official variable I can set within Compass to tell it I don't want this string appended?

Thanks in advance for any help on this.

like image 316
Peter J Langley Avatar asked Jan 05 '13 15:01

Peter J Langley


2 Answers

Try to add these lines into your config.rb:

module Compass::SassExtensions::Functions::Sprites
  def sprite_url(map)
    verify_map(map, "sprite-url")
    map.generate
    generated_image_url(Sass::Script::String.new(map.name_and_hash))
  end
end

module Compass::SassExtensions::Sprites::SpriteMethods
  def name_and_hash
    "sprite-#{path}.png"
  end

  def cleanup_old_sprites
    Dir[File.join(::Compass.configuration.generated_images_path, "sprite-#{path}.png")].each do |file|
      log :remove, file
      FileUtils.rm file
      ::Compass.configuration.run_sprite_removed(file)
    end
  end
end

module Compass
  class << SpriteImporter
    def find_all_sprite_map_files(path)
      glob = "sprite-*{#{self::VALID_EXTENSIONS.join(",")}}"
      Dir.glob(File.join(path, "**", glob))
    end
  end
end

Works for me with Compass 0.12.2 (Alnilam)

like image 71
jslayer Avatar answered Nov 06 '22 22:11

jslayer


in your project config file enter something like this

asset_cache_buster :none

# Make a copy of sprites with a name that has no uniqueness of the hash.
on_sprite_saved do |filename|
  if File.exists?(filename)
    FileUtils.mv filename, filename.gsub(%r{-s[a-z0-9]{10}\.png$}, '.png')
  end
end

# Replace in stylesheets generated references to sprites
# by their counterparts without the hash uniqueness.
on_stylesheet_saved do |filename|
  if File.exists?(filename)
    css = File.read filename
    File.open(filename, 'w+') do |f|
      f << css.gsub(%r{-s([a-z0-9]{10})\.png}, '.png?v\1')
    end
  end
end

credits goes here How to remove the hash from Compass's generated sprite image filenames?

like image 24
FDisk Avatar answered Nov 06 '22 21:11

FDisk