Module: Sprockets::Bower

Included in:
Base
Defined in:
lib/sprockets/bower.rb

Constant Summary collapse

POSSIBLE_BOWER_JSONS =

Internal: All supported bower.json files.

github.com/bower/json/blob/0.4.0/lib/json.js#L7

['bower.json', 'component.json', '.bower.json']

Instance Method Summary collapse

Instance Method Details

#read_bower_main(dirname, filename) ⇒ Object

Internal: Read bower.json's main directive.

dirname - String path to component directory. filename - String path to bower.json.

Returns nothing.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sprockets/bower.rb', line 43

def read_bower_main(dirname, filename)
  bower = JSON.parse(File.read(filename), create_additions: false)

  case bower['main']
  when String
    yield File.expand_path(bower['main'], dirname)
  when Array
    bower['main'].each do |name|
      yield File.expand_path(name, dirname)
    end
  end
end

#resolve_alternates(load_path, logical_path, &block) ⇒ Object

Internal: Override resolve_alternates to install bower.json behavior.

load_path - String environment path logical_path - String path relative to base

Returns nothing.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sprockets/bower.rb', line 16

def resolve_alternates(load_path, logical_path, &block)
  super

  # bower.json can only be nested one level deep
  if !logical_path.index('/')
    dirname = File.join(load_path, logical_path)
    stat    = self.stat(dirname)

    if stat && stat.directory?
      filenames = POSSIBLE_BOWER_JSONS.map { |basename| File.join(dirname, basename) }
      filename  = filenames.detect { |fn| self.file?(fn) }

      if filename
        read_bower_main(dirname, filename, &block)
      end
    end
  end

  nil
end