Skip to content

Commit

Permalink
Use terser gem, factor out asset hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
carstene1ns committed Apr 6, 2024
1 parent 995e4ca commit 1cf94e6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -139,7 +142,6 @@ GEM
nokogiri (~> 1.6)
rexml (~> 3.2)
webrick (1.8.1)
yuicompressor (1.3.3)
zeitwerk (2.6.13)

PLATFORMS
Expand All @@ -157,9 +159,9 @@ DEPENDENCIES
rouge
ruby-xxHash
slim
terser
typogruby
w3c_validators
yuicompressor

RUBY VERSION
ruby 3.0.5p211
Expand Down
29 changes: 13 additions & 16 deletions Rules
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env ruby

require 'ruby-xxhash'

preprocess do
write_sitemap_file
end
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/asset_hash.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1cf94e6

Please sign in to comment.