From cf574800c86601463c71408dcc7abf38c44a51ac Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sun, 10 Jul 2011 16:02:52 -0700 Subject: [PATCH] Sprockets v2 support for JS --- features/sprockets.feature | 6 ++ .../source/javascripts/sprockets_base.js | 5 ++ .../source/javascripts/sprockets_sub.js | 3 + lib/middleman/core_extensions/sprockets.rb | 50 +++++++++++----- lib/middleman/features/minify_javascript.rb | 41 +++++++++++-- .../features/minify_javascript/rack.rb | 57 ------------------- 6 files changed, 86 insertions(+), 76 deletions(-) create mode 100644 features/sprockets.feature create mode 100644 fixtures/test-app/source/javascripts/sprockets_base.js create mode 100644 fixtures/test-app/source/javascripts/sprockets_sub.js delete mode 100644 lib/middleman/features/minify_javascript/rack.rb diff --git a/features/sprockets.feature b/features/sprockets.feature new file mode 100644 index 00000000..cf9bafb0 --- /dev/null +++ b/features/sprockets.feature @@ -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" \ No newline at end of file diff --git a/fixtures/test-app/source/javascripts/sprockets_base.js b/fixtures/test-app/source/javascripts/sprockets_base.js new file mode 100644 index 00000000..fc657dc0 --- /dev/null +++ b/fixtures/test-app/source/javascripts/sprockets_base.js @@ -0,0 +1,5 @@ +//= require "sprockets_sub" + +function base() { + +} \ No newline at end of file diff --git a/fixtures/test-app/source/javascripts/sprockets_sub.js b/fixtures/test-app/source/javascripts/sprockets_sub.js new file mode 100644 index 00000000..1b27ea83 --- /dev/null +++ b/fixtures/test-app/source/javascripts/sprockets_sub.js @@ -0,0 +1,3 @@ +function sprockets_sub_function() { + +} \ No newline at end of file diff --git a/lib/middleman/core_extensions/sprockets.rb b/lib/middleman/core_extensions/sprockets.rb index d8e27dc2..aebe398e 100644 --- a/lib/middleman/core_extensions/sprockets.rb +++ b/lib/middleman/core_extensions/sprockets.rb @@ -3,27 +3,49 @@ require "sprockets" module Middleman::CoreExtensions::Sprockets class << self def registered(app) - # app.map '/assets' do - # run ::Sprockets::Environment.new + app.set :js_compressor, false + + app.map "/#{app.js_dir}" do + run JavascriptEnvironment.new(app) + end + # app.map "/#{app.css_dir}" do + # run StylesheetEnvironment.new(app) # end end alias :included :registered end - - class Environment < Sprockets::Environment - - # Pass in the project you want the pipeline to manage. - def initialize(app, mode = :debug) - # Views/ ? - super app.root - # Disable css for now - # unregister_processor "text/css", Sprockets::DirectiveProcessor + class MiddlemanEnvironment < Sprockets::Environment + def initialize(app) + super File.expand_path(app.views) + end + end + + class JavascriptEnvironment < MiddlemanEnvironment + def initialize(app) + super + + # Disable css + unregister_processor "text/css", ::Sprockets::DirectiveProcessor + + self.js_compressor = app.settings.js_compressor # configure search paths - # append_path File.dirname project_path - # append_path File.join project_path, 'assets' + javascripts_path = File.join(File.expand_path(app.views), app.js_dir) + append_path javascripts_path 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 \ No newline at end of file diff --git a/lib/middleman/features/minify_javascript.rb b/lib/middleman/features/minify_javascript.rb index 1b364829..d49cb0aa 100755 --- a/lib/middleman/features/minify_javascript.rb +++ b/lib/middleman/features/minify_javascript.rb @@ -1,12 +1,43 @@ module Middleman::Features::MinifyJavascript class << self def registered(app) - # Only do minification on build or prod mode - return unless app.build? || app.production? - - require "middleman/features/minify_javascript/rack" - app.use Middleman::Rack::MinifyJavascript + require 'uglifier' + app.set :js_compressor, ::Uglifier.new + app.use InlineJavascriptRack end alias :included :registered 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(/()/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 \ No newline at end of file diff --git a/lib/middleman/features/minify_javascript/rack.rb b/lib/middleman/features/minify_javascript/rack.rb deleted file mode 100644 index bc29066c..00000000 --- a/lib/middleman/features/minify_javascript/rack.rb +++ /dev/null @@ -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(/()/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