Class: Middleman::LiveReloadExtension
- Inherits:
-
Extension
- Object
- Extension
- Middleman::LiveReloadExtension
- Defined in:
- lib/middleman-livereload/extension_3_1.rb
Instance Method Summary collapse
-
#initialize(app, options_hash = {}, &block) ⇒ LiveReloadExtension
constructor
A new instance of LiveReloadExtension.
Constructor Details
#initialize(app, options_hash = {}, &block) ⇒ LiveReloadExtension
Returns a new instance of LiveReloadExtension.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/middleman-livereload/extension_3_1.rb', line 19 def initialize(app, ={}, &block) super if app.respond_to?(:server?) return unless app.server? else return unless app.environment == :development end port = .port.to_i host = .host js_port = .js_port || port js_host = .js_host || host no_swf = .no_swf ignore = .ignore = .to_h livereload_css_target = .livereload_css_target livereload_css_pattern = .livereload_css_pattern extension = self app.ready do reactor = ::Middleman::LiveReload::Reactor.create(, self) ignored = lambda do |file| return true if app.files.respond_to?(:ignored?) && app.files.send(:ignored?, file) ignore.any? { |i| file.to_s.match(i) } end app.files.changed do |file| next if ignored.call(file) logger.debug "LiveReload: File changed - #{file}" reload_path = "#{Dir.pwd}/#{file}" file_url = app.sitemap.file_to_path(file) if file_url file_resource = app.sitemap.find_resource_by_path(file_url) if file_resource reload_path = file_resource.url end end # handle imported partials # load target file instead of triggering full page refresh if livereload_css_pattern.match(file.to_s) and not livereload_css_target.nil? logger.info("LiveReload: CSS import changed, reloading target") reload_path = livereload_css_target end reactor.reload_browser(reload_path) end app.files.deleted do |file| next if ignored.call(file) logger.debug "LiveReload: File deleted - #{file}" reactor.reload_browser("#{Dir.pwd}/#{file}") end # Use the vendored livereload.js source rather than trying to get it from Middleman # https://github.com/johnbintz/rack-livereload#which-livereload-script-does-it-use app.use ::Rack::LiveReload, port: js_port, host: js_host, no_swf: no_swf, source: :vendored, ignore: ignore end end |