Skip to content

Commit

Permalink
Import preprocessor so @import'ed less files are automatic asset depe…
Browse files Browse the repository at this point in the history
…ndencies. Fixes metaskills#3.
  • Loading branch information
metaskills committed Nov 18, 2011
1 parent 442b7d4 commit b247f24
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.0.4 -
------------------
* Import preprocessor so @import'ed less files are automatic asset dependencies. Fixes #3.


2.0.3 - 11/13/2011
------------------
* Add generator support. Fixes #8.
Expand Down
1 change: 1 addition & 0 deletions lib/less/rails.rb
Expand Up @@ -12,5 +12,6 @@ module Rails
require 'less/rails/version'
require 'less/rails/helpers'
require 'less/rails/template_handlers'
require 'less/rails/import_processor'
require 'less/rails/css_compressor'
require 'less/rails/railtie'
23 changes: 23 additions & 0 deletions lib/less/rails/import_processor.rb
@@ -0,0 +1,23 @@
module Less
module Rails
class ImportProcessor < Tilt::Template

IMPORT_SCANNER = /@import\s*['"]([^'"]+)['"]\s*;/.freeze

def prepare
end

def evaluate(context, locals, &block)
import_paths = data.scan(IMPORT_SCANNER).flatten.compact.uniq
import_paths.each do |path|
asset = context.environment[path]
if asset && asset.pathname.to_s.ends_with?('.less')
context.depend_on_asset(asset.pathname)
end
end
data
end

end
end
end
1 change: 1 addition & 0 deletions lib/less/rails/railtie.rb
Expand Up @@ -20,6 +20,7 @@ module LessContext

initializer 'less-rails.before.load_config_initializers', :before => 'load_config_initializers', :group => :all do |app|
raise 'The less-rails plugin requires the asset pipeline to be enabled.' unless app.config.assets.enabled
app.assets.register_preprocessor 'text/css', ImportProcessor
end

initializer 'less-rails.after.load_config_initializers', :after => 'load_config_initializers', :group => :all do |app|
Expand Down
19 changes: 18 additions & 1 deletion test/cases/basics_spec.rb
Expand Up @@ -15,7 +15,12 @@ class BasicsSpec < Less::Rails::Spec
end

it 'must hook into less import so that imported paths are declared as sprocket dependencies of the source file' do
skip 'will need to use the basics asset, then change the frameworks/bootstrap/mixins.less file, then render basics again'
basics.must_match %r{#test-radiused\{border-radius:5px;\}}, 'default is 5px'
safely_edit_mixins do |d|
d.gsub! '5px', '10px'
File.open(mixins_asset.pathname,'w') { |f| f.write(d) }
basics.must_match %r{#test-radiused\{border-radius:10px;\}}, 'mixins.less should be a sprockets context dependency'
end
end

protected
Expand All @@ -24,4 +29,16 @@ def basics
dummy_asset 'basics'
end

def mixins_asset
dummy_assets['frameworks/bootstrap/mixins.less']
end

def safely_edit_mixins
data = File.read(mixins_asset.pathname)
begin
yield data.dup
ensure
File.open(mixins_asset.pathname,'w') { |f| f.write(data) }
end
end
end
4 changes: 4 additions & 0 deletions test/cases/railtie_spec.rb
Expand Up @@ -34,6 +34,10 @@ class RailtieSpec < Less::Rails::Spec
dummy_assets.context_class.less_config.must_equal dummy_config.less
end

it 'must register our import pre processor' do
dummy_assets.preprocessors['text/css'].must_include Less::Rails::ImportProcessor
end

end


Expand Down

0 comments on commit b247f24

Please sign in to comment.