Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take into account when a sprite image changes and is recompiled #3

Merged
merged 1 commit into from
Jun 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/spritely/sass_functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Spritely
module SassFunctions
def spritely_map(glob, kwargs = {})
SpriteMap.create(glob.value, kwargs).tap do |sprite_map|
reset_sprockets_directory_cache!
reset_sprockets_caches!(sprite_map)
sprockets_context.depend_on(Spritely.directory)
sprite_map.files.each do |file|
sprockets_context.depend_on(file)
Expand Down Expand Up @@ -60,8 +60,12 @@ def find_image(sprite_map, image_name)
sprite_map.find(image_name.value) || raise(Sass::SyntaxError, "No image '#{image_name.value}' found in sprite map '#{sprite_map.name}'.")
end

def reset_sprockets_directory_cache!
sprockets_context.environment.send(:trail).instance_variable_get(:@entries).delete(Spritely.directory.to_s)
def reset_sprockets_caches!(sprite_map)
sprockets_environment.instance_variable_get(:@assets).delete_if { |_, asset| asset.pathname == sprite_map.filename }
sprockets_environment.send(:trail).tap do |trail|
trail.instance_variable_get(:@entries).delete(Spritely.directory.to_s)
trail.instance_variable_get(:@stats).delete(sprite_map.filename.to_s)
end
end
end
end
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$application-sprite: spritely-map("application/*.png",
$background-repeat: true,
$football-spacing: 100px,
$mario-spacing: 10px,
$mario-position: right,
$spacing: 5px
);

body {
background-image: spritely-url($application-sprite);
background-position: spritely-position($application-sprite, "background");
}

#mario {
background-image: spritely-url($application-sprite);
background-position: spritely-position($application-sprite, "mario");
width: spritely-width($application-sprite, "mario");
height: spritely-height($application-sprite, "mario");
}

#arrow {
background-image: spritely-url($application-sprite);
background-position: spritely-position($application-sprite, "dropdown-arrow");
width: spritely-width($application-sprite, "dropdown-arrow");
height: spritely-height($application-sprite, "dropdown-arrow");
}
8 changes: 8 additions & 0 deletions spec/integration/precompilation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@
expect(File).to exist(File.join('app', 'assets', 'images', 'sprites', 'application.png'))
expect(File).to exist(File.join('app', 'assets', 'images', 'sprites', 'foo.png'))
end

it 'should correctly compile a new sprite image and CSS when files change' do
compile_assets
FileUtils.cp_r "#{__dir__}/../fixtures/rails-app-changes/.", "."
compile_assets
sprite_files = Dir.glob(File.join('public', 'assets', 'sprites', 'application-*.png'))
expect(sprite_files.length).to eq(2)
end
end
20 changes: 13 additions & 7 deletions spec/spritely/sass_functions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require 'spec_helper'
require 'sprockets'
require 'ostruct'

describe Spritely::SassFunctions do
class SpriteMapDouble < Sass::Script::Literal
def name; 'test'; end
def filename; 'test.png'; end
def files; []; end
end

Expand All @@ -25,17 +27,19 @@ def files; []; end
end
end

shared_examples "a sprite function that resets the sprockets directory cache" do
it 'should clear out the trail entries' do
shared_examples "a sprite function that resets the sprockets directory caches" do
it 'should clear out the trail caches' do
subject
expect(environment.instance_variable_get(:@assets).length).to eq(0)
expect(environment.send(:trail).instance_variable_get(:@entries)).to_not have_key(directory)
expect(environment.send(:trail).instance_variable_get(:@stats)).to_not have_key('test.png')
end
end

describe '#spritely_url' do
subject { evaluate(".background-image { background-image: spritely-url(spritely-map('test/*.png')); }") }

it_should_behave_like "a sprite function that resets the sprockets directory cache"
it_should_behave_like "a sprite function that resets the sprockets directory caches"

it { should eq(".background-image {\n background-image: url(sprites/test.png); }\n") }
end
Expand All @@ -44,7 +48,7 @@ def files; []; end
subject { evaluate(".background-position { background-position: spritely-position(spritely-map('test/*.png'), 'bar'); }") }

it_should_behave_like "a sprite function that checks image existence"
it_should_behave_like "a sprite function that resets the sprockets directory cache"
it_should_behave_like "a sprite function that resets the sprockets directory caches"

it { should eq(".background-position {\n background-position: -10px -12px; }\n") }

Expand All @@ -59,7 +63,7 @@ def files; []; end
subject { evaluate(".background { background: spritely-background(spritely-map('test/*.png'), 'bar'); }") }

it_should_behave_like "a sprite function that checks image existence"
it_should_behave_like "a sprite function that resets the sprockets directory cache"
it_should_behave_like "a sprite function that resets the sprockets directory caches"

it { should eq(".background {\n background: url(sprites/test.png) -10px -12px; }\n") }
end
Expand All @@ -68,7 +72,7 @@ def files; []; end
subject { evaluate(".width { width: spritely-width(spritely-map('test/*.png'), 'bar'); }") }

it_should_behave_like "a sprite function that checks image existence"
it_should_behave_like "a sprite function that resets the sprockets directory cache"
it_should_behave_like "a sprite function that resets the sprockets directory caches"

it { should eq(".width {\n width: 25px; }\n") }
end
Expand All @@ -77,7 +81,7 @@ def files; []; end
subject { evaluate(".height { height: spritely-height(spritely-map('test/*.png'), 'bar'); }") }

it_should_behave_like "a sprite function that checks image existence"
it_should_behave_like "a sprite function that resets the sprockets directory cache"
it_should_behave_like "a sprite function that resets the sprockets directory caches"

it { should eq(".height {\n height: 50px; }\n") }
end
Expand All @@ -88,7 +92,9 @@ def evaluate(value)

def environment
@environment ||= Sprockets::Environment.new.tap do |environment|
environment.instance_variable_set(:@assets, {'test' => OpenStruct.new(pathname: 'test.png')})
environment.send(:trail).instance_variable_set(:@entries, {'spritely-directory' => 'blah'})
environment.send(:trail).instance_variable_set(:@stats, {'test.png' => 'blah'})
environment.context_class.class_eval do
def asset_path(path, options = {})
path
Expand Down