FEATURE: Skip "/packs" when serving static assets (#447)

FEATURE: Skip “/packs” when serving static assets (#447)

  • Skip “/packs” when serving static assets

  • Skip /packs if Webpacker is defined and use its config to get the path

  • Fix: Don’t mutate webpacker’s config…

  • Add test case

Co-authored-by: Ivo Jesus ivo.jesus@gmail.com

diff --git a/lib/mini_profiler_rails/railtie.rb b/lib/mini_profiler_rails/railtie.rb
index ba3f73d..1d5b0c8 100644
--- a/lib/mini_profiler_rails/railtie.rb
+++ b/lib/mini_profiler_rails/railtie.rb
@@ -30,6 +30,8 @@ module Rack::MiniProfilerRails
 
     if serves_static_assets?(app)
       c.skip_paths << app.config.assets.prefix
+      wp_assets_path = get_webpacker_assets_path()
+      c.skip_paths << wp_assets_path if wp_assets_path
     end
 
     unless Rails.env.development? || Rails.env.test?
diff --git a/lib/mini_profiler_rails/railtie_methods.rb b/lib/mini_profiler_rails/railtie_methods.rb
index 6ce1671..396692f 100644
--- a/lib/mini_profiler_rails/railtie_methods.rb
+++ b/lib/mini_profiler_rails/railtie_methods.rb
@@ -51,5 +51,11 @@ module Rack::MiniProfilerRailsMethods
     child[start] + child[duration] <= node[start] + node[duration]
   end
 
+  def get_webpacker_assets_path
+    if defined?(Webpacker)
+      Webpacker.config.public_output_path.to_s.gsub(Webpacker.config.public_path.to_s, "")
+    end
+  end
+
   extend self
 end
diff --git a/rack-mini-profiler.gemspec b/rack-mini-profiler.gemspec
index b5e3ca5..44e8f49 100644
--- a/rack-mini-profiler.gemspec
+++ b/rack-mini-profiler.gemspec
@@ -30,7 +30,6 @@ Gem::Specification.new do |s|
 
   s.add_development_dependency 'rake', '< 11'
   s.add_development_dependency 'rack-test'
-  s.add_development_dependency 'activerecord', '~> 3.0'
   s.add_development_dependency 'dalli'
   s.add_development_dependency 'rspec', '~> 3.6.0'
   s.add_development_dependency 'redis'
@@ -41,6 +40,8 @@ Gem::Specification.new do |s|
   s.add_development_dependency 'nokogiri'
   s.add_development_dependency 'rubocop-discourse'
   s.add_development_dependency 'listen'
+  s.add_development_dependency 'webpacker', '~> 5.1'
+  s.add_development_dependency 'rails', '~> 5.1'
 
   s.require_paths = ["lib"]
 end
diff --git a/spec/fixtures/webpacker.yml b/spec/fixtures/webpacker.yml
new file mode 100644
index 0000000..519b031
--- /dev/null
+++ b/spec/fixtures/webpacker.yml
@@ -0,0 +1,2 @@
+development:
+  public_output_path: some/assets/path
diff --git a/spec/integration/railtie_methods_spec.rb b/spec/integration/railtie_methods_spec.rb
index d595c25..9079c72 100644
--- a/spec/integration/railtie_methods_spec.rb
+++ b/spec/integration/railtie_methods_spec.rb
@@ -178,4 +178,19 @@ describe Rack::MiniProfilerRailsMethods do
       expect(@current_timer[cts]).to eq({})
     end
   end
+
+  it '#get_webpacker_assets_path returns webpacker public_output_path if webpacker exists' do
+    expect(described_class.get_webpacker_assets_path()).to eq(nil)
+    require 'rails'
+    require 'webpacker'
+    tmp_path = Pathname.new("/tmp/rails_root_#{SecureRandom.hex}")
+    FileUtils.mkdir(tmp_path)
+    Webpacker.instance = Webpacker::Instance.new(
+      root_path: tmp_path,
+      config_path: Pathname.new(File.expand_path("../fixtures/webpacker.yml", __dir__))
+    )
+    expect(described_class.get_webpacker_assets_path()).to eq("/some/assets/path")
+  ensure
+    FileUtils.rm_rf(tmp_path)
+  end
 end

GitHub sha: 4d5839de

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