Class: Condenser::Asset
- Inherits:
-
Object
- Object
- Condenser::Asset
- Includes:
- EncodingUtils
- Defined in:
- lib/condenser/asset.rb
Constant Summary
Constants included from EncodingUtils
EncodingUtils::BOM, EncodingUtils::CHARSET_SIZE, EncodingUtils::CHARSET_START
Instance Attribute Summary collapse
-
#content_types ⇒ Object
readonly
Returns the value of attribute content_types.
-
#content_types_digest ⇒ Object
readonly
Returns the value of attribute content_types_digest.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#exports ⇒ Object
readonly
Returns the value of attribute exports.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#imports ⇒ Object
Returns the value of attribute imports.
-
#linked_assets ⇒ Object
readonly
Returns the value of attribute linked_assets.
-
#processed ⇒ Object
Returns the value of attribute processed.
- #source ⇒ Object
-
#source_file ⇒ Object
readonly
Returns the value of attribute source_file.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
- #sourcemap ⇒ Object
Instance Method Summary collapse
- #all_dependenies(deps, visited, meth, &block) ⇒ Object
- #all_export_dependencies ⇒ Object
- #all_process_dependencies ⇒ Object
- #basepath ⇒ Object
- #cache_key ⇒ Object
- #charset ⇒ Object
- #content_type ⇒ Object
- #digest ⇒ Object
-
#eql?(other) ⇒ Boolean
(also: #==)
Public: Compare assets.
- #export ⇒ Object
- #export_cache_version ⇒ Object
- #export_dependencies ⇒ Object
- #ext ⇒ Object
- #has_default_export? ⇒ Boolean
- #has_exports? ⇒ Boolean
-
#hexdigest ⇒ Object
(also: #etag)
Public: Returns String hexdigest of source.
-
#initialize(env, attributes = {}) ⇒ Asset
constructor
A new instance of Asset.
- #inspect ⇒ Object
- #integrity ⇒ Object
- #length ⇒ Object (also: #size)
- #load_processors ⇒ Object
- #needs_reexporting! ⇒ Object
- #needs_reprocessing! ⇒ Object
- #path ⇒ Object
- #process ⇒ Object
- #process_cache_version ⇒ Object
- #process_dependencies ⇒ Object
- #restat! ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
- #write(output_directory) ⇒ Object
Methods included from EncodingUtils
#detect, #detect_css, #detect_html, #detect_unicode, #detect_unicode_bom, #scan_css_charset
Constructor Details
#initialize(env, attributes = {}) ⇒ Asset
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/condenser/asset.rb', line 18 def initialize(env, attributes={}) @environment = env @filename = attributes[:filename] @content_types = Array(attributes[:content_types] || attributes[:content_type]) @content_types_digest = Digest::SHA1.base64digest(@content_types.join(':')) @source_file = attributes[:source_file] @source_path = attributes[:source_path] @linked_assets = Set.new @process_dependencies = Set.new @export_dependencies = Set.new @default_export = nil @exports = nil @processed = false @pcv = nil @export = nil @ecv = nil @processors_loaded = false @processors = Set.new end |
Instance Attribute Details
#content_types ⇒ Object (readonly)
Returns the value of attribute content_types.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def content_types @content_types end |
#content_types_digest ⇒ Object (readonly)
Returns the value of attribute content_types_digest.
13 14 15 |
# File 'lib/condenser/asset.rb', line 13 def content_types_digest @content_types_digest end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def environment @environment end |
#exports ⇒ Object (readonly)
Returns the value of attribute exports.
13 14 15 |
# File 'lib/condenser/asset.rb', line 13 def exports @exports end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def filename @filename end |
#imports ⇒ Object
Returns the value of attribute imports.
16 17 18 |
# File 'lib/condenser/asset.rb', line 16 def imports @imports end |
#linked_assets ⇒ Object (readonly)
Returns the value of attribute linked_assets.
13 14 15 |
# File 'lib/condenser/asset.rb', line 13 def linked_assets @linked_assets end |
#processed ⇒ Object
Returns the value of attribute processed.
16 17 18 |
# File 'lib/condenser/asset.rb', line 16 def processed @processed end |
#source ⇒ Object
371 372 373 374 |
# File 'lib/condenser/asset.rb', line 371 def source process @source end |
#source_file ⇒ Object (readonly)
Returns the value of attribute source_file.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def source_file @source_file end |
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def source_path @source_path end |
#sourcemap ⇒ Object
376 377 378 379 |
# File 'lib/condenser/asset.rb', line 376 def sourcemap process @sourcemap end |
Instance Method Details
#all_dependenies(deps, visited, meth, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/condenser/asset.rb', line 116 def all_dependenies(deps, visited, meth, &block) deps.each do |dep| if !visited.include?(dep.source_file) visited << dep.source_file block.call(dep) all_dependenies(dep.send(meth), visited, meth, &block) end end end |
#all_export_dependencies ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/condenser/asset.rb', line 134 def all_export_dependencies f = [@source_file] all_dependenies(export_dependencies, Set.new, :export_dependencies) do |dep| f << dep.source_file end f end |
#all_process_dependencies ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/condenser/asset.rb', line 126 def all_process_dependencies f = [@source_file] all_dependenies(process_dependencies, Set.new, :process_dependencies) do |dep| f << dep.source_file end f end |
#basepath ⇒ Object
49 50 51 52 |
# File 'lib/condenser/asset.rb', line 49 def basepath dirname, basename, extensions, mime_types = @environment.decompose_path(filename) [dirname, basename].compact.join('/') end |
#cache_key ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/condenser/asset.rb', line 142 def cache_key @cache_key ||= Digest::SHA1.base64digest(JSON.generate([ Condenser::VERSION, @environment.pipline_digest, @environment.base ? @source_file.delete_prefix(@environment.base) : @source_file, Digest::SHA256.file(@source_file).hexdigest, @content_types_digest ])) end |
#charset ⇒ Object
392 393 394 |
# File 'lib/condenser/asset.rb', line 392 def charset @source.encoding.name.downcase end |
#content_type ⇒ Object
45 46 47 |
# File 'lib/condenser/asset.rb', line 45 def content_type @content_types.last end |
#digest ⇒ Object
387 388 389 390 |
# File 'lib/condenser/asset.rb', line 387 def digest process @digest end |
#eql?(other) ⇒ Boolean Also known as: ==
Public: Compare assets.
Assets are equal if they share the same path and digest.
Returns true or false.
428 429 430 |
# File 'lib/condenser/asset.rb', line 428 def eql?(other) self.class == other.class && self.filename == other.filename && self.content_types == other.content_types end |
#export ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/condenser/asset.rb', line 317 def export return @export if @export @export = @environment.build do data = @environment.cache.fetch_if(Proc.new {"export/#{cache_key}/#{export_cache_version}"}, "export-deps/#{cache_key}") do process dirname, basename, extensions, mime_types = @environment.decompose_path(@filename) data = { source: @source.dup, source_file: @source_file, filename: @filename.dup, content_types: @content_types, sourcemap: nil, linked_assets: [], process_dependencies: [], export_dependencies: [] } if @environment.exporters.has_key?(content_type) @environment.exporters[content_type].each do |exporter| @environment.logger.info { "Exporting #{self.filename} with #{exporter.name}" } exporter.call(@environment, data) end end if minifier = @environment.minifier_for(content_type) @environment.logger.info { "Minifing #{self.filename} with #{minifier.name}" } minifier.call(@environment, data) end data[:digest] = @environment.digestor.digest(data[:source]) data[:digest_name] = @environment.digestor.name.sub(/^.*::/, '').downcase data end if @environment.build_cache.listening # TODO we could skip file and all their depencies here if they are # already in build_cache.@export_dependencies all_export_dependencies.each do |sf| @environment.build_cache.instance_variable_get(:@export_dependencies)[sf]&.add(self) end end Export.new(@environment, data) end end |
#export_cache_version ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/condenser/asset.rb', line 166 def export_cache_version return @ecv if @ecv f = [] all_dependenies(export_dependencies, Set.new, :export_dependencies) do |dep| f << [ @environment.base ? dep.source_file.delete_prefix(@environment.base) : dep.source_file, Digest::SHA256.file(dep.source_file).hexdigest ] end @ecv = Digest::SHA1.base64digest(JSON.generate(f)) end |
#export_dependencies ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/condenser/asset.rb', line 81 def export_dependencies deps = @environment.cache.fetch "export-deps/#{cache_key}" do process @export_dependencies + @process_dependencies end d = [] deps.each do |i| i = [i, @content_types] if i.is_a?(String) @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset| d << asset end end d end |
#ext ⇒ Object
419 420 421 |
# File 'lib/condenser/asset.rb', line 419 def ext File.extname(filename) end |
#has_default_export? ⇒ Boolean
97 98 99 100 |
# File 'lib/condenser/asset.rb', line 97 def has_default_export? process @default_export end |
#has_exports? ⇒ Boolean
102 103 104 105 |
# File 'lib/condenser/asset.rb', line 102 def has_exports? process @exports end |
#hexdigest ⇒ Object Also known as: etag
Public: Returns String hexdigest of source.
397 398 399 400 |
# File 'lib/condenser/asset.rb', line 397 def hexdigest process @digest.unpack('H*'.freeze).first end |
#inspect ⇒ Object
58 59 60 61 62 63 |
# File 'lib/condenser/asset.rb', line 58 def inspect dirname, basename, extensions, mime_types = @environment.decompose_path(@filename) " #<\#{self.class.name}#\#{self.object_id} @filename=\#{@filename} @content_types=\#{@content_types.inspect} @source_file=\#{@source_file} @source_mime_types=\#{mime_types.inspect}>\n TEXT\nend\n" |
#integrity ⇒ Object
403 404 405 406 |
# File 'lib/condenser/asset.rb', line 403 def integrity process "#{@digest_name}-#{[@digest].pack('m0')}" end |
#length ⇒ Object Also known as: size
381 382 383 384 |
# File 'lib/condenser/asset.rb', line 381 def length process @source.bytesize end |
#load_processors ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/condenser/asset.rb', line 107 def load_processors return if @processors_loaded @processors_loaded = true process @processors.map! { |p| p.is_a?(String) ? p.constantize : p } @environment.load_processors(*@processors) end |
#needs_reexporting! ⇒ Object
187 188 189 190 191 |
# File 'lib/condenser/asset.rb', line 187 def needs_reexporting! restat! @export = nil @ecv = nil end |
#needs_reprocessing! ⇒ Object
180 181 182 183 184 185 |
# File 'lib/condenser/asset.rb', line 180 def needs_reprocessing! @processed = false @pcv = nil @cache_key = nil needs_reexporting! end |
#path ⇒ Object
41 42 43 |
# File 'lib/condenser/asset.rb', line 41 def path filename.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" } end |
#process ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/condenser/asset.rb', line 193 def process return if @processed result = @environment.build do @environment.cache.fetch_if(Proc.new {"process/#{cache_key}/#{process_cache_version}"}, "direct-deps/#{cache_key}") do @source = File.binread(@source_file) dirname, basename, extensions, mime_types = @environment.decompose_path(@source_file) data = { source: @source, source_file: @source_file, filename: @filename.dup, content_type: mime_types, map: nil, linked_assets: Set.new, process_dependencies: Set.new, export_dependencies: Set.new, processors: Set.new } while @environment.templates.has_key?(data[:content_type].last) templator = @environment.templates[data[:content_type].pop] templator_klass = (templator.is_a?(Class) ? templator : templator.class) data[:processors] << templator_klass.name @environment.load_processors(templator_klass) templator.call(@environment, data) data[:filename] = data[:filename].gsub(/\.#{extensions.last}$/, '') end case @environment.mime_types[data[:content_type].last][:charset] when :unicode detect_unicode(data[:source]) when :css detect_css(data[:source]) when :html detect_html(data[:source]) else detect(data[:source]) if mime_types.last.start_with?('text/') end if @environment.preprocessors.has_key?(data[:content_type].last) @environment.preprocessors[data[:content_type].last].each do |processor| processor_klass = (processor.is_a?(Class) ? processor : processor.class) data[:processors] << processor_klass.name @environment.load_processors(processor_klass) @environment.logger.info { "Pre Processing #{self.filename} with #{processor.name}" } processor.call(@environment, data) end end if data[:content_type].last != @content_types.last && @environment.transformers.has_key?(data[:content_type].last) from_mime_type = data[:content_type].pop @environment.transformers[from_mime_type].each do |to_mime_type, processor| processor_klass = (processor.is_a?(Class) ? processor : processor.class) data[:processors] << processor_klass.name @environment.load_processors(processor_klass) @environment.logger.info { "Transforming #{self.filename} from #{from_mime_type} to #{to_mime_type} with #{processor.name}" } processor.call(@environment, data) data[:content_type] << to_mime_type end end if @environment.postprocessors.has_key?(data[:content_type].last) @environment.postprocessors[data[:content_type].last].each do |processor| processor_klass = (processor.is_a?(Class) ? processor : processor.class) data[:processors] << processor_klass.name @environment.load_processors(processor_klass) @environment.logger.info { "Post Processing #{self.filename} with #{processor.name}" } processor.call(@environment, data) end end if mime_types != @content_types raise ContentTypeMismatch, "mime type(s) \"#{@content_types.join(', ')}\" does not match requested mime type(s) \"#{data[:mime_types].join(', ')}\"" end data[:digest] = @environment.digestor.digest(data[:source]) data[:digest_name] = @environment.digestor.name.sub(/^.*::/, '').downcase # Do this here and at the end so cache_key can be calculated if we # run this block @source = data[:source] @sourcemap = data[:map] @filename = data[:filename] @content_types = data[:content_type] @digest = data[:digest] @digest_name = data[:digest_name] @linked_assets = Set.new(data[:linked_assets]) @process_dependencies = Set.new(data[:process_dependencies]) @export_dependencies = Set.new(data[:export_dependencies]) @default_export = data[:default_export] @exports = data[:exports] @processors = data[:processors] @processors_loaded = true @processed = true data end end @source = result[:source] @sourcemap = result[:map] @filename = result[:filename] @content_types = result[:content_type] @digest = result[:digest] @digest_name = result[:digest_name] @linked_assets = Set.new(result[:linked_assets]) @process_dependencies = Set.new(result[:process_dependencies]) @export_dependencies = Set.new(result[:export_dependencies]) @default_export = result[:default_export] @exports = result[:exports] @processors = result[:processors] load_processors @processed = true end |
#process_cache_version ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/condenser/asset.rb', line 152 def process_cache_version return @pcv if @pcv f = [] all_dependenies(process_dependencies, Set.new, :process_dependencies) do |dep| f << [ @environment.base ? dep.source_file.delete_prefix(@environment.base) : dep.source_file, Digest::SHA256.file(dep.source_file).hexdigest ] end @pcv = Digest::SHA1.base64digest(JSON.generate(f)) end |
#process_dependencies ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/condenser/asset.rb', line 65 def process_dependencies deps = @environment.cache.fetch "direct-deps/#{cache_key}" do process @process_dependencies end d = [] deps.each do |i| i = [i, @content_types] if i.is_a?(String) @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset| d << asset end end d end |
#restat! ⇒ Object
54 55 56 |
# File 'lib/condenser/asset.rb', line 54 def restat! @stat = nil end |
#to_json ⇒ Object
408 409 410 |
# File 'lib/condenser/asset.rb', line 408 def to_json { path: path, digest: hexdigest, size: size, integrity: integrity } end |
#to_s ⇒ Object
366 367 368 369 |
# File 'lib/condenser/asset.rb', line 366 def to_s process @source end |
#write(output_directory) ⇒ Object
412 413 414 415 416 417 |
# File 'lib/condenser/asset.rb', line 412 def write(output_directory) files = @environment.writers_for_mime_type(content_type).map do |writer| writer.call(output_directory, self) end files.flatten.compact end |