Class: Middleman::SprocketsExtension

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-sprockets/extension.rb

Defined Under Namespace

Modules: SprocketsAccessor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ SprocketsExtension

Returns a new instance of SprocketsExtension.



28
29
30
31
32
33
34
35
36
37
# File 'lib/middleman-sprockets/extension.rb', line 28

def initialize(app, options_hash={}, &block)
  require "middleman-sprockets/sass_function_hack"

  super

  # Start out with a stub environment that can only be configured (paths and such)
  @environment = ::Middleman::Sprockets::ConfigOnlyEnvironment.new

  app.send :include, SprocketsAccessor
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



16
17
18
# File 'lib/middleman-sprockets/extension.rb', line 16

def environment
  @environment
end

Instance Method Details

#after_configurationObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/middleman-sprockets/extension.rb', line 44

def after_configuration
  ::Tilt.register ::Sprockets::EjsTemplate, 'ejs'
  ::Tilt.register ::Sprockets::EcoTemplate, 'eco'
  ::Tilt.register ::Sprockets::JstProcessor, 'jst'

  app.template_extensions :jst => :js, :eco => :js, :ejs => :js

  if app.config.defines_setting?(:debug_assets) && !options.setting(:debug_assets).value_set?
    options[:debug_assets] = app.config[:debug_assets]
  end

  config_environment = @environment
  debug_assets = !app.build? && options[:debug_assets]
  @environment = ::Middleman::Sprockets::Environment.new(app, :debug_assets => debug_assets)
  config_environment.apply_to_environment(@environment)

  append_paths_from_gems

  # Setup Sprockets Sass options
  if app.config.defines_setting?(:sass)
    app.config[:sass].each { |k, v| ::Sprockets::Sass.options[k] = v }
  end

  # Intercept requests to /javascripts and /stylesheets and pass to sprockets
  our_sprockets = self.environment

  [app.config[:js_dir], app.config[:css_dir], app.config[:images_dir], app.config[:fonts_dir]].each do |dir|
    app.map("/#{dir}") { run our_sprockets }
  end
end

#manipulate_resource_list(resources) ⇒ Object

Add sitemap resource for every image in the sprockets load path



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/middleman-sprockets/extension.rb', line 76

def manipulate_resource_list(resources)
  imported_assets = Middleman::Sprockets::AssetList.new

  environment.imported_assets.each do |asset|
    asset.resolve_path_with environment
    @app.logger.debug "== Importing Sprockets asset #{asset.real_path}"

    imported_assets << asset
  end

  resources_list = []
  environment.paths.each do |load_path|
    environment.each_entry(load_path) do |path|
      asset = Middleman::Sprockets::Asset.new(path, source_directory: load_path)

      imported_assets.lookup(asset) do |candidate, found_asset| 
        candidate.destination_path = found_asset.output_path if found_asset.output_path
        candidate.import_it
      end

      next unless asset.import?

      if asset.has_type? :image
        asset.destination_directory = @app.config[:images_dir]
      elsif asset.has_type? :script
        asset.destination_directory = @app.config[:js_dir]
      elsif asset.has_type? :font
        asset.destination_directory = @app.config[:fonts_dir]
      elsif asset.has_type? :stylesheet
        asset.destination_directory = @app.config[:css_dir]
      end

      new_path = @app.sitemap.extensionless_path(asset.destination_path.to_s)

      next if @app.sitemap.find_resource_by_destination_path(new_path.to_s)
      resources_list << ::Middleman::Sitemap::Resource.new(@app.sitemap, new_path.to_s, path.to_s)
    end
  end
  resources + resources_list
end