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:
### `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.

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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