Compass support in Sprockets

This commit is contained in:
Thomas Reynolds 2011-10-19 16:21:46 -07:00
parent 7d6386fdc0
commit 5cc96210ce
15 changed files with 116 additions and 57 deletions

View file

@ -1,16 +1,16 @@
Feature: Sprockets Feature: Sprockets
Scenario: Sprockets require Scenario: Sprockets JS require
Given the Server is running at "test-app" Given the Server is running at "test-app"
When I go to "/javascripts/sprockets_base.js" When I go to "/javascripts/sprockets_base.js"
Then I should see "sprockets_sub_function" Then I should see "sprockets_sub_function"
Scenario: Sprockets require with custom :js_dir Scenario: Sprockets JS require with custom :js_dir
Given the Server is running at "sprockets-app" Given the Server is running at "sprockets-app"
When I go to "/library/javascripts/sprockets_base.js" When I go to "/library/javascripts/sprockets_base.js"
Then I should see "sprockets_sub_function" Then I should see "sprockets_sub_function"
Scenario: Sprockets should have access to yaml data Scenario: Sprockets JS should have access to yaml data
Given the Server is running at "test-app" Given the Server is running at "test-app"
When I go to "/javascripts/multiple_engines.js" When I go to "/javascripts/multiple_engines.js"
Then I should see "Hello One" Then I should see "Hello One"
@ -19,3 +19,23 @@ Feature: Sprockets
Given a built app at "test-app" Given a built app at "test-app"
Then "javascripts/multiple_engines.js" should exist at "test-app" and include "Hello One" Then "javascripts/multiple_engines.js" should exist at "test-app" and include "Hello One"
And cleanup built app at "test-app" And cleanup built app at "test-app"
Scenario: Sprockets CSS require //require
Given the Server is running at "test-app"
When I go to "/stylesheets/sprockets_base1.css"
Then I should see "hello"
Scenario: Sprockets CSS require @import
Given the Server is running at "test-app"
When I go to "/stylesheets/sprockets_base2.css"
Then I should see "hello"
Scenario: Sprockets CSS require with custom :css_dir //require
Given the Server is running at "sprockets-app"
When I go to "/library/stylesheets/sprockets_base1.css"
Then I should see "hello"
Scenario: Sprockets CSS require with custom :css_dir @import
Given the Server is running at "sprockets-app"
When I go to "/library/stylesheets/sprockets_base2.css"
Then I should see "hello"

View file

@ -1,7 +1,5 @@
@wip
Feature: Sprockets Gems Feature: Sprockets Gems
Scenario: Sprockets can pull jQuery from gem Scenario: Sprockets can pull jQuery from gem
Given the Server is running at "sprockets-app" Given the Server is running at "sprockets-app"
When I go to "/javascripts/jquery_include.js" When I go to "/library/javascripts/jquery_include.js"
Then I should see "var jQuery =" Then I should see "var jQuery ="

View file

@ -1 +1,2 @@
set :js_dir, "library/javascripts" set :js_dir, "library/javascripts"
set :css_dir, "library/stylesheets"

View file

@ -0,0 +1 @@
//= require "sprockets_sub"

View file

@ -0,0 +1 @@
@import "sprockets_sub";

View file

@ -0,0 +1 @@
hello { world: "hi"; }

View file

@ -0,0 +1 @@
//= require "sprockets_sub"

View file

@ -0,0 +1 @@
@import "sprockets_sub";

View file

@ -0,0 +1 @@
hello { world: "hi"; }

View file

@ -68,7 +68,7 @@ module Middleman::CoreExtensions::Compass
config.asset_cache_buster :none config.asset_cache_buster :none
config.output_style = :nested config.output_style = :nested
config.add_import_path(config.sass_dir) # config.add_import_path(config.sass_dir)
end end
# Required for relative paths # Required for relative paths
@ -84,8 +84,9 @@ module Middleman::CoreExtensions::Compass
end end
app.execute_after_compass_init! app.execute_after_compass_init!
app.execute_after_compass_config!
app.set :sass, ::Compass.configuration.to_sass_engine_options # app.set :sass, ::Compass.configuration.to_sass_engine_options
end end
end end
alias :included :registered alias :included :registered
@ -102,5 +103,15 @@ module Middleman::CoreExtensions::Compass
@run_after_compass ||= [] @run_after_compass ||= []
@run_after_compass.each { |block| block.call(::Compass.configuration) } @run_after_compass.each { |block| block.call(::Compass.configuration) }
end end
def after_compass_config(&block)
@run_after_compass_config ||= []
@run_after_compass_config << block
end
def execute_after_compass_config!
@run_after_compass_config ||= []
@run_after_compass_config.each { |block| block.call() }
end
end end
end end

View file

@ -33,10 +33,20 @@ module Middleman::CoreExtensions::Sprockets
app.map "/#{app.js_dir}" do app.map "/#{app.js_dir}" do
run js_env run js_env
end end
end
# app.map "/#{app.css_dir}" do app.after_compass_config do
# run Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(app) css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(app)
# end css_dir = File.join("vendor", "assets", "stylesheets")
gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, css_dir)
end.each do |spec|
css_env.append_path File.join(spec.full_gem_path, css_dir)
end
app.map "/#{app.css_dir}" do
run css_env
end
end end
end end
alias :included :registered alias :included :registered
@ -82,21 +92,20 @@ module Middleman::CoreExtensions::Sprockets
end end
end end
# class StylesheetEnvironment < MiddlemanEnvironment class StylesheetEnvironment < MiddlemanEnvironment
# def initialize(app) def initialize(app)
# super super
#
# # Disable js # Disable js
# unregister_processor "application/javascript", ::Sprockets::DirectiveProcessor unregister_processor "application/javascript", ::Sprockets::DirectiveProcessor
#
# # configure search paths # configure search paths
# stylesheets_path = File.join(File.expand_path(app.views), app.css_dir) append_path app.css_dir
# append_path stylesheets_path end
# end
# def css_exception_response(exception)
# def css_exception_response(exception) expire_index!
# expire_index! super(exception)
# super(exception) end
# end end
# end
end end

