Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable devserver proxy for custom environments #1415

Merged
Merged
2 changes: 1 addition & 1 deletion lib/webpacker/dev_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def fetch(key)
end

def defaults
config.send(:defaults)[:dev_server]
config.send(:defaults)[:dev_server] || {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about config.dig(:defaults, :dev_server)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config is not a hash, but a Webpacker::Configuration which has a 'defaults' private method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i see

end
end
4 changes: 3 additions & 1 deletion lib/webpacker/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class Webpacker::Engine < ::Rails::Engine
end

initializer "webpacker.proxy" do |app|
if Rails.env.development?
insert_middleware = Webpacker.config.dev_server.present? rescue nil
insert_middleware ||= false
if insert_middleware
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil is equal to false in if statement
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right ! thanks
ed6743a

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually if Webpacker.config.dev_server will be fine, because call present? to an object is equal to check the object is not nil
besides, in this case, this statement would not raise any error (nil.present? will got false), so no need to rescue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Webpacker.config is a Webpacker::Configuration object ; and the call to Webpacker.config.dev_server.present? leads to call the "load" method of Webpacker.config instance.
And this can fail for some reasons : bad config, no config file... This happens in unit tests environments for projects which depend on webpacker, for exemple.
I think that devserver proxy middleware initializer should not raise error in this case, but just skip.

app.middleware.insert_before 0,
Rails::VERSION::MAJOR >= 5 ?
Webpacker::DevServerProxy : "Webpacker::DevServerProxy", ssl_verify_none: true
Expand Down
6 changes: 4 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
require "rails/test_help"
require "byebug"

require_relative "test_app/config/environment"

Rails.env = "production"

# Webpacker.instance paths hacks need to be done before application initialization
require "webpacker"
Webpacker.instance = Webpacker::Instance.new \
root_path: Pathname.new(File.expand_path("test_app", __dir__)),
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker.yml", __dir__))

require_relative "test_app/config/environment"

class Webpacker::Test < Minitest::Test
private
def reloaded_config
Expand Down