Sprockets v2 support for JS

This commit is contained in:
Thomas Reynolds 2011-07-10 16:02:52 -07:00
parent 142e6b9f24
commit cf574800c8
6 changed files with 86 additions and 76 deletions

View file

@ -0,0 +1,6 @@
Feature: Sprockets
Scenario: Sprockets require
Given the Server is running
When I go to "/javascripts/sprockets_base.js"
Then I should see "sprockets_sub_function"

View file

@ -0,0 +1,5 @@
//= require "sprockets_sub"
function base() {
}

View file

@ -0,0 +1,3 @@
function sprockets_sub_function() {
}

View file

@ -3,27 +3,49 @@ require "sprockets"
module Middleman::CoreExtensions::Sprockets module Middleman::CoreExtensions::Sprockets
class << self class << self
def registered(app) def registered(app)
# app.map '/assets' do app.set :js_compressor, false
# run ::Sprockets::Environment.new
app.map "/#{app.js_dir}" do
run JavascriptEnvironment.new(app)
end
# app.map "/#{app.css_dir}" do
# run StylesheetEnvironment.new(app)
# end # end
end end
alias :included :registered alias :included :registered
end end
class Environment < Sprockets::Environment class MiddlemanEnvironment < Sprockets::Environment
def initialize(app)
super File.expand_path(app.views)
end
end
# Pass in the project you want the pipeline to manage. class JavascriptEnvironment < MiddlemanEnvironment
def initialize(app, mode = :debug) def initialize(app)
# Views/ ? super
super app.root
# Disable css for now # Disable css
# unregister_processor "text/css", Sprockets::DirectiveProcessor unregister_processor "text/css", ::Sprockets::DirectiveProcessor
self.js_compressor = app.settings.js_compressor
# configure search paths # configure search paths
# append_path File.dirname project_path javascripts_path = File.join(File.expand_path(app.views), app.js_dir)
# append_path File.join project_path, 'assets' append_path javascripts_path
end end
end end
# class StylesheetEnvironment < MiddlemanEnvironment
# def initialize(app)
# super
#
# # Disable js
# unregister_processor "application/javascript", ::Sprockets::DirectiveProcessor
#
# # configure search paths
# stylesheets_path = File.join(File.expand_path(app.views), app.css_dir)
# append_path stylesheets_path
# end
# end
end end

View file

@ -1,12 +1,43 @@
module Middleman::Features::MinifyJavascript module Middleman::Features::MinifyJavascript
class << self class << self
def registered(app) def registered(app)
# Only do minification on build or prod mode require 'uglifier'
return unless app.build? || app.production? app.set :js_compressor, ::Uglifier.new
app.use InlineJavascriptRack
require "middleman/features/minify_javascript/rack"
app.use Middleman::Rack::MinifyJavascript
end end
alias :included :registered alias :included :registered
end end
class InlineJavascriptRack
def initialize(app, options={})
@app = app
end
def call(env)
status, headers, response = @app.call(env)
if env["PATH_INFO"].match(/\.html$/)
compressor = ::Uglifier.new
if response.is_a?(::Rack::File) or response.is_a?(::Sinatra::Helpers::StaticFile)
uncompressed_source = File.read(response.path)
else
uncompressed_source = response.join
end
minified = uncompressed_source.gsub(/(<scri.*?\/\/<!\[CDATA\[\n)(.*?)(\/\/\]\].*?<\/script>)/m) do |m|
first = $1
uncompressed_source = $2
last = $3
minified_js = compressor.compile(uncompressed_source)
first << minified_js << "\n" << last
end
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
response = [minified]
end
[status, headers, response]
end
end
end end

View file

@ -1,57 +0,0 @@
begin
require 'uglifier'
rescue LoadError
puts "UglifyJS not available. Install it with: gem install uglifier"
end
module Middleman
module Rack
class MinifyJavascript
def initialize(app, options={})
@app = app
end
def call(env)
status, headers, response = @app.call(env)
if env["PATH_INFO"].match(/\.js$/)
compressor = ::Uglifier.new
if response.is_a?(::Rack::File) or response.is_a?(Sinatra::Helpers::StaticFile)
uncompressed_source = File.read(response.path)
else
uncompressed_source = response.join
end
minified = compressor.compile(uncompressed_source)
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
response = [minified]
end
if env["PATH_INFO"].match(/\.html$/)
compressor = ::Uglifier.new
if response.is_a?(::Rack::File) or response.is_a?(Sinatra::Helpers::StaticFile)
uncompressed_source = File.read(response.path)
else
uncompressed_source = response.join
end
minified = uncompressed_source.gsub(/(<scri.*?\/\/<!\[CDATA\[\n)(.*?)(\/\/\]\].*?<\/script>)/m) do |m|
first = $1
uncompressed_source = $2
last = $3
minified_js = compressor.compile(uncompressed_source)
first << minified_js << "\n" << last
end
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
response = [minified]
end
[status, headers, response]
end
end
end
end