Class: Rawfeed::Build::Minifier

Inherits:
Object
  • Object
show all
Defined in:
lib/rawfeed/build/minifier.rb

Constant Summary collapse

BUILD_DIR =
"_site"

Class Method Summary collapse

Class Method Details

.minify_allObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rawfeed/build/minifier.rb', line 69

def self.minify_all
  puts "Starting minify process...".cyan
  begin
    minify_javascript
    minify_html
    minify_images
    puts "\n Minify completed successfully!\n".bold.green
  rescue => e
    puts "\n An error occurred during minify.\n".bold.red
    puts e.message.red
    exit 1
  end
end

.minify_htmlObject



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
# File 'lib/rawfeed/build/minifier.rb', line 42

def self.minify_html
  puts "Minifying HTML files...".yellow
  html_files = Dir.glob("#{BUILD_DIR}/**/*.html")

  if html_files.empty?
    puts "No HTML files found."
    return
  end

  begin
    html_files.each do |file|
      content = File.read(file)
      # Simple HTML minification: remove comments, collapse whitespace
      # Preserves whitespace inside <pre>, <code>, <textarea>, <svg>
      minified = content
        .gsub(/<!--.*?-->/m, "")
        .gsub(/(<(pre|code|textarea|svg)\b[^>]*>.*?<\/\2>)|(\s+)/mi) { $1 || " " }
        .strip
      File.write(file, minified)
    end
    puts "HTML minified successfully!".green
  rescue => e
    puts "Error minifying HTML: #{e.message}".red
    raise
  end
end

.minify_imagesObject



83
84
85
# File 'lib/rawfeed/build/minifier.rb', line 83

def self.minify_images
  ImageMinifier.minify_images
end

.minify_javascriptObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rawfeed/build/minifier.rb', line 11

def self.minify_javascript
  begin
    require "uglifier"
  rescue LoadError
    puts "[!] Error: uglifier gem is required for minify feature".red
    puts "    Install it with: gem install uglifier".yellow
    puts "    Or add to your Gemfile: gem 'uglifier', '~> 4.2.0'".yellow
    exit 1
  end

  puts "Minifying JavaScript files...".yellow
  js_files = Dir.glob("#{BUILD_DIR}/assets/js/**/*.js")

  if js_files.empty?
    puts "No JavaScript files found."
    return
  end

  begin
    js_files.each do |file|
      content = File.read(file)
      minified = Uglifier.compile(content, harmony: true)
      File.write(file, minified)
    end
    puts "JavaScript minified successfully!".green
  rescue => e
    puts "Error minifying JavaScript: #{e.message}".red
    raise
  end
end