Sha256: 5ea7961f70a574cd4c90832175e6d72250db1a19c9147c1b2c4793bf3a100c79

Contents?: true

Size: 1.18 KB

Versions: 12

Compression:

Stored size: 1.18 KB

Contents

require 'native'
module Hyperstack
  class Hotloader
    class CssReloader

      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

      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
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hyperstack-config-1.0.alpha1.8 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.alpha1.7 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.alpha1.6 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.alpha1.5 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.alpha1.4 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.alpha1.3 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.alpha1.2 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.alpha1.1 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.0.pre.alpha1 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.alpha1 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-1.0.pre.alpha1 lib/hyperstack/hotloader/css_reloader.rb
hyperstack-config-0.1 lib/hyperstack/hotloader/css_reloader.rb