Class: Palimpsest::Assets
- Inherits:
-
Object
- Object
- Palimpsest::Assets
- Defined in:
- lib/palimpsest/assets.rb
Overview
Flexible asset pipeline using Sprockets.
Paths are loaded into a Sprockets::Environment
(relative to #directory if given).
Asset tags are used in source code and replaced
with generated asset path or compiled source if inline
is used.
For example, if type is set to :javascripts
the following replacements would be made:
[% javascript app %] -> app-9413c7f112033f0c6f2a8e8dd313399c18d93878.js
[% javascript lib/jquery %] -> lib/jquery-e2a8cde3f5b3cdb011e38a673556c7a94729e0d1.js
[% javascript inline tracking %] -> <compiled source of tracking.js asset>
Constant Summary collapse
- DEFAULT_OPTIONS =
Default #options.
{ # default path to output all saved assets (relative to directory) output: '', # assume assets will be served under this url # e.g. `https://cdn.example.com/` cdn: '', # keyword to use in asset tag for inline assets inline: 'inline', # if true, use sprockets-image_compressor with pngcrush and jpegoptim image_compression: false, # if true, also generate a gzipped asset gzip: false, # include hash in asset name hash: true, # opening and closing brackets for asset source tags src_pre: '[%', src_post: '%]', # allowed options for `Sprockets::Environment` sprockets_options: [:js_compressor, :css_compressor] }
Instance Attribute Summary collapse
-
#directory ⇒ String
Directory which all paths will be relative to if set.
-
#paths ⇒ Array
Paths to load into sprockets environment.
-
#type ⇒ Symbol
Type of asset.
Class Method Summary collapse
-
.find_tags(path, type = nil, options = {}) ⇒ Object
Scans all non-binary files under
path
for asset tags.
Instance Method Summary collapse
- #assets ⇒ Sprockets::Environment
-
#find_tags(path: directory) ⇒ Array
Scans all non-binary files under
path
(#directory by default) for asset tags. -
#initialize(directory: '', options: {}, paths: {}) ⇒ Assets
constructor
A new instance of Assets.
-
#load_options ⇒ Object
Load options into the sprockets environment.
-
#load_paths ⇒ Object
Load paths into the sprockets environment.
-
#options(options = {}) ⇒ Hash
Uses DEFAULT_OPTIONS as initial value.
-
#sprockets ⇒ Sprockets::Environment
The current sprockets environment.
-
#update_source(source) ⇒ String
Replaces all asset tags in source string with asset path or asset source.
-
#update_source!(source) ⇒ String
Replaces all asset tags in source string with asset path or asset source.
-
#write(target, gzip: , hash: ) ⇒ String?
Write a target asset to file with a hashed name.
Constructor Details
#initialize(directory: '', options: {}, paths: {}) ⇒ Assets
Returns a new instance of Assets.
59 60 61 62 63 |
# File 'lib/palimpsest/assets.rb', line 59 def initialize directory: '', options: {}, paths: {} self. self.directory = directory self.paths = paths end |
Instance Attribute Details
#directory ⇒ String
Returns directory which all paths will be relative to if set.
57 58 59 |
# File 'lib/palimpsest/assets.rb', line 57 def directory @directory end |
#paths ⇒ Array
Returns paths to load into sprockets environment.
57 |
# File 'lib/palimpsest/assets.rb', line 57 attr_accessor :directory, :paths, :type |
#type ⇒ Symbol
Returns type of asset.
57 |
# File 'lib/palimpsest/assets.rb', line 57 attr_accessor :directory, :paths, :type |
Class Method Details
.find_tags(path, type = nil, options = {}) ⇒ Object
Scans all non-binary files under path
for asset tags.
(see #find_tags)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/palimpsest/assets.rb', line 175 def self. path, type=nil, ={} fail ArgumentError, 'path cannot be empty' if path.empty? = DEFAULT_OPTIONS.merge pre = Regexp.escape [:src_pre] post= Regexp.escape [:src_post] cmd = ['grep'] cmd.concat %w(-l -I -r -E) cmd << if type.nil? pre + '(.*?)' + post else pre + '\s+' + type.to_s + '\s+(.*?)' + post end cmd << path files = [] Open3.capture2(*cmd).first.each_line { |l| files << l.chomp unless l.empty? } files end |
Instance Method Details
#assets ⇒ Sprockets::Environment
102 103 104 105 106 107 108 109 |
# File 'lib/palimpsest/assets.rb', line 102 def assets unless @loaded load_paths end @loaded = true sprockets end |
#find_tags(path: directory) ⇒ Array
Scans all non-binary files under path
(#directory by default) for asset tags.
Uses current asset #type (if set) and #options.
166 167 168 |
# File 'lib/palimpsest/assets.rb', line 166 def path: directory self.class. path, type, end |
#load_options ⇒ Object
Load options into the sprockets environment. Values are loaded from #options.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/palimpsest/assets.rb', line 80 def [:sprockets_options].each do |opt| sprockets.send "#{opt}=".to_sym, [opt] if [opt] end if [:image_compression] Sprockets::ImageCompressor::Integration.setup sprockets end self end |
#load_paths ⇒ Object
Load paths into the sprockets environment. Values are loaded from #paths.
94 95 96 97 98 99 |
# File 'lib/palimpsest/assets.rb', line 94 def load_paths paths.each do |path| sprockets.append_path "#{directory + '/' unless directory.empty?}#{path}" end self end |
#options(options = {}) ⇒ Hash
Uses DEFAULT_OPTIONS as initial value.
68 69 70 71 |
# File 'lib/palimpsest/assets.rb', line 68 def ={} @options ||= DEFAULT_OPTIONS @options = @options.merge end |
#sprockets ⇒ Sprockets::Environment
Returns the current sprockets environment.
74 75 76 |
# File 'lib/palimpsest/assets.rb', line 74 def sprockets @sprockets ||= Sprockets::Environment.new end |
#update_source(source) ⇒ String
Replaces all asset tags in source string with asset path or asset source. Writes any referenced assets to disk.
156 157 158 159 160 |
# File 'lib/palimpsest/assets.rb', line 156 def update_source source s = source update_source! s s end |
#update_source!(source) ⇒ String
this modifies the source
String
in place
Replaces all asset tags in source string with asset path or asset source. Writes any referenced assets to disk.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/palimpsest/assets.rb', line 134 def update_source! source # e.g. /\[%\s+javascript\s+((\S+)\s?(\S+))\s+%\]/ regex = /#{Regexp.escape [:src_pre]}\s+#{type.to_s.singularize}\s+((\S+)\s?(\S+))\s+#{Regexp.escape [:src_post]}/ source.gsub! regex do if $2 == [:inline] assets[$3].to_s else asset = write $1 # @todo raise warning or error if asset not found p "asset not found: #{$1}" and next if asset.nil? [:cdn].empty? ? asset : [:cdn] + asset end end return true end |
#write(target, gzip: , hash: ) ⇒ String?
Write a target asset to file with a hashed name.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/palimpsest/assets.rb', line 116 def write target, gzip: [:gzip], hash: [:hash] asset = assets[target] return if asset.nil? name = hash ? asset.digest_path : asset.logical_path.to_s name = "#{[:output]}/#{name}" unless [:output].empty? path = directory.empty? ? '' : "#{directory}/" path << name asset.write_to "#{path}.gz", compress: true if gzip asset.write_to path name end |