Class: Middleman::Extensions::MinifyJavaScript

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

Overview

Minify JavaScript Extension

Constant Summary collapse

INLINE_JS_REGEX =
/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/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) ⇒ MinifyJavaScript

Returns a new instance of MinifyJavaScript


17
18
19
20
21
22
23
24
# File 'middleman-core/lib/middleman-core/extensions/minify_javascript.rb', line 17

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)

40
41
42
# File 'middleman-core/lib/middleman-core/extensions/minify_javascript.rb', line 40

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

#manipulate_resource_list_container!(resource_list) ⇒ Object


27
28
29
30
31
32
33
34
35
36
# File 'middleman-core/lib/middleman-core/extensions/minify_javascript.rb', line 27

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)

47
48
49
# File 'middleman-core/lib/middleman-core/extensions/minify_javascript.rb', line 47

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

#minifiable_inline?(content_type) ⇒ Boolean

Returns:

  • (Boolean)

54
55
56
# File 'middleman-core/lib/middleman-core/extensions/minify_javascript.rb', line 54

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

#minify(content) ⇒ Object


61
62
63
64
65
66
# File 'middleman-core/lib/middleman-core/extensions/minify_javascript.rb', line 61

def minify(content)
  @compressor.compress(content)
rescue ::ExecJS::ProgramError => e
  warn "WARNING: Couldn't compress JavaScript in #{@path}: #{e.message}"
  content
end

#minify_inline(content) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'middleman-core/lib/middleman-core/extensions/minify_javascript.rb', line 71

def minify_inline(content)
  content.gsub(INLINE_JS_REGEX) do |match|
    first = Regexp.last_match(1)
    inline_content = Regexp.last_match(2)
    last = Regexp.last_match(3)

    # Only compress script tags that contain JavaScript (as opposed to
    # something like jQuery templates, identified with a "text/html" type).
    if !first.include?('type=') || first.include?('text/javascript')
      first + minify(inline_content) + last
    else
      match
    end
  end
end