diff --git a/Gemfile b/Gemfile index 91a5da3..14c58a3 100644 --- a/Gemfile +++ b/Gemfile @@ -7,7 +7,7 @@ gem 'typogruby' # text enhancements gem 'rouge' # syntax highlighting gem 'slim' # templates gem 'builder' # xml sitemaps -gem 'yuicompressor' # css/js uglifying +gem 'terser' # js uglifying gem 'ruby-xxHash' # for cache busting group :nanoc do diff --git a/Gemfile.lock b/Gemfile.lock index 5bbe64b..7851cb5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -25,6 +25,7 @@ GEM eventmachine (>= 0.12.9) http_parser.rb (~> 0) eventmachine (1.2.7) + execjs (2.9.1) ffi (1.16.3) google-protobuf (3.25.3-x86_64-linux) http_parser.rb (0.8.0) @@ -126,6 +127,8 @@ GEM set (~> 1.0) stringio (3.1.0) temple (0.10.3) + terser (1.2.2) + execjs (>= 0.3.0, < 3) tilt (2.3.0) tty-color (0.6.0) tty-command (0.10.1) @@ -139,7 +142,6 @@ GEM nokogiri (~> 1.6) rexml (~> 3.2) webrick (1.8.1) - yuicompressor (1.3.3) zeitwerk (2.6.13) PLATFORMS @@ -157,9 +159,9 @@ DEPENDENCIES rouge ruby-xxHash slim + terser typogruby w3c_validators - yuicompressor RUBY VERSION ruby 3.0.5p211 diff --git a/Rules b/Rules index 965c86b..5fb49c4 100644 --- a/Rules +++ b/Rules @@ -1,7 +1,5 @@ #!/usr/bin/env ruby -require 'ruby-xxhash' - preprocess do write_sitemap_file end @@ -17,26 +15,25 @@ compile '/**/*.{html,md}' do filter :tidy if @config[:prod] end -compile '/{css/*.scss,js/*.js}' do - ext = "." + item.identifier.ext - if ext == '.scss' - filter :dart_sass, { - :syntax => :scss, - :style => (@config[:prod] ? :compressed : :expanded) - } - ext = ".css" +ignore '/css/{vendor,bits}/*' +compile '/css/*.scss' do + filter :dart_sass, :style => (@config[:prod] ? :compressed : :expanded) +end + +compile '/js/*.js' do + if @config[:prod] + filter :terser end +end - # compress and use a hash to avoid caching issues +route '/{js/*.js,css/*.scss}' do + ext = item.identifier.ext == 'scss' ? '.css' : '.' + item.identifier.ext if @config[:prod] - filter :yui_compressor, :type => :js if ext == ".js" - checksum = XXhash.xxh32(File.read(item[:filename]), 0xEA51) - write item.identifier.without_ext + '-' + checksum.to_s + ".min" + ext + item.identifier.without_ext + '-' + asset_hash(item) + '.min' + ext else - write item.identifier.without_ext + ext + item.identifier.without_ext + ext end end -ignore '/css/**/*' compile '/sitemap.xml' do filter :erb diff --git a/lib/asset_hash.rb b/lib/asset_hash.rb new file mode 100644 index 0000000..7956724 --- /dev/null +++ b/lib/asset_hash.rb @@ -0,0 +1,9 @@ + +# use a hash to avoid caching issues + +require 'ruby-xxhash' + +def asset_hash(item) + sum = XXhash.xxh32(File.read(@item[:filename]), 0xEA51) + return sum.to_s +end