Class: Sprockets::Environment

Inherits:
Base
  • Object
show all
Defined in:
lib/sprockets/environment.rb

Instance Attribute Summary

Attributes inherited from Base

#cache, #context_class, #default_external_encoding, #digest_class, #logger, #version

Instance Method Summary collapse

Methods inherited from Base

#[], #attributes_for, #content_type_of, #digest, #each_entry, #each_file, #each_logical_path, #entries, #file_digest, #inspect, #stat

Methods included from Caching

#cache_get, #cache_set

Methods included from Processing

#bundle_processors, #css_compressor, #css_compressor=, #format_extensions, #js_compressor, #js_compressor=, #postprocessors, #preprocessors, #processors, #register_bundle_processor, #register_engine, #register_mime_type, #register_postprocessor, #register_preprocessor, #register_processor, #unregister_bundle_processor, #unregister_postprocessor, #unregister_preprocessor, #unregister_processor

Methods included from Engines

#engine_extensions, #engines, #register_engine

Methods included from Mime

#encoding_for_mime_type, #extension_for_mime_type, #mime_types, #register_mime_type

Methods included from Server

#call

Methods included from Trail

#append_path, #clear_paths, #extensions, #paths, #prepend_path, #resolve, #root

Constructor Details

#initialize(root = ".") {|_self| ... } ⇒ Environment

‘Environment` should initialized with your application’s root directory. This should be the same as your Rails or Rack root.

env = Environment.new(Rails.root)

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sprockets/environment.rb', line 20

def initialize(root = ".")
  @trail = Hike::Trail.new(root)

  self.logger = Logger.new($stderr)
  self.logger.level = Logger::FATAL

  if respond_to?(:default_external_encoding)
    self.default_external_encoding = Encoding::UTF_8
  end

  # Create a safe `Context` subclass to mutate
  @context_class = Class.new(Context)

  # Set MD5 as the default digest
  require 'digest/md5'
  @digest_class = ::Digest::MD5
  @version = ''

  @mime_types        = {}
  @engines           = Sprockets.engines
  @preprocessors     = Hash.new { |h, k| h[k] = [] }
  @postprocessors    = Hash.new { |h, k| h[k] = [] }
  @bundle_processors = Hash.new { |h, k| h[k] = [] }

  @engines.each do |ext, klass|
    add_engine_to_trail(ext, klass)
  end

  register_mime_type 'text/css', '.css'
  register_mime_type 'application/javascript', '.js'

  register_preprocessor 'text/css', DirectiveProcessor
  register_preprocessor 'application/javascript', DirectiveProcessor

  register_postprocessor 'application/javascript', SafetyColons
  register_bundle_processor 'text/css', CharsetNormalizer

  expire_index!

  yield self if block_given?
end

Instance Method Details

#find_asset(path, options = {}) ⇒ Object

Cache ‘find_asset` calls



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sprockets/environment.rb', line 72

def find_asset(path, options = {})
  options[:bundle] = true unless options.key?(:bundle)

  # Ensure inmemory cached assets are still fresh on every lookup
  if (asset = @assets[cache_key_for(path, options)]) && asset.fresh?(self)
    asset
  elsif asset = index.find_asset(path, options)
    # Cache is pushed upstream by Index#find_asset
    asset
  end
end

#indexObject

Returns a cached version of the environment.

All its file system calls are cached which makes ‘index` much faster. This behavior is ideal in production since the file system only changes between deploys.



67
68
69
# File 'lib/sprockets/environment.rb', line 67

def index
  Index.new(self)
end