View file

@ -1,5 +1,6 @@
require "sprockets"
require "sprockets-sass"
require "sass" require "sass"
require "sass/plugin"
module Middleman::Renderers::Sass module Middleman::Renderers::Sass
class << self class << self
@ -10,40 +11,49 @@ module Middleman::Renderers::Sass
alias :included :registered alias :included :registered
end end
class SassPlusCSSFilenameTemplate < ::Tilt::SassTemplate class SassPlusCSSFilenameTemplate < ::Sprockets::Sass::SassTemplate
def sass_options_with_scope(scope) self.default_mime_type = "text/css"
return sass_options if basename.nil?
location_of_sass_file = if scope.build?
File.join(scope.root, scope.build_dir)
else
scope.views
end
parts = basename.split('.')
parts.pop
css_filename = File.join(location_of_sass_file, scope.css_dir, parts.join("."))
sass_options.merge(scope.settings.sass).merge(:css_filename => css_filename)
end
def evaluate(scope, locals, &block)
@engine = ::Sass::Engine.new(data, sass_options_with_scope(scope.class))
# Add exception messaging
def evaluate(context, locals, &block)
begin begin
super super
rescue Sass::SyntaxError => e rescue Sass::SyntaxError => e
Sass::SyntaxError.exception_to_css(e, :full_exception => true) Sass::SyntaxError.exception_to_css(e, :full_exception => true)
end end
end end
protected
def sass_options
location_of_sass_file = if @context.build?
File.expand_path(@context.build_dir, @context.root)
else
File.expand_path(@context.views, @context.root)
end end
parts = basename.split('.')
parts.pop
css_filename = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
super.merge(
:css_filename => css_filename
)
end
end
::Sprockets.register_engine ".sass", SassPlusCSSFilenameTemplate
::Tilt.register 'sass', SassPlusCSSFilenameTemplate ::Tilt.register 'sass', SassPlusCSSFilenameTemplate
::Tilt.prefer(SassPlusCSSFilenameTemplate) ::Tilt.prefer(SassPlusCSSFilenameTemplate)
class ScssPlusCSSFilenameTemplate < SassPlusCSSFilenameTemplate class ScssPlusCSSFilenameTemplate < SassPlusCSSFilenameTemplate
def sass_options_with_scope(scope) self.default_mime_type = "text/css"
super.merge(:syntax => :scss)
# Define the expected syntax for the template
def syntax
:scss
end end
end end
::Sprockets.register_engine ".scss", ScssPlusCSSFilenameTemplate
::Tilt.register 'scss', ScssPlusCSSFilenameTemplate ::Tilt.register 'scss', ScssPlusCSSFilenameTemplate
::Tilt.prefer(ScssPlusCSSFilenameTemplate) ::Tilt.prefer(ScssPlusCSSFilenameTemplate)
end end

View file

@ -51,6 +51,7 @@ eos
s.add_dependency("compass", ["~> 0.11.3"]) s.add_dependency("compass", ["~> 0.11.3"])
s.add_dependency("coffee-script", ["~> 2.2.0"]) s.add_dependency("coffee-script", ["~> 2.2.0"])
s.add_dependency("sprockets", ["~> 2.0.3"]) s.add_dependency("sprockets", ["~> 2.0.3"])
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
s.add_dependency("padrino-core", ["~> 0.10.5"]) s.add_dependency("padrino-core", ["~> 0.10.5"])
s.add_dependency("padrino-helpers", ["~> 0.10.5"]) s.add_dependency("padrino-helpers", ["~> 0.10.5"])
@ -68,5 +69,6 @@ eos
s.add_development_dependency("cucumber", ["~> 1.0.2"]) s.add_development_dependency("cucumber", ["~> 1.0.2"])
s.add_development_dependency("rake", ["~> 0.9.2"]) s.add_development_dependency("rake", ["~> 0.9.2"])
s.add_development_dependency("rspec", ["~> 2.6.0"]) s.add_development_dependency("rspec", ["~> 2.6.0"])
s.add_development_dependency("jquery-rails")
end end

View file

@ -53,6 +53,7 @@ eos
s.add_dependency("coffee-script", ["~> 2.2.0"]) s.add_dependency("coffee-script", ["~> 2.2.0"])
s.add_dependency("execjs", ["~> 1.2.7"]) s.add_dependency("execjs", ["~> 1.2.7"])
s.add_dependency("sprockets", ["~> 2.0.3"]) s.add_dependency("sprockets", ["~> 2.0.3"])
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
s.add_dependency("padrino-core", ["~> 0.10.5"]) s.add_dependency("padrino-core", ["~> 0.10.5"])
s.add_dependency("padrino-helpers", ["~> 0.10.5"]) s.add_dependency("padrino-helpers", ["~> 0.10.5"])
@ -65,5 +66,6 @@ eos
s.add_development_dependency("cucumber", ["~> 1.0.2"]) s.add_development_dependency("cucumber", ["~> 1.0.2"])
s.add_development_dependency("rake", ["~> 0.9.2"]) s.add_development_dependency("rake", ["~> 0.9.2"])
s.add_development_dependency("rspec", ["~> 2.6.0"]) s.add_development_dependency("rspec", ["~> 2.6.0"])
s.add_development_dependency("jquery-rails")
end end