Class: Sprockets::Environment

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

Instance Attribute Summary

Attributes inherited from Base

#cache, #context_class, #logger

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Caching

#asset_from_hash, #cache_hash

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

#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

Methods included from Digest

#digest, #digest_class, #digest_class=, #version, #version=

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
# 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

  # 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



68
69
70
71
72
73
74
75
76
# File 'lib/sprockets/environment.rb', line 68

def find_asset(path, options = {})
  # Ensure inmemory cached assets are still fresh on every lookup
  if (asset = @assets[path.to_s]) && asset.fresh?
    asset
  elsif asset = super
    @assets[path.to_s] = @assets[asset.pathname.to_s] = 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.



63
64
65
# File 'lib/sprockets/environment.rb', line 63

def index
  Index.new(self)
end