Skip to content

Commit

Permalink
re-organizing and cleaning up things to prepare for a working compass…
Browse files Browse the repository at this point in the history
… extension instead of the Rakefile.
  • Loading branch information
heygrady committed Mar 20, 2012
1 parent 24c7a7e commit 2cc965e
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 158 deletions.
9 changes: 0 additions & 9 deletions Gemfile

This file was deleted.

118 changes: 0 additions & 118 deletions Rakefile

This file was deleted.

3 changes: 0 additions & 3 deletions bin/sprockets_watch

This file was deleted.

24 changes: 24 additions & 0 deletions compass-sprockets.gemspec
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "compass/sprockets/version"

Gem::Specification.new do |s|
s.name = "compass-sprockets"
s.version = Compass::Sprockets::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Grady Kuhnline"]
s.email = ["heygrady@gmail.com"]
s.homepage = "http://rubygems.org/gems/compass-sprockets"
s.summary = %q{ Compass extension for Sprockets }
s.description = %q{ An integration of Sprockets with Compass for auto-compiling JavaScript assets }

s.rubyforge_project = "compass-sprockets"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency("compass")
s.add_dependency("sprockets")
end
5 changes: 5 additions & 0 deletions lib/compass-sprockets.rb
@@ -0,0 +1,5 @@
require "sprockets"
require "sprockets/compressors"
require "sprockets/static_compiler"
require "compass/sprockets"

35 changes: 35 additions & 0 deletions lib/compass/sprockets.rb
@@ -0,0 +1,35 @@
require "compass/sprockets/version"

module Compass
module Sprockets
# register the framework
Compass::Frameworks.register("sprockets", :path => "#{File.dirname(__FILE__)}/..")

# register the custom config variables
Compass::Configuration.add_configuration_property(:javascripts_src_dir, "Location of JavaScript source files for use with Sprockets") do
"javascripts/src"
end

# register a custom watch
Compass::Configuration::Data.watch Compass.configuration.javascripts_src_dir + "/**/*" do |project_dir, relative_path|
if File.exists?(File.join(project_dir, relative_path))
# configure environment
environment = Sprockets::Environment.new(project_dir)
environment.append_path(Compass.configuration.javascripts_src_dir) #Compass.configuration.javascripts_dir + "/src"
environment.js_compressor = Sprockets::LazyCompressor.new { Sprockets::Compressors.registered_js_compressor(:uglifier) }

# configure the static compiler
compiler = Sprockets::StaticCompiler.new(
environment,
Compass.configuration.javascripts_dir,
[/^([\w-]+\/)?[^_]\w+\.(js|coffee)$/],
:manifest_path => Compass.configuration.javascripts_dir,
:digest => false,
:manifest => false,
:zip_files => /a^/
)
compiler.compile
end
end
end
end
6 changes: 6 additions & 0 deletions lib/compass/sprockets/version.rb
@@ -0,0 +1,6 @@
module Compass
module Sprockets
VERSION = "0.0.0"
#HTML5_BOILERPLATE_VERSION = "3.0.2"
end
end
96 changes: 96 additions & 0 deletions lib/sprockets/compressors.rb
@@ -0,0 +1,96 @@
module Sprockets
module Compressors
extend self

@@css_compressors = {}
@@js_compressors = {}
@@default_css_compressor = nil
@@default_js_compressor = nil

def register_css_compressor(name, klass, options = {})
@@default_css_compressor = name.to_sym if options[:default] || @@default_css_compressor.nil?
@@css_compressors[name.to_sym] = { :klass => klass.to_s, :require => options[:require] }
end

def register_js_compressor(name, klass, options = {})
@@default_js_compressor = name.to_sym if options[:default] || @@default_js_compressor.nil?
@@js_compressors[name.to_sym] = { :klass => klass.to_s, :require => options[:require] }
end

def registered_css_compressor(name)
find_registered_compressor name, @@css_compressors, @@default_css_compressor
end

