middleman/middleman-core/lib/middleman-core/extensions/minify_css.rb

119 lines
3.7 KiB
Ruby
Raw Normal View History

2014-07-03 04:04:34 +02:00
require 'middleman-core/contracts'
# Minify CSS Extension
class Middleman::Extensions::MinifyCss < ::Middleman::Extension
option :inline, false, 'Whether to minify CSS inline within HTML files'
option :ignore, [], 'Patterns to avoid minifying'
2014-04-29 19:50:21 +02:00
option :compressor, proc {
require 'sass'
SassCompressor
}, 'Set the CSS compressor to use.'
option :content_types, %w(text/css), 'Content types of resources that contain CSS'
option :inline_content_types, %w(text/html text/php), 'Content types of resources that contain inline CSS'
def ready
# Setup Rack middleware to minify CSS
2014-04-29 19:50:21 +02:00
app.use Rack, compressor: options[:compressor],
ignore: Array(options[:ignore]) + [/\.min\./],
inline: options[:inline],
content_types: options[:content_types],
inline_content_types: options[:inline_content_types]
end
class SassCompressor
2014-04-29 19:50:21 +02:00
def self.compress(style, options={})
root_node = ::Sass::SCSS::CssParser.new(style, 'middleman-css-input', 1).parse
2014-04-29 19:44:24 +02:00
root_node.options = options.merge(style: :compressed)
root_node.render.strip
end
end
# Rack middleware to look for CSS and compress it
class Rack
2014-07-03 04:04:34 +02:00
include Contracts
INLINE_CSS_REGEX = /(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m
# Init
# @param [Class] app
# @param [Hash] options
2016-01-14 20:21:42 +01:00
Contract RespondTo[:call], {
2014-07-10 21:35:47 +02:00
ignore: ArrayOf[PATH_MATCHER],
inline: Bool,
compressor: Or[Proc, RespondTo[:to_proc], RespondTo[:compress]]
2016-01-14 20:21:42 +01:00
} => Any
def initialize(app, options={})
@app = app
@ignore = options.fetch(:ignore)
@inline = options.fetch(:inline)
@compressor = options.fetch(:compressor)
@compressor = @compressor.to_proc if @compressor.respond_to? :to_proc
@compressor = @compressor.call if @compressor.is_a? Proc
@content_types = options[:content_types]
@inline_content_types = options[:inline_content_types]
end
# Rack interface
# @param [Rack::Environmemt] env
# @return [Array]
def call(env)
status, headers, response = @app.call(env)
content_type = headers['Content-Type'].try(:slice, /^[^;]*/)
path = env['PATH_INFO']
minified = if @inline && minifiable_inline?(content_type)
minify_inline(::Middleman::Util.extract_response_text(response))
elsif minifiable?(content_type) && !ignore?(path)
minify(::Middleman::Util.extract_response_text(response))
end
if minified
headers['Content-Length'] = ::Rack::Utils.bytesize(minified).to_s
response = [minified]
end
[status, headers, response]
end
2014-04-29 19:50:21 +02:00
private
2014-04-29 01:02:18 +02:00
# Whether the path should be ignored
# @param [String] path
# @return [Boolean]
def ignore?(path)
@ignore.any? { |ignore| Middleman::Util.path_match(ignore, path) }
end
# Whether this type of content can be minified
# @param [String, nil] content_type
# @return [Boolean]
def minifiable?(content_type)
@content_types.include?(content_type)
end
# Whether this type of content contains inline content that can be minified
# @param [String, nil] content_type
# @return [Boolean]
def minifiable_inline?(content_type)
@inline_content_types.include?(content_type)
end
# Minify the content
# @param [String] content
# @return [String]
def minify(content)
@compressor.compress(content)
end
# Detect and minify inline content
# @param [String] content
# @return [String]
def minify_inline(content)
content.gsub(INLINE_CSS_REGEX) do
$1 + minify($2) + $3
end
end
end
end