Skip to content

Commit

Permalink
For custom environments fall back to production defaults (shakacode#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
ur5us authored Jul 22, 2022
1 parent 1ab4e0d commit 3a5018e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _Please add entries here for your pull requests that are not yet released._

- `append_javascript_pack_tag` and `append_stylesheet_pack_tag` helpers return `nil` to prevent rendering the queue into view when using `<%= … %>` ERB syntax. [PR 167](https://github.com/shakacode/shakapacker/pull/167) by [ur5us](https://github.com/ur5us)
- fix non-runnable test due to wrong code nesting [PR 173](https://github.com/shakacode/shakapacker/pull/173) by [ur5us](https://github.com/ur5us)
- fix default configurations not working for custom Rails environments [PR 168](https://github.com/shakacode/shakapacker/pull/168) by [ur5us](https://github.com/ur5us)

## [v6.5.0] - July 4, 2022
### Added
Expand Down
2 changes: 2 additions & 0 deletions lib/webpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
module Webpacker
extend self

DEFAULT_ENV = "production".freeze

def instance=(instance)
@instance = instance
end
Expand Down
2 changes: 1 addition & 1 deletion lib/webpacker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def defaults
rescue ArgumentError
YAML.load_file(path)
end
HashWithIndifferentAccess.new(config[env])
HashWithIndifferentAccess.new(config[env] || config[Webpacker::DEFAULT_ENV])
end
end
end
6 changes: 2 additions & 4 deletions lib/webpacker/env.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class Webpacker::Env
DEFAULT = "production".freeze

delegate :config_path, :logger, to: :@webpacker

def self.inquire(webpacker)
Expand All @@ -13,7 +11,7 @@ def initialize(webpacker)

def inquire
fallback_env_warning if config_path.exist? && !current
current || DEFAULT.inquiry
current || Webpacker::DEFAULT_ENV.inquiry
end

private
Expand All @@ -22,7 +20,7 @@ def current
end

def fallback_env_warning
logger.info "RAILS_ENV=#{Rails.env} environment is not defined in config/webpacker.yml, falling back to #{DEFAULT} environment"
logger.info "RAILS_ENV=#{Rails.env} environment is not defined in config/webpacker.yml, falling back to #{Webpacker::DEFAULT_ENV} environment"
end

def available_environments
Expand Down
22 changes: 22 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,26 @@ def test_webpacker_precompile

refute @no_precompile_config.webpacker_precompile?
end

def test_fall_back_to_bundled_config_with_the_same_name_for_standard_environments
@no_default_config = Webpacker::Configuration.new(
root_path: @config.root_path,
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_defaults_fallback.yml", __dir__)),
env: "default"
)

refute @no_default_config.cache_manifest? # fall back to "default" config from bundled file
refute @no_default_config.webpacker_precompile? # use "default" config from custom file
end

def test_fall_back_to_bundled_production_config_for_custom_environments
@no_env_config = Webpacker::Configuration.new(
root_path: @config.root_path,
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_defaults_fallback.yml", __dir__)),
env: "staging"
)

assert @no_env_config.cache_manifest? # fall back to "production" config from bundled file
refute @no_env_config.webpacker_precompile? # use "staging" config from custom file
end
end
2 changes: 1 addition & 1 deletion test/env_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def test_custom_with_config
end

def test_default
assert_equal Webpacker::Env::DEFAULT, "production"
assert_equal Webpacker::DEFAULT_ENV, "production"
end
end
11 changes: 11 additions & 0 deletions test/test_app/config/webpacker_defaults_fallback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Note: You must restart bin/webpacker-dev-server for changes to take effect

default: &default
# cache_manifest will be fetched from bundled defaults
webpacker_precompile: false

staging:
<<: *default

production:
<<: *default

0 comments on commit 3a5018e

Please sign in to comment.