Skip to content

Commit

Permalink
Allow using a custom assets configuration manifest location
Browse files Browse the repository at this point in the history
At the moment the only allowed path is the default one but
there could be the need to set a custom configuration manifest
location.

This commit introduces a new setting:

`config.assets.config_manifest`

that allows to change the location of this manifest.

This is an example configuration:

```
config.assets.config_manifest = File.expand_path('my_custom_path/manifest.js', __dir__)
```
  • Loading branch information
kennyadsl committed Oct 11, 2019
1 parent 7ab889c commit 53ae935
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
17 changes: 13 additions & 4 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class Railtie < ::Rails::Railtie
include Sprockets::Rails::Utils

class ManifestNeededError < StandardError
def initialize
msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" +
def initialize(manifest_path)
msg = "Expected to find a manifest file in `#{manifest_path}`\n" +
"But did not, please create this file and use it to link any assets that need\n" +
"to be rendered by your app:\n\n" +
"Example:\n" +
Expand Down Expand Up @@ -99,11 +99,20 @@ def configure(&block)
config.assets.prefix = "/assets"
config.assets.manifest = nil
config.assets.quiet = false
config.assets.config_manifest = nil

initializer :set_config_manifest_path do |app|
config_manifest_path = Pathname.new(
app.config.assets.config_manifest ||
::Rails.root.join("app/assets/config/manifest.js")
)
raise ManifestNeededError.new(config_manifest_path) unless config_manifest_path.exist?
app.config.assets.config_manifest = config_manifest_path.to_s
end

initializer :set_default_precompile do |app|
if using_sprockets4?
raise ManifestNeededError unless ::Rails.root.join("app/assets/config/manifest.js").exist?
app.config.assets.precompile += %w( manifest.js )
app.config.assets.precompile += [app.config.assets.config_manifest]
else
app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
end
Expand Down
42 changes: 35 additions & 7 deletions test/test_railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ def silence_stderr
$stderr.reopen orig_stderr
end

def create_config_manifest(dir = "app/assets/config")
Dir.chdir(app.root) do
FileUtils.mkdir_p(dir)
File.open("#{ dir }/manifest.js", "w") do |f|
f << ""
end
end
end

class TestBoot < Minitest::Test
include ActiveSupport::Testing::Isolation

Expand Down Expand Up @@ -39,13 +48,7 @@ def setup
@app.config.active_support.deprecation = :notify
ActionView::Base # load ActionView

Dir.chdir(app.root) do
dir = "app/assets/config"
FileUtils.mkdir_p(dir)
File.open("#{ dir }/manifest.js", "w") do |f|
f << ""
end
end
create_config_manifest
end

def test_initialize
Expand Down Expand Up @@ -295,6 +298,31 @@ def test_sprockets_context_helper
assert_nil env.context_class.config.asset_host
end

def test_default_config_manifest_path
app.initialize!
assert_match %r{app/assets/config.manifest.js$}, app.config.assets.config_manifest
end

def test_custom_config_manifest_path
create_config_manifest("my/custom/path")
app.configure do
config.assets.config_manifest = 'my/custom/path/manifest.js'
end
app.initialize!

assert_match %r{my/custom/path/manifest.js$}, app.config.assets.config_manifest
end

def test_config_manifest_raises_when_does_not_exist
app.configure do
config.assets.config_manifest = 'not/found/manifest.js'
end

assert_raises Sprockets::Railtie::ManifestNeededError do
app.initialize!
end
end

def test_manifest_path
app.configure do
config.assets.manifest = Rails.root.join('config','foo','bar.json')
Expand Down

0 comments on commit 53ae935

Please sign in to comment.