-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcache_buster_filter.rb
42 lines (38 loc) · 1.24 KB
/
cache_buster_filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Rake::Pipeline::Web::Filters
# A filter that inserts a cache-busting key into the outputted file name.
#
# @example
# !!!ruby
# Rake::Pipeline.build do
# input "app/assets"
# output "public"
#
# filter Rake::Pipeline::Web::Filters::CacheBusterFilter
# end
class CacheBusterFilter < Rake::Pipeline::Filter
# @return [Proc] the default cache key generator, which
# takes the MD5 hash of the input's file name and contents.
DEFAULT_KEY_GENERATOR = proc { |input|
require 'digest/md5'
Digest::MD5.new << input.path << input.read
}
# @param [Proc] key_generator a block to use as the Filter's method of
# turning input file wrappers into cache keys; defaults to
# +DEFAULT_KEY_GENERATOR+
def initialize(&key_generator)
key_generator ||= DEFAULT_KEY_GENERATOR
output_name_generator = proc { |path, file|
parts = path.split('.')
index_to_modify = parts.length > 1 ? -2 : -1
parts[index_to_modify] << "-#{key_generator.call(file)}"
parts.join('.')
}
super(&output_name_generator)
end
def generate_output(inputs, output)
inputs.each do |input|
output.write input.read
end
end
end
end