Skip to content

Instantly share code, notes, and snippets.

@dportalesr
Created October 23, 2018 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dportalesr/de535477362963dcdfc7cc402b9a1665 to your computer and use it in GitHub Desktop.
Save dportalesr/de535477362963dcdfc7cc402b9a1665 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'bundler'
Bundler.require
unless production?
require 'sinatra/reloader'
# ..other non-production requires..
# Avoid this one since it'll cause recursive
# reloads when using other `also_reload` calls
# also_reload 'app.rb'
# Fine (unless those files are also_reload-free)
also_reload 'app/**/*.rb'
end
# ...
unless ENV['RACK_ENV'] == 'production'
require 'rack-livereload'
use Rack::LiveReload
end
# your main app file
require_relative 'app'
run Sinatra::Application
group :development, :test do
gem "guard-livereload", require: false
gem "rack-livereload"
end
# Run `guard init livereload` to generate this file
# This is the minimal recommended setup
guard 'livereload' do
watch(%r{^app\.rb})
watch(%r{views/.+\.(erb|haml|slim)$})
watch(%r{public/.+\.(css|js|html)})
end
# Don't forget to rename this file to `Guardfile`
# This Guardfile works with sinatra-asset-pipeline
# taking source and compiled extensions into account
guard 'livereload' do
# source-compiled extensions
extensions = {
css: %i(scss sass),
js: nil,
html: nil,
png: nil,
gif: nil,
jpg: nil,
jpeg: nil,
}.freeze
# Files from asset pipeline
extensions.each do |compiled_ext, source_exts|
exts = Array(source_exts) + [compiled_ext]
exts.each do |source_ext|
watch(%r{
(?:assets/\w+/(?<path>[^.]+) # path+base part
(?<ext>\.#{source_ext})) # source extension part
(?:\.\w+|$) # compiled extension part or nothing
# so it matches both .sass & .sass.css files
}x) do |m|
# We need to remap to send to the browser (livereloading)
# the compiled version of the asset instead of the modified
# one, that is, the source file.
path = m[1]
"/assets/#{path}.#{compiled_ext}"
end
end
end
# Will trigger full page reload
compiled_exts = extensions.keys
watch(%r{public/.+\.(#{compiled_exts * '|'})}) # Static files in public/
watch(%r{views\/.+\.(erb|haml|slim)$}) # View templates
watch(%r{app\/(helpers|models)\/(.+)\.rb$}) # app subfolders
watch(%r{^app\.rb})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment