Simplify compass config with extra file or new callback

This commit is contained in:
Thomas Reynolds 2011-07-25 20:10:07 -07:00
parent d3cb8808ae
commit 2c3523b94a
7 changed files with 65 additions and 31 deletions

View file

@ -7,4 +7,6 @@
- Refactored Dynamically Reloadable Core
- Combine views/ and public/ into a single source/ folder.
- Support YAML front-matter
- Added callback to run code after Compass is configured
- Added support for a compass.config file which is passed directly to Compass
- Blog-aware Feature

View file

@ -19,5 +19,5 @@ Feature: Builder
Scenario: Force relative assets
Given a built test app with flags "--relative"
Then "stylesheets/site.css" should exist and include "../"
Then "stylesheets/relative_assets.css" should exist and include "../"
And cleanup built test app

View file

@ -12,31 +12,60 @@ module Middleman::CoreExtensions::Compass
end
app.after_feature_init do
views_root = File.basename(app.views)
# Support a stand-alone compass config file
# Many options are overwritten by Middleman, but the config is a good
# place to add:
# * output_style
# * disable_warnings
# * sass_options
# * line_comments
# * sprite_engine
# * chunky_png_options
compass_config_file = File.join(app.root, "compass.config")
if File.exists?(compass_config_file)
::Compass.add_project_configuration(compass_config_file)
end
::Compass.configuration do |config|
# config.cache = false # For sassc files
config.cache_path = File.join(app.root, ".sass-cache")
config.project_path = app.root
config.environment = :development
config.cache_path = File.join(app.root, ".sass-cache")
views_root = File.basename(app.views)
config.sass_dir = File.join(views_root, app.css_dir)
config.output_style = :nested
config.fonts_dir = File.join(views_root, app.fonts_dir)
config.css_dir = File.join(views_root, app.css_dir)
config.images_dir = File.join(views_root, app.images_dir)
config.http_images_path = app.http_images_path rescue File.join(app.http_prefix || "/", app.images_dir)
config.http_stylesheets_path = app.http_css_path rescue File.join(app.http_prefix || "/", app.css_dir)
config.javascripts_dir = File.join(views_root, app.js_dir)
config.fonts_dir = File.join(views_root, app.fonts_dir)
config.images_dir = File.join(views_root, app.images_dir)
if app.respond_to? :http_images_path
config.http_images_path = app.http_images_path
end
if app.respond_to? :http_css_path
config.http_stylesheets_path = app.http_css_path
end
if app.respond_to? :http_js_path
config.http_javascripts_path = app.http_js_path
end
config.asset_cache_buster :none
config.output_style = :nested
config.add_import_path(config.sass_dir)
end
# Required for relative paths
configure :build do
build_root = File.basename(self.build_dir)
::Compass.configuration do |config|
config.css_dir = File.join(build_root, self.css_dir)
config.images_dir = File.join(build_root, self.images_dir)
end
end
::Compass.configuration do |config|
config.environment = :production
build_root = File.basename(self.build_dir)
config.css_dir = File.join(build_root, self.css_dir)
config.images_dir = File.join(build_root, self.images_dir)
end
end
app.execute_after_compass_init!
@ -48,14 +77,14 @@ module Middleman::CoreExtensions::Compass
module ClassMethods
# Add a block/proc to be run after features have been setup
def after_compass_init(&block)
def compass_config(&block)
@run_after_compass ||= []
@run_after_compass << block
end
def execute_after_compass_init!
@run_after_compass ||= []
@run_after_compass.each { |block| class_eval(&block) }
@run_after_compass.each { |block| block.call(::Compass.configuration) }
end
end
end

View file

@ -1,9 +1,9 @@
module Middleman::Features::AssetHost
class << self
def registered(app)
app.after_compass_init do
app.compass_config do |config|
if app.asset_host.is_a?(Proc)
::Compass.configuration.asset_host(&app.asset_host)
config.asset_host(&app.asset_host)
end
end

View file

@ -25,16 +25,14 @@ module Middleman::Features::CacheBuster
end
end
app.after_compass_init do
::Compass.configuration do |config|
config.asset_cache_buster do |path, real_path|
real_path = real_path.path if real_path.is_a? File
real_path = real_path.gsub(File.join(app.root, app.build_dir), app.views)
if File.readable?(real_path)
File.mtime(real_path).strftime("%s")
else
$stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
end
app.compass_config do |config|
config.asset_cache_buster do |path, real_path|
real_path = real_path.path if real_path.is_a? File
real_path = real_path.gsub(File.join(app.root, app.build_dir), app.views)
if File.readable?(real_path)
File.mtime(real_path).strftime("%s")
else
$stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
end
end
end

View file

@ -1,8 +1,8 @@
module Middleman::Features::MinifyCss
class << self
def registered(app)
app.after_compass_init do
::Compass.configuration.output_style = :compressed
app.compass_config do |config|
config.output_style = :compressed
end
end
alias :included :registered

View file

@ -52,6 +52,11 @@ set :images_dir, "<%= options[:images_dir] -%>"
# set :images_dir, "alternative_image_directory"
<% end -%>
# Change Compass configuration
# compass_config do |config|
# config.output_style = :compact
# end
# Build-specific configuration
configure :build do
# For example, change the Compass output style for deployment