-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjsminify.rb
55 lines (45 loc) · 1.55 KB
/
jsminify.rb
1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# https://github.com/documentcloud/closure-compiler
require 'rubygems'
require 'closure-compiler'
module Jekyll
module JsMinify
class MinJsFile < Jekyll::StaticFile
# Minify JS
# +dest+ is the String path to the destination dir
#
# Returns false if the file was not modified since last time (no-op).
def write(dest)
dest_path = File.join(dest, @dir, @name)
return false if File.exist? dest and !modified?
@@mtimes[path] = mtime
FileUtils.mkdir_p(File.dirname(dest_path))
begin
content = File.read(path)
content = Closure::Compiler.new.compile(content)
File.open(dest_path, 'w') do |f|
f.write(content)
end
rescue => e
STDERR.puts "Closure Compiler Exception: #{e.message}"
end
true
end
end
class JsGenerator < Jekyll::Generator
safe true
# Jekyll will have already added the *.js files as Jekyll::StaticFile
# objects to the static_files array. Here we replace those with a
# MinJSFile object.
def generate(site)
site.static_files.clone.each do |sf|
if sf.kind_of?(Jekyll::StaticFile) && sf.path =~ /\.js$/ && !(sf.path =~ /\.min\.js$/)
site.static_files.delete(sf)
name = File.basename(sf.path)
destination = File.dirname(sf.path).sub(site.source, '')
site.static_files << MinJsFile.new(site, site.source, destination, name)
end
end
end
end
end
end