Class: Middleman::Extensions::MinifyCss

Inherits:
Middleman::Extension show all
Defined in:
middleman-core/lib/middleman-core/extensions/minify_css.rb

Overview

Minify CSS Extension

Defined Under Namespace

Classes: SassCompressor

Constant Summary collapse

INLINE_CSS_REGEX =
/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m.freeze

Constants included from Contracts

Contracts::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

Instance Attribute Summary

Attributes inherited from Middleman::Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Middleman::Extension

activated_extension, #add_exposed_to_context, #after_build, #after_configuration, after_extension_activated, #after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, #manipulate_resource_list, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block) ⇒ MinifyCss

Returns a new instance of MinifyCss



32
33
34
35
36
37
38
39
# File 'middleman-core/lib/middleman-core/extensions/minify_css.rb', line 32

def initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block)
  super

  @ignore = Array(options.ignore) + [/\.min\./]
  @compressor = options.compressor
  @compressor = @compressor.to_proc if @compressor.respond_to? :to_proc
  @compressor = @compressor.call if @compressor.is_a? Proc
end

Instance Method Details

#ignore?(path) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'middleman-core/lib/middleman-core/extensions/minify_css.rb', line 55

def ignore?(path)
  @ignore.any? { |ignore| ::Middleman::Util.path_match(ignore, path) }
end

#manipulate_resource_list_container!(resource_list) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'middleman-core/lib/middleman-core/extensions/minify_css.rb', line 42

def manipulate_resource_list_container!(resource_list)
  resource_list.by_binary(false).each do |r|
    type = r.content_type.try(:slice, /^[^;]*/)
    if options[:inline] && minifiable_inline?(type)
      r.add_filter method(:minify_inline)
    elsif minifiable?(type) && !ignore?(r.destination_path)
      r.add_filter method(:minify)
    end
  end
end

#minifiable?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'middleman-core/lib/middleman-core/extensions/minify_css.rb', line 62

def minifiable?(content_type)
  options[:content_types].include?(content_type)
end

#minifiable_inline?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'middleman-core/lib/middleman-core/extensions/minify_css.rb', line 69

def minifiable_inline?(content_type)
  options[:inline_content_types].include?(content_type)
end

#minify(content) ⇒ Object



76
77
78
# File 'middleman-core/lib/middleman-core/extensions/minify_css.rb', line 76

def minify(content)
  @compressor.compress(content)
end

#minify_inline(content) ⇒ String

Detect and minify inline content

Parameters:

  • content (String)

Returns:

  • (String)


84
85
86
87
88
# File 'middleman-core/lib/middleman-core/extensions/minify_css.rb', line 84

def minify_inline(content)
  content.gsub(INLINE_CSS_REGEX) do
    Regexp.last_match(1) + minify(Regexp.last_match(2)) + Regexp.last_match(3)
  end
end