Class: Jekyll::Uglify::UglifyJS

Inherits:
Command
  • Object
show all
Defined in:
lib/jekyll/uglify.rb

Overview

Uglify given JS files or directories. Ignores files with ‘.min.js` extensions. Gets Uglifier configuration settings from `_config.yml`

Syntax: jekyll uglify FILEPATH [ADDITIONAL_FILEPATHS] [or]

bundle exec jekyll uglify FILEPATH [ADDITIONAL_FILEPATHS]

Arguments:

FILEPATH  path to file or directory to be uglified

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



19
20
21
22
23
24
25
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
# File 'lib/jekyll/uglify.rb', line 19

def self.init_with_program(prog)
  prog.command(:uglify) do |c|
    c.syntax "uglify FILEPATH [ADDITIONAL_FILEPATHS]"
    c.description 'Uglifies given JS files or directories. Ignores minified files.'

    c.action do |args, options|
      config_raw = Jekyll.configuration()["jekyll-uglify"]
      config = Hash.new
      config_raw.each do |k,v|
        if v.is_a?(Hash)
          tmp = Hash.new
          v.each do |ik,iv|
            tmp[ik.to_sym] = iv
          end
          config[k.to_sym] = tmp
        else
          config[k.to_sym] = v
        end
      end
      args.each do |dir|
        if !(dir.start_with? "/") then dir = "/" + dir end
        if ((dir.end_with? ".js") && !(dir.end_with? ".min.js"))
          filepath = Dir.pwd + dir
          output = Uglifier.compile(File.open(filepath, 'r'), config)
          File.open(filepath, 'w').write(output)
          next
        end
        if !(dir.end_with? "/") then dir = dir + "/" end
        Dir.foreach(Dir.pwd + dir) do |file|
          next if file == '.' or file == '..' or file.end_with? '.min.js'
          next unless file.end_with? '.js'
          filepath = Dir.pwd + dir + file
          output = Uglifier.compile(File.open(filepath, 'r'), config)
          File.open(filepath, 'w').write(output)
        end
      end
    end
  end
end