def registered_js_compressor(name)
find_registered_compressor name, @@js_compressors, @@default_js_compressor
end

# The default compressors must be registered in default plugins (ex. Sass-Rails)
register_css_compressor(:scss, 'Sass::Rails::Compressor', :require => 'sass/rails/compressor', :default => true)
register_js_compressor(:uglifier, 'Uglifier', :require => 'uglifier', :default => true)

# Automaticaly register some compressors
register_css_compressor(:yui, 'YUI::CssCompressor', :require => 'yui/compressor')
register_js_compressor(:closure, 'Closure::Compiler', :require => 'closure-compiler')
register_js_compressor(:yui, 'YUI::JavaScriptCompressor', :require => 'yui/compressor')

private

def find_registered_compressor(name, compressors_hash, default_compressor_name)
if name.respond_to?(:to_sym)
compressor = compressors_hash[name.to_sym] || compressors_hash[default_compressor_name]
require compressor[:require] if compressor[:require]
constantize(compressor[:klass]).new
else
name
end
end

def constantize(camel_cased_word)
names = camel_cased_word.split('::')
names.shift if names.empty? || names.first.empty?

constant = Object
names.each do |name|
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
end

# An asset compressor which does nothing.
#
# This compressor simply returns the asset as-is, without any compression
# whatsoever. It is useful in development mode, when compression isn't
# needed but using the same asset pipeline as production is desired.
class NullCompressor #:nodoc:
def compress(content)
content
end
end

# An asset compressor which only initializes the underlying compression
# engine when needed.
#
# This postpones the initialization of the compressor until
# <code>#compress</code> is called the first time.
class LazyCompressor #:nodoc:
# Initializes a new LazyCompressor.
#
# The block should return a compressor when called, i.e. an object
# which responds to <code>#compress</code>.
def initialize(&block)
@block = block
end

def compress(content)
compressor.compress(content)
end

private

def compressor
@compressor ||= (@block.call || NullCompressor.new)
end
end
end
9 changes: 5 additions & 4 deletions static_compiler.rb → lib/sprockets/static_compiler.rb
Expand Up @@ -8,12 +8,13 @@ def initialize(env, target, paths, options = {})
@env = env
@target = target
@paths = paths
@digest = options.key?(:digest) ? options.delete(:digest) : true
@manifest = options.key?(:manifest) ? options.delete(:manifest) : true
@digest = options.fetch(:digest, true)
@manifest = options.fetch(:manifest, true)
@manifest_path = options.delete(:manifest_path) || target
@zip_files = options.delete(:zip_files) || /\.(?:css|html|js|svg|txt|xml)$/
end

def compile(base = nil, relative = nil)
def compile
manifest = {}
env.each_logical_path do |logical_path|
next unless compile_path?(logical_path)
Expand All @@ -36,7 +37,7 @@ def write_asset(asset)
filename = File.join(target, path)
FileUtils.mkdir_p File.dirname(filename)
asset.write_to(filename)
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
asset.write_to("#{filename}.gz") if filename.to_s =~ @zip_files
end
end

Expand Down
Empty file removed lib/sprockets_watch.rb
Empty file.
Empty file removed sprockets_watch.gemspec
Empty file.
3 changes: 0 additions & 3 deletions test/css/_include.css

This file was deleted.

1 change: 0 additions & 1 deletion test/css/global.css

This file was deleted.

3 changes: 0 additions & 3 deletions test/js/_fun.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/js/all.js

This file was deleted.

Empty file removed test/js/application.js
Empty file.
2 changes: 0 additions & 2 deletions test/js/sub-folder/_sub.js

This file was deleted.

1 change: 0 additions & 1 deletion test/js/sub-folder/grady.js

This file was deleted.

10 changes: 0 additions & 10 deletions test/js/test.js

This file was deleted.

1 change: 0 additions & 1 deletion test/js/test.txt

This file was deleted.

0 comments on commit 2cc965e

Please sign in to comment.