Skip to content

Commit

Permalink
Add shakapacker.yml as the secondary source for asset_host and relati…
Browse files Browse the repository at this point in the history
…ve_url_root (#376)

* Show deprecation message for `relative_url_root`
  • Loading branch information
ahangarha authored Dec 22, 2023
1 parent 0f89275 commit 1cb8e16
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/install/config/shakapacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/shakapacker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 52 additions & 9 deletions spec/shakapacker/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit 1cb8e16

Please sign in to comment.