Class: Bowerify::AssetsProcessor

Inherits:
Sprockets::Processor
  • Object
show all
Defined in:
lib/bowerify/assets_processor.rb

Constant Summary collapse

CSS_URL_RE =
/(url\(('|"|))((.+?)\.(gif|png|jpg|jpeg|ttf|svg|woff2|woff|eot))(.*?\2\))/

Instance Method Summary collapse

Instance Method Details

#bower_component?(path) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
# File 'lib/bowerify/assets_processor.rb', line 26

def bower_component?(path)
  bower_components_paths.each do |bower_path|
    if path.to_s.starts_with?(bower_path)
      return true
    end
  end

  false
end

#bower_components_pathsObject



36
37
38
# File 'lib/bowerify/assets_processor.rb', line 36

def bower_components_paths
  Array(Rails.application.config.bower_components_path).map(&:to_s)
end

#evaluate(context, locals = {}) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/bowerify/assets_processor.rb', line 4

def evaluate(context, locals={})
  if bower_component?(context.pathname)
    fix_assets_path data, context
  else
    data
  end
end

#fix_assets_path(data, context) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bowerify/assets_processor.rb', line 12

def fix_assets_path(data, context)
  data.gsub CSS_URL_RE do |*args|
    s1, s2 = $1.dup, $6.dup

    path = File.expand_path("#{context.pathname.dirname}/#{$3}")
    bower_components_paths.each do |bower_path|
      path = path.gsub("#{bower_path}/", "")
    end
    path = context.asset_path(path)

    "#{s1}#{path}#{s2}"
  end
end