Skip to content

Commit

Permalink
FEATURE: Skip "/packs" when serving static assets (#447)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
OsamaSayegh and ivopt committed Jun 23, 2020
1 parent c42c190 commit 4d5839d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/mini_profiler_rails/railtie.rb
Expand Up @@ -30,6 +30,8 @@ def self.initialize!(app)

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?
Expand Down
6 changes: 6 additions & 0 deletions lib/mini_profiler_rails/railtie_methods.rb
Expand Up @@ -51,5 +51,11 @@ def should_move?(child, node)
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
3 changes: 2 additions & 1 deletion rack-mini-profiler.gemspec
Expand Up @@ -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'
Expand All @@ -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
2 changes: 2 additions & 0 deletions spec/fixtures/webpacker.yml
@@ -0,0 +1,2 @@
development:
public_output_path: some/assets/path
15 changes: 15 additions & 0 deletions spec/integration/railtie_methods_spec.rb
Expand Up @@ -178,4 +178,19 @@ def to_seconds(array)
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

0 comments on commit 4d5839d

Please sign in to comment.