-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
65 lines (51 loc) · 1.3 KB
/
Rakefile
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
56
57
58
59
60
61
62
63
64
65
require 'fileutils'
require 'nokogiri'
require 'uglifier'
dev = "public"
dist = "dist"
def uglify(js_out, js_in)
compiled = Uglifier.compile(File.read(js_in))
File.open(js_out, 'w') do |f|
f.write compiled
end
end
desc "Copy static files to dist directory"
task :copy => Dir.glob("#{dev}/**/*") do |t|
FileUtils.cp_r "#{dev}/.", dist
end
desc "Minify javascript files"
task :minify => :copy do
Dir.glob("#{dist}/**/*.js").each do |js|
uglify(js, js)
end
end
desc "Combines scripts, stylesheets, and HTML files"
task :combine => :minify do
file = "#{dist}/follow_button.html"
html = File.read file
doc = Nokogiri::HTML html
head = doc.xpath("//head").first
doc.xpath("//link[@type='text/css']").each do |css|
href = "#{dist}/#{css['href']}"
style = Nokogiri::XML::Node.new "style", doc
style.content = File.read href
style['type'] = "text/css"
css.remove
head.add_child style
File.delete href
end
doc.xpath("//script[@type='text/javascript']").each do |js|
src = "#{dist}/#{js['src']}"
js.content = File.read src
js.remove_attribute 'src'
File.delete src
end
File.open(file, 'w') do |f|
f.write doc.to_html
end
end
desc "Remove generated files"
task :clean do
FileUtils.rm_rf Dir.glob("#{dist}/**/*")
end
task :default => [:combine]