Skip to content

Commit

Permalink
Clarify dev_server.running? first checks configured
Browse files Browse the repository at this point in the history
Webpacker.dev_server.running? might appear to be costly, but it first
checks that the dev_server value is on the config Hash.

Renaming the method makes it clear that the first check is whether it is
configured.

Since the dev_server would ever be configured on production, there is no
cost for calling this method on production.
  • Loading branch information
justin808 committed Jun 3, 2021
1 parent 284204e commit 8a8a90f
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ environment.loaders.append('nodeModules', nodeModules)
- Simple webpack config
- Removed integration installers
- Splitchunks enabled by default
- CSS extraction enabled by default
- CSS extraction enabled by default, except when devServer is configured and running
- Optional CSS support
- Renamed DevServer.running? to DevServer.configured_and_running?

## [[5.4.0]](https://github.com/rails/webpacker/compare/v5.3.0...v5.4.0) - 2021-05-18

Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/dev_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(config)
@config = config
end

def running?
def configured_and_running?
if config.dev_server.present?
Socket.tcp(host, port, connect_timeout: connect_timeout).close
true
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/dev_server_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(app = nil, opts = {})
end

def perform_request(env)
if env["PATH_INFO"].start_with?("/#{public_output_uri_path}") && dev_server.running?
if env["PATH_INFO"].start_with?("/#{public_output_uri_path}") && dev_server.configured_and_running?
env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = dev_server.host
env["HTTP_X_FORWARDED_SERVER"] = dev_server.host_with_port
env["HTTP_PORT"] = env["HTTP_X_FORWARDED_PORT"] = dev_server.port.to_s
Expand Down
7 changes: 6 additions & 1 deletion lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ def preload_pack_asset(name, **options)
# <link rel="stylesheet" media="screen" href="/packs/calendar-8c7ce31a.chunk.css" />
# <link rel="stylesheet" media="screen" href="/packs/map-8c7ce31a.chunk.css" />
#
# When using the webpack-dev-server, CSS is inlined so HMR can be turned on for CSS,
# including CSS modules
# <%= stylesheet_pack_tag 'calendar', 'map' %> # => nil
#
# DO:
#
# <%= stylesheet_pack_tag 'calendar', 'map' %>
Expand All @@ -137,7 +141,8 @@ def preload_pack_asset(name, **options)
# <%= stylesheet_pack_tag 'calendar' %>
# <%= stylesheet_pack_tag 'map' %>
def stylesheet_pack_tag(*names, **options)
unless current_webpacker_instance.dev_server.running?
css_extracted = !Webpacker.dev_server.configured_and_running?
if css_extracted
stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def lookup!(name, pack_type = {})

private
def compiling?
config.compile? && !dev_server.running?
config.compile? && !dev_server.configured_and_running?
end

def compile
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Webpacker::Engine < ::Rails::Engine
config.webpacker = ActiveSupport::OrderedOptions.new

initializer "webpacker.proxy" do |app|
insert_middleware = Webpacker.config.dev_server.present? rescue nil
insert_middleware = Webpacker.config.dev_server.present?
if insert_middleware
app.middleware.insert_before 0,
Rails::VERSION::MAJOR >= 5 ?
Expand Down
2 changes: 1 addition & 1 deletion test/dev_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class DevServerTest < Webpacker::Test
def test_running?
refute Webpacker.dev_server.running?
refute Webpacker.dev_server.configured_and_running?
end

def test_host
Expand Down

0 comments on commit 8a8a90f

Please sign in to comment.