Class: Jekyll::Minify::Minifier

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-minify.rb

Overview

Minifies HTML, CSS, and JS files in the Jekyll destination directory.

Runs as a Jekyll post-write hook. Processes all eligible files after the site build completes. Files that fail minification are preserved unchanged.

Examples:

Disable minification in development

# _config-dev.yml
with_minify: false

Override minify-html options for production

# _config.yml
with_minify: true
with_minify_data:
  remove_bangs: false
  minify_css: false

Constant Summary collapse

DEFAULT_MINIFY_OPTIONS =

Default options passed to the minify-html gem. User overrides from with_minify_data in _config.yml are merged over these.

Returns:

  • (Hash{Symbol => Boolean})
{
  keep_html_and_head_opening_tags: true,
  keep_closing_tags: true,
  minify_css: true,
  minify_js: true,
  remove_bangs: true,
  remove_processing_instructions: true,
  ensure_spec_compliant_unquoted_attribute_values: true
}.freeze
ASSET_TYPES =

Asset types processed by the minifier, in order. Each entry has :type (Symbol) and :display_name (String) for logging.

Returns:

  • (Array<Hash>)
[
  { type: :html, display_name: 'HTML' },
  { type: :css,  display_name: 'CSS'  },
  { type: :js,   display_name: 'JS'   }
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMinifier

Initializes a new Minifier with empty statistics and a monotonic start time.



125
126
127
128
129
130
131
132
133
134
# File 'lib/jekyll-minify.rb', line 125

def initialize
  @minify_options = DEFAULT_MINIFY_OPTIONS
  @stats = {
    html: { count: 0, original_bytes: 0, minified_bytes: 0 },
    css: { count: 0, original_bytes: 0, minified_bytes: 0 },
    js: { count: 0, original_bytes: 0, minified_bytes: 0 },
    errors: []
  }
  @start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Class Method Details

.minify_site(site) ⇒ void

This method returns an undefined value.

Minifies all eligible files in the site destination directory.

Parameters:

  • site (Jekyll::Site)

    the Jekyll site object



140
141
142
# File 'lib/jekyll-minify.rb', line 140

def self.minify_site(site)
  new.run(site)
end

Instance Method Details

#run(site) ⇒ Hash?

Runs the minification pipeline for the given site. Reads configuration from site.config:

  • with_minify (Boolean, defaults to true): enable/disable the plugin
  • with_minify_data (Hash, optional): override minify-html options

Returns immediately if with_minify is false or the destination directory does not exist.

Parameters:

  • site (Jekyll::Site)

    the Jekyll site object providing dest and config

Returns:

  • (Hash, nil)

    the run's @stats, or nil if minification was skipped



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/jekyll-minify.rb', line 153

def run(site)
  unless plugin_enabled?(site.config)
    Jekyll.logger.info 'jekyll-minify:', 'Minification disabled (with_minify: false).'
    return
  end

  @minify_options = build_minify_options(site.config)

  dest = Pathname.new(site.dest)
  return unless dest.directory?

  Jekyll.logger.info 'jekyll-minify:', 'Starting minification...'

  minify_each_file(dest)
  log_statistics
  @stats
end