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(visited = Set.new) ⇒ Object
- #all_process_dependencies(visited = Set.new) ⇒ 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
-
#normalize_filename_base(source_filename) ⇒ Object
Remove Enviroment base if it exists.
- #normialize_dependency_names(deps) ⇒ 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
Returns a new instance of 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
402 403 404 405 |
# File 'lib/condenser/asset.rb', line 402 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
407 408 409 410 |
# File 'lib/condenser/asset.rb', line 407 def sourcemap process @sourcemap end |
Instance Method Details
#all_dependenies(deps, visited, meth, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/condenser/asset.rb', line 114 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(visited = Set.new) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/condenser/asset.rb', line 137 def all_export_dependencies(visited = Set.new) f = [] if !visited.include?(@source_file) f << @source_file visited << self.source_file end all_dependenies(export_dependencies, visited, :export_dependencies) do |dep| f << dep.source_file end f end |
#all_process_dependencies(visited = Set.new) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/condenser/asset.rb', line 124 def all_process_dependencies(visited = Set.new) f = [] if !visited.include?(@source_file) f << @source_file visited << self.source_file end all_dependenies(process_dependencies, visited, :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
150 151 152 153 154 155 156 157 158 |
# File 'lib/condenser/asset.rb', line 150 def cache_key @cache_key ||= Digest::SHA1.base64digest(JSON.generate([ Condenser::VERSION, @environment.pipline_digest, normalize_filename_base(@source_file), Digest::SHA256.file(@source_file).hexdigest, @content_types_digest ])) end |
#charset ⇒ Object
423 424 425 |
# File 'lib/condenser/asset.rb', line 423 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
418 419 420 421 |
# File 'lib/condenser/asset.rb', line 418 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.
459 460 461 |
# File 'lib/condenser/asset.rb', line 459 def eql?(other) self.class == other.class && self.filename == other.filename && self.content_types == other.content_types end |
#export ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/condenser/asset.rb', line 348 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
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/condenser/asset.rb', line 184 def export_cache_version return @ecv if @ecv f = [] all_dependenies(export_dependencies, Set.new, :export_dependencies) do |dep| f << [ normalize_filename_base(dep.source_file), Digest::SHA256.file(dep.source_file).hexdigest ] end @ecv = Digest::SHA1.base64digest(JSON.generate(f)) end |
#export_dependencies ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/condenser/asset.rb', line 80 def export_dependencies deps = @environment.cache.fetch "export-deps/#{cache_key}" do process (@export_dependencies + @process_dependencies).map { |fn| [normalize_filename_base(fn[0]), fn[1]] } end deps.inject([]) do |memo, i| i[0] = File.join(@environment.base, i[0].delete_prefix('!')) if i[0].start_with?('!') && @environment.base @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset| memo << asset end memo end end |
#ext ⇒ Object
450 451 452 |
# File 'lib/condenser/asset.rb', line 450 def ext File.extname(filename) end |
#has_default_export? ⇒ Boolean
95 96 97 98 |
# File 'lib/condenser/asset.rb', line 95 def has_default_export? process @default_export end |
#has_exports? ⇒ Boolean
100 101 102 103 |
# File 'lib/condenser/asset.rb', line 100 def has_exports? process @exports end |
#hexdigest ⇒ Object Also known as: etag
Public: Returns String hexdigest of source.
428 429 430 431 |
# File 'lib/condenser/asset.rb', line 428 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) <<-TEXT #<#{self.class.name}##{self.object_id} @filename=#{@filename} @content_types=#{@content_types.inspect} @source_file=#{@source_file} @source_mime_types=#{mime_types.inspect}> TEXT end |
#integrity ⇒ Object
434 435 436 437 |
# File 'lib/condenser/asset.rb', line 434 def integrity process "#{@digest_name}-#{[@digest].pack('m0')}" end |
#length ⇒ Object Also known as: size
412 413 414 415 |
# File 'lib/condenser/asset.rb', line 412 def length process @source.bytesize end |
#load_processors ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/condenser/asset.rb', line 105 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
205 206 207 208 209 |
# File 'lib/condenser/asset.rb', line 205 def needs_reexporting! restat! @export = nil @ecv = nil end |
#needs_reprocessing! ⇒ Object
198 199 200 201 202 203 |
# File 'lib/condenser/asset.rb', line 198 def needs_reprocessing! @processed = false @pcv = nil @cache_key = nil needs_reexporting! end |
#normalize_filename_base(source_filename) ⇒ Object
Remove Enviroment base if it exists. This allows two of the same repos in a different location to use the same cache (like capistrano deploys)
162 163 164 165 166 167 168 |
# File 'lib/condenser/asset.rb', line 162 def normalize_filename_base(source_filename) if @environment.base && source_filename.start_with?(@environment.base) '!'+source_filename.delete_prefix(@environment.base).delete_prefix(File::SEPARATOR) else source_filename end end |
#normialize_dependency_names(deps) ⇒ Object
337 338 339 340 341 342 343 344 345 346 |
# File 'lib/condenser/asset.rb', line 337 def normialize_dependency_names(deps) deps.map do |fn| if fn.is_a?(String) dirname, basename, extensions, mime_types = @environment.decompose_path(fn, source_file) [dirname ? File.join(dirname, basename) : basename, mime_types.empty? ? @content_types : mime_types] else fn end end end |
#path ⇒ Object
41 42 43 |
# File 'lib/condenser/asset.rb', line 41 def path filename.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" } end |
#process ⇒ Object
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/condenser/asset.rb', line 211 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 data[:process_dependencies] = normialize_dependency_names(data[:process_dependencies]) data[:export_dependencies] = normialize_dependency_names(data[:export_dependencies]) # 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
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/condenser/asset.rb', line 170 def process_cache_version return @pcv if @pcv f = [] all_dependenies(process_dependencies, Set.new, :process_dependencies) do |dep| f << [ normalize_filename_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 |
# File 'lib/condenser/asset.rb', line 65 def process_dependencies deps = @environment.cache.fetch "direct-deps/#{cache_key}" do process @process_dependencies.map { |fn| [normalize_filename_base(fn[0]), fn[1]] } end deps.inject([]) do |memo, i| i[0] = File.join(@environment.base, i[0].delete_prefix('!')) if i[0].start_with?('!') && @environment.base @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset| memo << asset end memo end end |
#restat! ⇒ Object
54 55 56 |
# File 'lib/condenser/asset.rb', line 54 def restat! @stat = nil end |
#to_json ⇒ Object
439 440 441 |
# File 'lib/condenser/asset.rb', line 439 def to_json { path: path, digest: hexdigest, size: size, integrity: integrity } end |
#to_s ⇒ Object
397 398 399 400 |
# File 'lib/condenser/asset.rb', line 397 def to_s process @source end |
#write(output_directory) ⇒ Object
443 444 445 446 447 448 |
# File 'lib/condenser/asset.rb', line 443 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 |