添加 post_process 插件 (#445)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
neveler 2026-05-21 09:38:54 +08:00 committed by GitHub
parent d7c2569844
commit 17d1ae9f05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 90 additions and 14 deletions

View File

@ -33,3 +33,5 @@ gem "http_parser.rb", "0.8.0", :platforms => [:jruby]
# plugin dependencies # plugin dependencies
gem "webp-ffi", "0.4.0" if ENV["ENABLE_WEBP_AUTO_CONVERSION"] == "true" || ENV["DRONE"] == "true" gem "webp-ffi", "0.4.0" if ENV["ENABLE_WEBP_AUTO_CONVERSION"] == "true" || ENV["DRONE"] == "true"
gem "mini_racer", "0.20.0" if ENV["ENABLE_EMBEDDED_V8"] == "true" || ENV["DRONE"] == "true"
gem "terser", "1.2.7"

View File

@ -316,12 +316,6 @@ compress_html:
ignore: ignore:
envs: development envs: development
head_scripts:
- /assets/js/settings.js
- /assets/js/theme.js
after_footer_scripts:
- /assets/js/plugins/jquery.auto-redirect.js
# jekyll-feed # jekyll-feed
feed: feed:
collections: collections:
@ -330,3 +324,29 @@ feed:
- modpack - modpack
- eula - eula
- multiplayer - multiplayer
post_process:
terser:
/assets/js/main.min.js:
- /assets/js/settings.js
- /assets/js/theme.js
- /assets/js/meta.js
- /assets/js/vendor/jquery/jquery-3.6.0.js
- /assets/js/plugins/gumshoe.js
- /assets/js/plugins/jquery.ba-throttle-debounce.js
- /assets/js/plugins/jquery.fitvids.js
- /assets/js/plugins/jquery.greedy-navigation.js
- /assets/js/plugins/jquery.magnific-popup.js
- /assets/js/plugins/smooth-scroll.js
- /assets/js/plugins/jquery.auto-redirect.js
- /assets/js/_main.js
remove_dirs:
- /assets/images/
- /assets/js/plugins/
- /assets/js/vendor/
remove_files:
- /assets/js/meta.js
- /assets/js/_main.js
- /assets/js/theme.js
- /assets/js/settings.js
- /assets/js/main.min.js.map

View File

@ -5,10 +5,11 @@ layout: single
{{ content }} {{ content }}
{% if page.author or page.contributors or jekyll.environment == 'production' and page.hits %} {% if page.author or page.contributors or jekyll.environment == 'production' and page.hits %}
<script src="{{ '/assets/js/meta.js' | relative_url }}"></script>
<script> <script>
{%- if page.author %}appendMeta("{{ page.author }}", "fas fa-user-pen");{% endif -%} window.addEventListener("load", () => {
{%- for contributor in page.contributors %}appendMeta("{{ contributor }}", "fas fa-user-pen");{% endfor -%} {%- if page.author %}appendMeta("{{ page.author }}", "fas fa-user-pen");{% endif -%}
{%- if jekyll.environment == 'production' and page.hits %}hits({{ page.url | absolute_url | jsonify }});{% endif -%} {%- for contributor in page.contributors %}appendMeta("{{ contributor }}", "fas fa-user-pen");{% endfor -%}
{%- if jekyll.environment == 'production' and page.hits %}hits({{ page.url | absolute_url | jsonify }});{% endif -%}
});
</script> </script>
{% endif %} {% endif %}

View File

@ -46,10 +46,12 @@ layout: single
{% endfor %} {% endfor %}
<script> <script>
for (const settingItem of document.getElementsByClassName("setting-item")) { window.addEventListener("load", () => {
settingItem.addEventListener("change", ({ target }) => settings.set(target.name, target.value)); for (const settingItem of document.getElementsByClassName("setting-item")) {
settings.onChange(settingItem.name, (value) => settingItem.type === "radio" && (settingItem.checked = settingItem.value === value)); settingItem.addEventListener("change", ({ target }) => settings.set(target.name, target.value));
} settings.onChange(settingItem.name, (value) => settingItem.type === "radio" && (settingItem.checked = settingItem.value === value));
}
});
</script> </script>
<style>.notice label input { display: inline }</style> <style>.notice label input { display: inline }</style>

51
_plugins/post_process.rb Normal file
View File

@ -0,0 +1,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