add sprockets paths and enable yui compressor

This commit is contained in:
John Bintz 2012-08-13 14:25:18 -04:00
parent 88bf24a936
commit a9c866b05e
6 changed files with 38 additions and 6 deletions

View File

@ -17,6 +17,10 @@ This starts a server on localhost:8080. Hack away.
Prett simple DSL: Prett simple DSL:
### `append_path`
Add a load path to Sprockets.
### `middleware` ### `middleware`
Define a middleware stack. Define a middleware stack.
@ -27,5 +31,6 @@ This is the guts of a `Sinatra::Base` app.
### `compile` ### `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.

View File

@ -8,10 +8,18 @@ module Sprockets
class AppBuilder class AppBuilder
ASSISTANT_CONFIG_FILE = Pathname('assistant_config.rb') ASSISTANT_CONFIG_FILE = Pathname('assistant_config.rb')
attr_reader :paths
def initialize def initialize
@paths = []
instance_eval(ASSISTANT_CONFIG_FILE.read) instance_eval(ASSISTANT_CONFIG_FILE.read)
end end
def append_path(path)
@paths << path
end
def app(&block) def app(&block)
if block if block
@app = block @app = block
@ -42,7 +50,7 @@ module Sprockets
if block if block
@compile = block @compile = block
else else
Compiler.new(@compile).compile Compiler.new(@compile).compile(@paths)
end end
end end
end end

View File

@ -8,10 +8,13 @@ module Sprockets
source_root File.expand_path('../../../..', __FILE__) source_root File.expand_path('../../../..', __FILE__)
desc "server", "server" desc "server", "server"
method_options %w{port -p} => 8080
def server def server
require 'sprockets/assistant/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 end
desc "compile", "compile things" desc "compile", "compile things"

View File

@ -4,6 +4,7 @@ require 'pathname'
require 'sprockets/assistant/output' require 'sprockets/assistant/output'
require 'sprockets-sass' require 'sprockets-sass'
require 'sprockets/assistant/compass' require 'sprockets/assistant/compass'
require 'yui/compressor'
module Sprockets module Sprockets
module Assistant module Assistant
@ -17,7 +18,7 @@ module Sprockets
@target = DEFAULT_TARGET @target = DEFAULT_TARGET
end end
def compile def compile(paths)
::Compass.configuration do |c| ::Compass.configuration do |c|
c.output_style = :compressed c.output_style = :compressed
end end
@ -26,6 +27,8 @@ module Sprockets
@env.append_path('assets/javascripts') @env.append_path('assets/javascripts')
@env.append_path('assets/stylesheets') @env.append_path('assets/stylesheets')
paths.each { |path| @env.append_path(path) }
instance_eval(&@settings) instance_eval(&@settings)
end end
@ -33,8 +36,18 @@ module Sprockets
file_target = target.join(name) file_target = target.join(name)
say "#{name} => #{file_target}" say "#{name} => #{file_target}"
js_compressor = YUI::JavaScriptCompressor.new(:munge => true)
file_target.parent.mkpath 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 end
def target def target

View File

@ -29,7 +29,9 @@ module Sprockets
instance_eval(&_app_builder.middleware) instance_eval(&_app_builder.middleware)
map "/#{Sinatra::Sprockets.config.prefix}" do 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 end
run _app run _app

View File

@ -23,5 +23,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'compass' gem.add_dependency 'compass'
gem.add_dependency 'sprockets-vendor_gems' gem.add_dependency 'sprockets-vendor_gems'
gem.add_dependency 'sprockets-sass' gem.add_dependency 'sprockets-sass'
gem.add_dependency 'yui-compressor'
end end