Sha256: 7cb0910aa97df5d0283c93e6cfce9f7a402a1dfe143c9e4d0263a0e04fb06ba3

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Middleman::Features::MinifyJavascript
  class << self
    def registered(app)
      require 'uglifier'
      app.after_configuration do
        set :js_compressor, ::Uglifier.new
      end
      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

        uncompressed_source = case(response)
          when String
            response
          when Array
            response.join
          when Rack::Response
            response.body.join
          when Rack::File
            File.read(response.path)
        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

1 entries across 1 versions & 1 rubygems

Version Path
middleman-3.0.0.alpha.2 lib/middleman/features/minify_javascript.rb