Module: Sprockets::Engines

Includes:
Utils
Included in:
Configuration, Loader
Defined in:
lib/sprockets/engines.rb

Overview

`Engines` provides a global and `Environment` instance registry.

An engine is a type of processor that is bound to a filename extension. `application.js.coffee` indicates that the `CoffeeScriptProcessor` engine will be ran on the file.

Extensions can be stacked and will be evaulated from right to left. `application.js.coffee.erb` will first run `ERBProcessor` then `CoffeeScriptProcessor`.

All `Engine`s must follow the `Template` interface. It is recommended to subclass `Template`.

Its recommended that you register engine changes on your local `Environment` instance.

environment.register_engine '.foo', FooProcessor

The global registry is exposed for plugins to register themselves.

Sprockets.register_engine '.sass', SassProcessor

Constant Summary

Constants included from Utils

Utils::UNBOUND_METHODS_BIND_TO_ANY_OBJECT

Instance Method Summary collapse

Methods included from Utils

#concat_javascript_sources, #dfs, #duplicable?, #hash_reassoc, #hash_reassoc1, #module_include, #normalize_extension, #string_end_with_semicolon?

Instance Method Details

#engine_mime_typesObject

Internal: Returns a `Hash` of engine extensions to mime types.

# => { '.coffee' => 'application/javascript' }



45
46
47
# File 'lib/sprockets/engines.rb', line 45

def engine_mime_types
  config[:engine_mime_types]
end

#enginesObject

Returns a `Hash` of `Engine`s registered on the `Environment`. If an `ext` argument is supplied, the `Engine` associated with that extension will be returned.

environment.engines
# => {".coffee" => CoffeeScriptProcessor, ".sass" => SassProcessor, ...}


38
39
40
# File 'lib/sprockets/engines.rb', line 38

def engines
  config[:engines]
end

#register_engine(ext, klass, options = {}) ⇒ Object

Registers a new Engine `klass` for `ext`. If the `ext` already has an engine registered, it will be overridden.

environment.register_engine '.coffee', CoffeeScriptProcessor


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sprockets/engines.rb', line 67

def register_engine(ext, klass, options = {})
  ext = Sprockets::Utils.normalize_extension(ext)

  if klass.class == Sprockets::LazyProcessor || klass.respond_to?(:call)
    self.config = hash_reassoc(config, :engines) do |engines|
      engines.merge(ext => klass)
    end
    if options[:mime_type]
      self.config = hash_reassoc(config, :engine_mime_types) do |mime_types|
        mime_types.merge(ext.to_s => options[:mime_type])
      end
    end
  else
    self.config = hash_reassoc(config, :engines) do |engines|
      engines.merge(ext => LegacyTiltProcessor.new(klass))
    end
    if klass.respond_to?(:default_mime_type) && klass.default_mime_type
      self.config = hash_reassoc(config, :engine_mime_types) do |mime_types|
        mime_types.merge(ext.to_s => klass.default_mime_type)
      end
    end
  end
end

#unwrap_engines(extnames) ⇒ Object

Internal: Find and load engines by extension.

extnames - Array of String extnames

Returns Array of Procs.



54
55
56
57
58
59
60
# File 'lib/sprockets/engines.rb', line 54

def unwrap_engines(extnames)
  extnames.map { |ext|
    engines[ext]
  }.map { |engine|
    unwrap_processor(engine)
  }
end