Permalink
Browse files

re-organizing and cleaning up things to prepare for a working compass…

… extension instead of the Rakefile.
  • Loading branch information...
1 parent 24c7a7e commit 2cc965efcb968a1a40dd43d658f1e7191308ccd2 @heygrady committed Mar 20, 2012
View
@@ -1,9 +0,0 @@
-source 'http://rubygems.org'
-
-gem 'rake'
-gem 'coffee-script'
-gem 'uglifier'
-gem 'yui-compressor'
-gem 'sass'
-gem 'fssm'
-gem 'sprockets', :git => 'git://github.com/sstephenson/sprockets.git'
View
118 Rakefile
@@ -1,118 +0,0 @@
-require "rake/sprocketstask"
-require "./static_compiler.rb"
-
-# @see http://guides.rubyonrails.org/asset_pipeline.html
-@paths = ["css", "js", "scss"]
-
-# @see https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/rails/railtie.rb#L77
-def expand_js_compressor(sym)
- case sym
- when :closure
- require 'closure-compiler'
- Closure::Compiler.new
- when :uglifier
- require 'uglifier'
- Uglifier.new
- when :yui
- require 'yui/compressor'
- YUI::JavaScriptCompressor.new
- else
- sym
- end
-end
-
-# @see https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/rails/railtie.rb#L93
-def expand_css_compressor(sym)
- case sym
- when :yui
- require 'yui/compressor'
- YUI::CssCompressor.new
- else
- sym
- end
-end
-
-# @see https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/environment.rb#L44
-def get_exts
- # look for all the files that Sprockets can handle
- @engines = Sprockets.engines
- exts = ['js', 'css']
- @engines.each do |ext, klass|
- exts += [ext[1..-1]]
- end
- return exts
-end
-
-def compile(base)
- # look for all the files that Sprockets can handle
- precompile = [/application.(css|js)$/]
- exts = get_exts
- precompile += [ Regexp.new( "^([\\w-]+\\/)?[^_]\\w+\\.(" + exts.join("|")+")$" ) ]
-
- # set up the env
- @env = Sprockets::Environment.new(".") do |env|
- #@paths.each do |path|
- # env.append_path(path)
- #end
- env.append_path(base)
-
- # @see https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/rails/railtie.rb#L69
- env.js_compressor = expand_js_compressor(:uglifier)
- #env.css_compressor = expand_css_compressor(:yui)
- end
-
- compiler = Sprockets::StaticCompiler.new(
- @env,
- './assets/' + base,
- precompile,
- :manifest_path => './' + base,
- :digest => false,
- :manifest => true)
-
- compiler.compile
-end
-
-task :precompile do
- @paths.each do |path|
- compile(path)
- end
-end
-
-# @see https://gist.github.com/222571
-task :watch, :src_folder do |t, args|
- require "fssm"
- srcFolder = args.src_folder.nil? ? "." : args.src_folder
-
- puts ">>> Watching #{srcFolder} for changes. Press Ctrl-C to Stop."
- #compile
-
- # @see https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/environment.rb#L44
- # @see https://github.com/chriseppstein/compass/blob/stable/lib/compass/commands/watch_project.rb#L98
- FSSM.monitor do |monitor|
- @paths.each do |load_path|
- compile(load_path)
- load_path = "./" + load_path
- next unless File.directory?(load_path)
- monitor.path load_path do |path|
- path.glob '**/*.{' + get_exts.join(',') +'}'
-
- path.update do |base, relative|
- puts ">>> Change detected to: #{relative}"
- compile(File.basename(base))
- end
- path.create do |base, relative|
- puts ">>> New file detected: #{relative}"
- compile(File.basename(base))
- end
- path.delete do |base, relative|
- puts ">>> File Removed: #{relative}"
- compile(File.basename(base))
- end
- end
- end
- end
-
- FSSM.monitor(srcFolder, "**/*.js") do
- update {|base, relative| update_file(relative)}
- end
-end
View
@@ -1,3 +0,0 @@
-#!/usr/bin/env ruby
-
-require "sprockets_watch"
View
@@ -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
View
@@ -0,0 +1,5 @@
+require "sprockets"
+require "sprockets/compressors"
+require "sprockets/static_compiler"
+require "compass/sprockets"
+
View
@@ -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
@@ -0,0 +1,6 @@
+module Compass
+ module Sprockets
+ VERSION = "0.0.0"
+ #HTML5_BOILERPLATE_VERSION = "3.0.2"
+ end
+end
@@ -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
@@ -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)
@@ -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
View
No changes.
View
No changes.
View
@@ -1,3 +0,0 @@
-body {
- color: green;
-}
View
@@ -1 +0,0 @@
-/* = require _include */
View
@@ -1,3 +0,0 @@
-//= require sub-folder/_sub
-var fun = 'awesome';
-var amazing = "cookie";
View
@@ -1,3 +0,0 @@
-//= require test
-hello = "awesome";
-var butt = 3;
View
No changes.
@@ -1,2 +0,0 @@
-var sub = 'sub-sub';
-var ting = "thingy";
@@ -1 +0,0 @@
-var grady = 'hello';
View
@@ -1,10 +0,0 @@
-//= require _fun
-var hello = 6;
-function stink() {
- return null;
-}
-function grady() {
- if (true) {
- return null;
- }
-}
View
@@ -1 +0,0 @@
-Fun!

0 comments on commit 2cc965e

Please sign in to comment.