DEV: Quality of life improvements when working on MP client-side code (#443)

DEV: Quality of life improvements when working on MP client-side code (#443)

  • DEV: Quality of life improvements when working on MP’s client assets

  • Add a note about the new rake task

  • Remove character

diff --git a/README.md b/README.md
index 8e7c97c..c0fb6f0 100644
--- a/README.md
+++ b/README.md
@@ -407,6 +407,16 @@ if JSON.const_defined?(:Pure)
 end
 `‍``
 
+## Development
+
+If you want to contribute to this project, that's great, thank you! You can run the following rake task:
+
+`‍``
+$ bundle exec rake client_dev
+`‍``
+
+which will start a local Sinatra server at `http://localhost:9292` where you'll be able to preview your changes. Refreshing the page should be enough to see any changes you make to files in the `lib/html` directory.
+
 ## Running the Specs
 
 `‍``
diff --git a/Rakefile b/Rakefile
index 839ce40..7677473 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,4 +1,5 @@
 # frozen_string_literal: true
+
 require 'rubygems'
 require 'bundler'
 require 'bundler/gem_tasks'
@@ -50,6 +51,7 @@ end\n"
   end
 end
 
+@mini_racer_context = nil
 desc "generate vendor asset file"
 task :write_vendor_js do
   require 'mini_racer'
@@ -65,13 +67,13 @@ task :write_vendor_js do
     templates[id] = node.content
   end
 
-  c = MiniRacer::Context.new
-  c.eval(dot_js)
+  @mini_racer_context ||= MiniRacer::Context.new
+  @mini_racer_context.eval(dot_js)
   templates_js = "MiniProfiler.templates = {};\n"
 
   templates.each do |k, v|
     template = v.gsub('`', '\\`')
-    compiled = c.eval <<~JS
+    compiled = @mini_racer_context.eval <<~JS
       doT.compile(`#{template}`).toString()
     JS
     templates_js += <<~JS
@@ -96,6 +98,29 @@ task :write_vendor_js do
   File.write(path, content)
 end
 
+desc "Start Sinatra server for client-side development"
+task :client_dev do
+  require 'listen'
+
+  regexp = /(vendor\.js|includes\.css)$/
+  listener = Listen.to(File.expand_path("lib/html", __dir__)) do |modified|
+    next if modified.all? { |m| m =~ regexp }
+    print("Assets change detected; updating ASSET_VERSION constant and recompiling templates... ")
+    rake_task = Rake.application[:update_asset_version]
+    rake_task.all_prerequisite_tasks.each(&:reenable)
+    rake_task.reenable
+    rake_task.invoke
+    puts "Done"
+  rescue => err
+    puts "\nError occurred: #{err.inspect}"
+  end
+  listener.start
+  pid = spawn("cd website && BUNDLE_GEMFILE=Gemfile bundle exec rackup")
+  Process.wait(pid)
+rescue Interrupt
+  listener.stop
+end
+
 desc "copy files from other parts of the tree"
 task :copy_files do
   # TODO grab files from MiniProfiler/UI
diff --git a/lib/mini_profiler/profiler.rb b/lib/mini_profiler/profiler.rb
index d5ad96a..72bbf79 100644
--- a/lib/mini_profiler/profiler.rb
+++ b/lib/mini_profiler/profiler.rb
@@ -152,7 +152,7 @@ module Rack
       resources_env = env.dup
       resources_env['PATH_INFO'] = file_name
 
-      rack_file = Rack::File.new(MiniProfiler.resources_root, 'Cache-Control' => 'max-age:86400')
+      rack_file = Rack::File.new(MiniProfiler.resources_root, 'Cache-Control' => "max-age:#{cache_control_value}")
       rack_file.call(resources_env)
     end
 
@@ -673,5 +673,8 @@ Append the following to your query string:
       current.inject_js = false
     end
 
+    def cache_control_value
+      86400
+    end
   end
 end
diff --git a/rack-mini-profiler.gemspec b/rack-mini-profiler.gemspec
index 843c791..8c89d81 100644
--- a/rack-mini-profiler.gemspec
+++ b/rack-mini-profiler.gemspec
@@ -40,6 +40,7 @@ Gem::Specification.new do |s|
   s.add_development_dependency 'mini_racer'
   s.add_development_dependency 'nokogiri'
   s.add_development_dependency 'rubocop-discourse'
+  s.add_development_dependency 'listen'
 
   s.require_paths = ["lib"]
 end
diff --git a/website/Gemfile b/website/Gemfile
new file mode 100644
index 0000000..85e3395
--- /dev/null
+++ b/website/Gemfile
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+source 'https://rubygems.org'
+
+gem 'sinatra'
+gem 'rack-mini-profiler', path: '../'
+gem 'puma'
+gem 'listen'
+gem 'byebug'
diff --git a/website/config.ru b/website/config.ru
new file mode 100644
index 0000000..0708967
--- /dev/null
+++ b/website/config.ru
@@ -0,0 +1,4 @@
+# frozen_string_literal: true
+
+require './sample'
+run Sample
diff --git a/website/data.json b/website/data.json
new file mode 100644
index 0000000..78f5ab8
--- /dev/null
+++ b/website/data.json
@@ -0,0 +1 @@

[... diff too long, it was truncated ...]

GitHub sha: 25945242

This commit appears in #443 which was merged by SamSaffron.