diff --git a/CHANGELOG.md b/CHANGELOG.md index ce68ea28..0dab3273 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,12 +12,16 @@ _Please add entries here for your pull requests that are not yet released._ ### Added - Experimental support for other JS package managers using `package_json` gem [PR 349](https://github.com/shakacode/shakapacker/pull/349) by [G-Rath](https://github.com/g-rath). - Support `hmr: only` configuration [PR 378](https://github.com/shakacode/shakapacker/pull/378) by [SimenB](https://github.com/SimenB). +- Use `config/shakapacker.yml` as the secondary source for `asset_host` and `relative_url_root` configurations [PR 376](https://github.com/shakacode/shakapacker/pull/376) by [ahangarha](https://github.com/ahangarha). ### Fixed - Recommend `server` option instead of deprecated `https` option when `--https` is provided [PR 380](https://github.com/shakacode/shakapacker/pull/380) by [G-Rath](https://github.com/g-rath) - Recompile assets on asset host change [PR 364](https://github.com/shakacode/shakapacker/pull/364) by [ahangarha](https://github.com/ahangarha). - Add deprecation warning for `https` option in `shakapacker.yml` (use `server: 'https'` instead) [PR 382](https://github.com/shakacode/shakapacker/pull/382) by [G-Rath](https://github.com/g-rath). +### Deprecated +- The usage of relative_url_root is deprecated in Shakapacker and will be removed in v8. [PR 376](https://github.com/shakacode/shakapacker/pull/376) by [ahangarha](https://github.com/ahangarha). + ## [v7.1.0] - September 30, 2023 ### Added diff --git a/lib/install/config/shakapacker.yml b/lib/install/config/shakapacker.yml index c071d131..6a4df72e 100644 --- a/lib/install/config/shakapacker.yml +++ b/lib/install/config/shakapacker.yml @@ -50,6 +50,11 @@ default: &default # https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling useContentHash: false + # Setting the asset host here will override Rails.application.config.asset_host. + # Here, you can set different asset_host per environment. Note that + # SHAKAPACKER_ASSET_HOST will override both configurations. + # asset_host: custom-path + development: <<: *default compile: true diff --git a/lib/shakapacker/configuration.rb b/lib/shakapacker/configuration.rb index 033ef3bd..d702f8ed 100644 --- a/lib/shakapacker/configuration.rb +++ b/lib/shakapacker/configuration.rb @@ -117,11 +117,19 @@ def fetch(key) end def asset_host - ENV.fetch("SHAKAPACKER_ASSET_HOST", ActionController::Base.helpers.compute_asset_host) + ENV.fetch( + "SHAKAPACKER_ASSET_HOST", + fetch(:asset_host) || ActionController::Base.helpers.compute_asset_host + ) end def relative_url_root - ENV.fetch("SHAKAPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root) + Shakapacker.puts_deprecation_message "The usage of relative_url_root is deprecated in Shakapacker and will be removed in v8." + + ENV.fetch( + "SHAKAPACKER_RELATIVE_URL_ROOT", + fetch(:relative_url_root) || ActionController::Base.relative_url_root + ) end private diff --git a/spec/shakapacker/configuration_spec.rb b/spec/shakapacker/configuration_spec.rb index f41e61bd..79cbc544 100644 --- a/spec/shakapacker/configuration_spec.rb +++ b/spec/shakapacker/configuration_spec.rb @@ -346,11 +346,31 @@ expect(config.asset_host).to eq "custom_host.abc" end - it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_ASSET_HOST is not set" do - allow(ActionController::Base.helpers).to receive(:compute_asset_host).and_return("domain.abc") - allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", "domain.abc").and_return("domain.abc") + context "without SHAKAPACKER_ASSET_HOST set" do + it "returns asset_host in shakapacker.yml if set" do + expect(config).to receive(:fetch).with(:asset_host).and_return("value-in-config-file.com") + expect(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", "value-in-config-file.com").and_return("value-in-config-file.com") - expect(config.asset_host).to eq "domain.abc" + expect(config.asset_host).to eq "value-in-config-file.com" + end + + context "without asset_host set in the shakapacker.yml" do + it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_ASSET_HOST is not set" do + expect(config).to receive(:fetch).with(:asset_host).and_return(nil) + expect(ActionController::Base.helpers).to receive(:compute_asset_host).and_return("domain.abc") + allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", "domain.abc").and_return("domain.abc") + + expect(config.asset_host).to eq "domain.abc" + end + + context "without ActionController::Base.helpers.compute_asset_host returing any value" do + it "returns nil" do + expect(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return(nil) + + expect(config.asset_host).to be nil + end + end + end end end @@ -363,17 +383,40 @@ ) end + it "shows deprecation message" do + expect { config.relative_url_root }.to output(/deprecated/).to_stdout + end + it "returns the value of SHAKAPACKER_RELATIVE_URL_ROOT if set" do - allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return("custom_value") + expect(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return("custom_value") expect(config.relative_url_root).to eq "custom_value" end - it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_RELATIVE_URL_ROOT is not set" do - allow(ActionController::Base).to receive(:relative_url_root).and_return("abcd") - allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", "abcd").and_return("abcd") + context "without SHAKAPACKER_RELATIVE_URL_ROOT set" do + it "returns relative_url_root in shakapacker.yml if set" do + expect(config).to receive(:fetch).with(:relative_url_root).and_return("value-in-config-file") + expect(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", "value-in-config-file").and_return("value-in-config-file") + + expect(config.relative_url_root).to eq "value-in-config-file" + end - expect(config.relative_url_root).to eq "abcd" + context "without relative_url_root set in the shakapacker.yml" do + it "returns ActionController::Base.relative_url_root if SHAKAPACKER_RELATIVE_URL_ROOT is not set" do + expect(ActionController::Base).to receive(:relative_url_root).and_return("abcd") + expect(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", "abcd").and_return("abcd") + + expect(config.relative_url_root).to eq "abcd" + end + + context "without ActionController::Base.relative_url_root returing any value" do + it "returns an empty string" do + expect(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return(nil) + + expect(config.relative_url_root).to be nil + end + end + end end end end