-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpost_process.rb
More file actions
51 lines (44 loc) · 1.6 KB
/
post_process.rb
File metadata and controls
51 lines (44 loc) · 1.6 KB
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
require "terser"
ExecJS.runtime = ExecJS::Runtimes::MiniRacer if ExecJS::Runtimes::MiniRacer.available?
Jekyll::Hooks.register :site, :post_write do |site|
config = site.config["post_process"]
next unless config
terser = config["terser"]
if terser.is_a?(Hash)
terser.each do |terser_output, terser_inputs|
next unless terser_output.is_a?(String) && terser_inputs.is_a?(Array)
terser_codes = []
terser_inputs_all_exist = true
terser_inputs.each do |file|
destination = File.join(site.dest, file)
if File.exist?(destination)
terser_codes << File.read(destination, encoding: "UTF-8")
else
terser_inputs_all_exist = false
break
end
end
if terser_inputs_all_exist
destination = File.join(site.dest, terser_output.to_s)
File.write(destination, Terser.compile(terser_codes.join(";")))
Jekyll.logger.info "Post Process:", "terser #{terser_output}"
end
end
end
remove_files = config["remove_files"]
if remove_files.is_a?(Array)
remove_files.each do |file|
destination = File.join(site.dest, file)
File.delete(destination) if File.exist?(destination)
Jekyll.logger.info "Post Process:", "remove_files #{file}"
end
end
remove_dirs = config["remove_dirs"]
if remove_dirs.is_a?(Array)
remove_dirs.each do |dir|
destination = File.join(site.dest, dir)
FileUtils.rm_rf(destination) if File.directory?(destination)
Jekyll.logger.info "Post Process:", "remove_dirs #{dir}"
end
end
end