Sha256: a5d265e410ace6914268f07d58c9f56c7ec9073531d3a1c4cb5612de1ae09d54

Contents?: true

Size: 1.15 KB

Versions: 31

Compression:

Stored size: 1.15 KB

Contents

module Middleman::Features::MinifyJavascript
  class << self
    def registered(app)
      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(/(<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

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
middleman-2.0.14.pre lib/middleman/features/minify_javascript.rb
middleman-2.0.13.1-x86-mingw32 lib/middleman/features/minify_javascript.rb
middleman-2.0.13.1 lib/middleman/features/minify_javascript.rb
middleman-2.0.13-x86-mingw32 lib/middleman/features/minify_javascript.rb
middleman-2.0.13 lib/middleman/features/minify_javascript.rb
middleman-2.0.12-x86-mingw32 lib/middleman/features/minify_javascript.rb
middleman-2.0.12 lib/middleman/features/minify_javascript.rb
middleman-2.0.12.pre-x86-mingw32 lib/middleman/features/minify_javascript.rb
middleman-2.0.12.pre lib/middleman/features/minify_javascript.rb
middleman-2.0.11.pre-x86-mingw32 lib/middleman/features/minify_javascript.rb
middleman-2.0.11.pre lib/middleman/features/minify_javascript.rb