-
Notifications
You must be signed in to change notification settings - Fork 16
/
asset_map.rb
70 lines (55 loc) · 1.5 KB
/
asset_map.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'nokogiri'
require "ember_cli/assets/errors"
module EmberCli
module Assets
class AssetMap
def initialize(name:, asset_map:, index_html:)
@name = name
@asset_map = asset_map
@index_html = index_html
end
def javascripts
assert_asset_map!
Nokogiri::HTML(index_html.read).css('script').map {|s|
filename = File.basename(s['src'])
asset_matching(/#{Regexp.escape(filename)}\z/)
}
end
def stylesheets
assert_asset_map!
Nokogiri::HTML(index_html.read).css('link[rel="stylesheet"]').map {|s|
filename = File.basename(s['href'])
asset_matching(/#{Regexp.escape(filename)}\z/)
}
end
private
attr_reader :name, :asset_map, :index_html
def asset_matching(regex)
matching_asset = files.detect { |asset| asset =~ regex }
if matching_asset.to_s.empty?
raise_missing_asset(regex)
end
prepend + matching_asset
end
def prepend
asset_map["prepend"].to_s
end
def files
Array(assets.values)
end
def assets
asset_map["assets"] || {}
end
def raise_missing_asset(regex)
raise BuildError.new("Failed to find assets matching `#{regex}`")
end
def assert_asset_map!
if assets.empty?
raise BuildError.new <<-MSG
Missing `#{name}/assets/assetMap.json`
MSG
end
end
end
end
end