|
1 | | -require "terser" |
2 | | - |
3 | | -ExecJS.runtime = ExecJS::Runtimes::MiniRacer if ExecJS::Runtimes::MiniRacer.available? |
4 | | - |
5 | | -Jekyll::Hooks.register :site, :post_write do |site| |
6 | | - config = site.config["post_process"] |
7 | | - next unless config |
8 | | - |
9 | | - terser = config["terser"] |
10 | | - if terser.is_a?(Hash) |
11 | | - terser.each do |terser_output, terser_inputs| |
12 | | - next unless terser_output.is_a?(String) && terser_inputs.is_a?(Array) |
13 | | - |
14 | | - terser_codes = [] |
15 | | - terser_inputs_all_exist = true |
16 | | - terser_inputs.each do |file| |
17 | | - destination = File.join(site.dest, file) |
18 | | - if File.exist?(destination) |
19 | | - terser_codes << File.read(destination, encoding: "UTF-8") |
20 | | - else |
21 | | - terser_inputs_all_exist = false |
22 | | - break |
23 | | - end |
24 | | - end |
25 | | - |
26 | | - if terser_inputs_all_exist |
27 | | - destination = File.join(site.dest, terser_output.to_s) |
28 | | - File.write(destination, Terser.compile(terser_codes.join(";"))) |
29 | | - Jekyll.logger.info "Post Process:", "terser #{terser_output}" |
30 | | - end |
31 | | - end |
32 | | - end |
33 | | - |
34 | | - remove_files = config["remove_files"] |
35 | | - if remove_files.is_a?(Array) |
36 | | - remove_files.each do |file| |
37 | | - destination = File.join(site.dest, file) |
38 | | - File.delete(destination) if File.exist?(destination) |
39 | | - Jekyll.logger.info "Post Process:", "remove_files #{file}" |
40 | | - end |
41 | | - end |
42 | | - |
43 | | - remove_dirs = config["remove_dirs"] |
44 | | - if remove_dirs.is_a?(Array) |
45 | | - remove_dirs.each do |dir| |
46 | | - destination = File.join(site.dest, dir) |
47 | | - FileUtils.rm_rf(destination) if File.directory?(destination) |
48 | | - Jekyll.logger.info "Post Process:", "remove_dirs #{dir}" |
49 | | - end |
50 | | - end |
51 | | -end |
| 1 | +Jekyll::Hooks.register :site, :post_write do |site| |
| 2 | + config = site.config["post_process"] |
| 3 | + next unless config |
| 4 | + |
| 5 | + cache = nil |
| 6 | + terser = config["terser"] |
| 7 | + if terser.is_a?(Hash) |
| 8 | + terser.each do |terser_output, terser_inputs| |
| 9 | + next unless terser_output.is_a?(String) && terser_inputs.is_a?(Array) |
| 10 | + |
| 11 | + cache = Jekyll::Cache.new("PostProcess") unless cache |
| 12 | + terser_codes = [] |
| 13 | + terser_inputs_all_exist = true |
| 14 | + terser_inputs.each do |file| |
| 15 | + destination = File.join(site.dest, file) |
| 16 | + if File.exist?(destination) |
| 17 | + terser_codes << File.read(destination, encoding: "UTF-8") |
| 18 | + else |
| 19 | + terser_inputs_all_exist = false |
| 20 | + break |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + if terser_inputs_all_exist |
| 25 | + destination = File.join(site.dest, terser_output.to_s) |
| 26 | + code = terser_codes.join(";") |
| 27 | + hash = Digest::SHA256.hexdigest(code) |
| 28 | + unless cache.key?("terser_#{hash}") |
| 29 | + cache["terser_#{hash}"] = Script.call("terser", code: code) |
| 30 | + end |
| 31 | + File.write(destination, cache["terser_#{hash}"]) |
| 32 | + Jekyll.logger.info "Post Process:", "[terser] #{terser_output}" |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + remove_files = config["remove_files"] |
| 38 | + if remove_files.is_a?(Array) |
| 39 | + remove_files.each do |file| |
| 40 | + destination = File.join(site.dest, file) |
| 41 | + File.delete(destination) if File.exist?(destination) |
| 42 | + Jekyll.logger.info "Post Process:", "[remove_files] #{file}" |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + remove_dirs = config["remove_dirs"] |
| 47 | + if remove_dirs.is_a?(Array) |
| 48 | + remove_dirs.each do |dir| |
| 49 | + destination = File.join(site.dest, dir) |
| 50 | + FileUtils.rm_rf(destination) if File.directory?(destination) |
| 51 | + Jekyll.logger.info "Post Process:", "[remove_dirs] #{dir}" |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # clean history jekyll cache |
| 56 | + jekyll_cache_dir = File.dirname(site.cache_dir) |
| 57 | + current_cache_name = File.basename(site.cache_dir) |
| 58 | + if File.basename(jekyll_cache_dir) == ".jekyll-cache" |
| 59 | + Dir.entries(jekyll_cache_dir).select do |entry| |
| 60 | + next if entry == "." || entry == ".." |
| 61 | + entry_path = File.join(jekyll_cache_dir, entry) |
| 62 | + if File.file?(entry_path) |
| 63 | + File.delete(entry_path) |
| 64 | + Jekyll.logger.info "Post Process:", "[clean_history_jekyll_cache] #{entry}" |
| 65 | + elsif File.directory?(entry_path) |
| 66 | + if entry < current_cache_name |
| 67 | + FileUtils.rm_rf(entry_path) |
| 68 | + Jekyll.logger.info "Post Process:", "[clean_history_jekyll_cache] #{entry}" |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | +end |
0 commit comments