Skip to content

Commit 17d1ae9

Browse files
authored
添加 post_process 插件 (#445)
1 parent d7c2569 commit 17d1ae9

5 files changed

Lines changed: 90 additions & 14 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ gem "http_parser.rb", "0.8.0", :platforms => [:jruby]
3333

3434
# plugin dependencies
3535
gem "webp-ffi", "0.4.0" if ENV["ENABLE_WEBP_AUTO_CONVERSION"] == "true" || ENV["DRONE"] == "true"
36+
gem "mini_racer", "0.20.0" if ENV["ENABLE_EMBEDDED_V8"] == "true" || ENV["DRONE"] == "true"
37+
gem "terser", "1.2.7"

_config.yml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,6 @@ compress_html:
316316
ignore:
317317
envs: development
318318

319-
head_scripts:
320-
- /assets/js/settings.js
321-
- /assets/js/theme.js
322-
after_footer_scripts:
323-
- /assets/js/plugins/jquery.auto-redirect.js
324-
325319
# jekyll-feed
326320
feed:
327321
collections:
@@ -330,3 +324,29 @@ feed:
330324
- modpack
331325
- eula
332326
- multiplayer
327+
328+
post_process:
329+
terser:
330+
/assets/js/main.min.js:
331+
- /assets/js/settings.js
332+
- /assets/js/theme.js
333+
- /assets/js/meta.js
334+
- /assets/js/vendor/jquery/jquery-3.6.0.js
335+
- /assets/js/plugins/gumshoe.js
336+
- /assets/js/plugins/jquery.ba-throttle-debounce.js
337+
- /assets/js/plugins/jquery.fitvids.js
338+
- /assets/js/plugins/jquery.greedy-navigation.js
339+
- /assets/js/plugins/jquery.magnific-popup.js
340+
- /assets/js/plugins/smooth-scroll.js
341+
- /assets/js/plugins/jquery.auto-redirect.js
342+
- /assets/js/_main.js
343+
remove_dirs:
344+
- /assets/images/
345+
- /assets/js/plugins/
346+
- /assets/js/vendor/
347+
remove_files:
348+
- /assets/js/meta.js
349+
- /assets/js/_main.js
350+
- /assets/js/theme.js
351+
- /assets/js/settings.js
352+
- /assets/js/main.min.js.map

_layouts/document.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
{{ content }}
66

77
{% if page.author or page.contributors or jekyll.environment == 'production' and page.hits %}
8-
<script src="{{ '/assets/js/meta.js' | relative_url }}"></script>
98
<script>
10-
{%- if page.author %}appendMeta("{{ page.author }}", "fas fa-user-pen");{% endif -%}
11-
{%- for contributor in page.contributors %}appendMeta("{{ contributor }}", "fas fa-user-pen");{% endfor -%}
12-
{%- if jekyll.environment == 'production' and page.hits %}hits({{ page.url | absolute_url | jsonify }});{% endif -%}
9+
window.addEventListener("load", () => {
10+
{%- if page.author %}appendMeta("{{ page.author }}", "fas fa-user-pen");{% endif -%}
11+
{%- for contributor in page.contributors %}appendMeta("{{ contributor }}", "fas fa-user-pen");{% endfor -%}
12+
{%- if jekyll.environment == 'production' and page.hits %}hits({{ page.url | absolute_url | jsonify }});{% endif -%}
13+
});
1314
</script>
1415
{% endif %}

_layouts/settings.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ <h3 id="{{ name }}">{{ value.title }}</h3>
4646
{% endfor %}
4747

4848
<script>
49-
for (const settingItem of document.getElementsByClassName("setting-item")) {
50-
settingItem.addEventListener("change", ({ target }) => settings.set(target.name, target.value));
51-
settings.onChange(settingItem.name, (value) => settingItem.type === "radio" && (settingItem.checked = settingItem.value === value));
52-
}
49+
window.addEventListener("load", () => {
50+
for (const settingItem of document.getElementsByClassName("setting-item")) {
51+
settingItem.addEventListener("change", ({ target }) => settings.set(target.name, target.value));
52+
settings.onChange(settingItem.name, (value) => settingItem.type === "radio" && (settingItem.checked = settingItem.value === value));
53+
}
54+
});
5355
</script>
5456

5557
<style>.notice label input { display: inline }</style>

_plugins/post_process.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

0 commit comments

Comments
 (0)