Class: Hyperstack::Hotloader::CssReloader

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperstack/hotloader/css_reloader.rb

Instance Method Summary collapse

Instance Method Details

#is_matching_stylesheet?(href, url) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hyperstack/hotloader/css_reloader.rb', line 27

def is_matching_stylesheet?(href, url)
  # straight match, like in Rack::Sass::Plugin
  if href.index(url)
    true
  else
    # Rails asset pipeline match
    url_base = File.basename(url).sub(/\.s?css+/, '').sub(/\.s?css+/, '')
    href_base = File.basename(href).sub(/\.self-?.*.css.+/, '')
    url_base == href_base
  end

end

#reload(reload_request, document) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hyperstack/hotloader/css_reloader.rb', line 6

def reload(reload_request, document)
  url = reload_request[:url]
  puts "Reloading CSS: #{url}"
  to_append = "t_hot_reload=#{Time.now.to_i}"
  links = Native(`document.getElementsByTagName("link")`)
  (0..links.length-1).each { |i|
    link = links[i]
    if link.rel == 'stylesheet' && is_matching_stylesheet?(link.href, url)
      if  link.href !~ /\?/
        link.href += "?#{to_append}"
      else
        if link.href !~ /t_hot_reload/
          link.href += "&#{to_append}"
        else
          link.href = link.href.sub(/t_hot_reload=\d+/, to_append)
        end
      end
    end
  }
end