From a9c866b05e3a8549cee615810778e3774f8588da Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 13 Aug 2012 14:25:18 -0400 Subject: [PATCH] add sprockets paths and enable yui compressor --- README.md | 7 ++++++- lib/sprockets/assistant/app_builder.rb | 10 +++++++++- lib/sprockets/assistant/cli.rb | 5 ++++- lib/sprockets/assistant/compiler.rb | 17 +++++++++++++++-- lib/sprockets/assistant/server.rb | 4 +++- sprockets-assistant.gemspec | 1 + 6 files changed, 38 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 47428c6..5dbecac 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,10 @@ This starts a server on localhost:8080. Hack away. Prett simple DSL: +### `append_path` + +Add a load path to Sprockets. + ### `middleware` Define a middleware stack. @@ -27,5 +31,6 @@ This is the guts of a `Sinatra::Base` app. ### `compile` -A list of files to compile with `sprockets-assistant compile`. +A list of files to compile with `sprockets-assistant compile`. YUI compressor is used to make the +JS files really small. diff --git a/lib/sprockets/assistant/app_builder.rb b/lib/sprockets/assistant/app_builder.rb index e2d2007..4bf6bbe 100644 --- a/lib/sprockets/assistant/app_builder.rb +++ b/lib/sprockets/assistant/app_builder.rb @@ -8,10 +8,18 @@ module Sprockets class AppBuilder ASSISTANT_CONFIG_FILE = Pathname('assistant_config.rb') + attr_reader :paths + def initialize + @paths = [] + instance_eval(ASSISTANT_CONFIG_FILE.read) end + def append_path(path) + @paths << path + end + def app(&block) if block @app = block @@ -42,7 +50,7 @@ module Sprockets if block @compile = block else - Compiler.new(@compile).compile + Compiler.new(@compile).compile(@paths) end end end diff --git a/lib/sprockets/assistant/cli.rb b/lib/sprockets/assistant/cli.rb index 6653f81..2e67f7a 100644 --- a/lib/sprockets/assistant/cli.rb +++ b/lib/sprockets/assistant/cli.rb @@ -8,10 +8,13 @@ module Sprockets source_root File.expand_path('../../../..', __FILE__) desc "server", "server" + method_options %w{port -p} => 8080 def server require 'sprockets/assistant/server' - Rack::Handler.default.run(Sprockets::Assistant::Server.app) + $stdout.sync = true + + Rack::Handler.default.run(Sprockets::Assistant::Server.app, :Port => options[:port]) end desc "compile", "compile things" diff --git a/lib/sprockets/assistant/compiler.rb b/lib/sprockets/assistant/compiler.rb index 3e48018..94a4622 100644 --- a/lib/sprockets/assistant/compiler.rb +++ b/lib/sprockets/assistant/compiler.rb @@ -4,6 +4,7 @@ require 'pathname' require 'sprockets/assistant/output' require 'sprockets-sass' require 'sprockets/assistant/compass' +require 'yui/compressor' module Sprockets module Assistant @@ -17,7 +18,7 @@ module Sprockets @target = DEFAULT_TARGET end - def compile + def compile(paths) ::Compass.configuration do |c| c.output_style = :compressed end @@ -26,6 +27,8 @@ module Sprockets @env.append_path('assets/javascripts') @env.append_path('assets/stylesheets') + paths.each { |path| @env.append_path(path) } + instance_eval(&@settings) end @@ -33,8 +36,18 @@ module Sprockets file_target = target.join(name) say "#{name} => #{file_target}" + js_compressor = YUI::JavaScriptCompressor.new(:munge => true) + file_target.parent.mkpath - target.join(name).open('w') { |fh| fh.print @env[name].to_s } + target.join(name).open('w') { |fh| + output = @env[name].to_s + + if File.extname(name) == '.js' + output = js_compressor.compress(output) + end + + fh.print output + } end def target diff --git a/lib/sprockets/assistant/server.rb b/lib/sprockets/assistant/server.rb index 5447704..4f91ba5 100644 --- a/lib/sprockets/assistant/server.rb +++ b/lib/sprockets/assistant/server.rb @@ -29,7 +29,9 @@ module Sprockets instance_eval(&_app_builder.middleware) map "/#{Sinatra::Sprockets.config.prefix}" do - run Sinatra::Sprockets.environment + env = Sinatra::Sprockets.environment + _app_builder.paths.each { |path| env.append_path(path) } + run env end run _app diff --git a/sprockets-assistant.gemspec b/sprockets-assistant.gemspec index 4770c82..b83b86f 100644 --- a/sprockets-assistant.gemspec +++ b/sprockets-assistant.gemspec @@ -23,5 +23,6 @@ Gem::Specification.new do |gem| gem.add_dependency 'compass' gem.add_dependency 'sprockets-vendor_gems' gem.add_dependency 'sprockets-sass' + gem.add_dependency 'yui-compressor' end