Class: Jekyll::MinifyJs

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

Overview

MinifyJs is a Jekyll ‘Generator` that minifies JavaScript files found under common asset directories. It prefers the `terser` Ruby gem when available and falls back to an external `terser` CLI when necessary.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(site) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jekyll-minify-js.rb', line 26

def self.run(site) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  # Support both repository-root layout and app/ layout.
  candidates = [
    File.join(site.source, 'assets', 'js'),
    File.join(site.source, 'app', 'assets', 'js')
  ]
  src_dir = candidates.find { |p| Dir.exist?(p) }
  return unless src_dir

  # Read configuration from _config.yml under `minify_js:` key
  cfg = site.config.fetch('minify_js', {})
  return if cfg.key?('enabled') && cfg['enabled'] == false

  dest_dir = File.join(site.dest, cfg.fetch('output_dir', 'assets/js'))
  FileUtils.mkdir_p(dest_dir)

  terser_available = defined?(Terser)

  # Build options from config
  compress_opt = cfg.fetch('compress', true)
  mangle_opt = cfg.fetch('mangle', true)
  source_map_enabled = cfg.fetch('source_map', true)
  exclude_patterns = cfg.fetch('exclude', ['**/*.min.js'])

  Dir.glob(File.join(src_dir, '**', '*.js')).each do |src| # rubocop:disable Metrics/BlockLength
    rel = Pathname.new(src).relative_path_from(Pathname.new(src_dir)).to_s
    out = File.join(dest_dir, rel)
    out_dir = File.dirname(out)
    FileUtils.mkdir_p(out_dir)
    map_name = "#{File.basename(out)}.map"

    # Skip excluded files
    next if exclude_patterns.any? { |pat| File.fnmatch(pat, rel) }

    if terser_available
      Jekyll.logger.info 'Terser:', "minifying #{rel} via terser Ruby gem"
      begin
        src_content = File.open(src, 'r:BOM|UTF-8', &:read)
        options = {}
        options[:compress] = compress_opt
        options[:mangle] = mangle_opt
        if source_map_enabled
          options[:source_map] =
            { filename: File.basename(src), output_filename: File.basename(out), sources_content: true,
              url: map_name }
          compiled, map = Terser.compile_with_map(src_content, options)
        else
          compiled = Terser.compile(src_content, options)
          map = nil
        end
        compiled += "\n//# sourceMappingURL=#{map_name}\n" unless compiled.include?('sourceMappingURL')
        File.write(out, compiled)
        File.write("#{out}.map", map) if map
      rescue StandardError => e
        Jekyll.logger.warn 'Terser:', "ruby terser failed for #{rel}: #{e.message}; copying original"
        FileUtils.cp(src, out)
      end
    else
      terser_cmd = new.detect_terser_cmd
      if terser_cmd
        cmd = %(#{terser_cmd} #{Shellwords.escape(src)} --compress --mangle -o #{Shellwords.escape(out)} --source-map "url='#{map_name}',includeSources") # rubocop:disable Layout/LineLength
        Jekyll.logger.info 'Terser:', "minifying #{rel} via external terser"
        success = system(cmd)
        unless success && File.exist?(out)
          Jekyll.logger.warn 'Terser:', "minify failed for #{rel}; copying original"
          FileUtils.cp(src, out)
        end
      else
        Jekyll.logger.warn 'Terser:', 'no terser available — copying original JS'
        FileUtils.cp(src, out)
      end
    end
  end
end

Instance Method Details

#detect_terser_cmdObject



101
102
103
104
105
106
107
# File 'lib/jekyll-minify-js.rb', line 101

def detect_terser_cmd
  # Prefer the bundler-installed terser (from the `terser` gem) via `bundle exec terser`.
  return 'bundle exec terser' if system('bundle exec terser --version > /dev/null 2>&1')
  return 'terser' if system('terser --version > /dev/null 2>&1')

  nil
end

#generate(_site) ⇒ Object



22
23
24
# File 'lib/jekyll-minify-js.rb', line 22

def generate(_site)
  # Work is done in the :post_write hook to avoid Jekyll overwriting outputs.
end