Skip to content

Commit

Permalink
Allow using a custom assets manifest via app config
Browse files Browse the repository at this point in the history
At the moment the only allowed path is the default one but
there is a config.assets.manifest option that should set
the path of the custom manifest path.

When an application uses a custom location for the manifest,
even by setting the path like:

```
config.assets.manifest = File.expand_path('my_custom_path/config/manifest.js', __dir__)
```

would raise a ManifestNeededError exception.

This commit adds a check of the configured assets manifest
path before trying to use the default location.
  • Loading branch information
kennyadsl committed Oct 10, 2019
1 parent 7ab889c commit cab84c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 12 additions & 3 deletions lib/sprockets/railtie.rb
Expand Up @@ -69,7 +69,11 @@ class Railtie < ::Rails::Railtie

class ManifestNeededError < StandardError
def initialize
msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" +
manifest_path = Pathname.new(
::Rails.application.config.assets.manifest ||
::Rails.root.join("app/assets/config/manifest.js")
)
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 @@ -102,8 +106,13 @@ def configure(&block)

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 )
manifest_path = Pathname.new(
app.config.assets.manifest ||
::Rails.root.join("app/assets/config/manifest.js")
)
raise ManifestNeededError unless manifest_path.exist?

app.config.assets.precompile += [manifest_path.to_s]
else
app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
end
Expand Down
10 changes: 9 additions & 1 deletion test/test_railtie.rb
Expand Up @@ -296,8 +296,16 @@ def test_sprockets_context_helper
end

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

app.configure do
config.assets.manifest = Rails.root.join('config','foo','bar.json')
config.assets.manifest = Rails.root.join('app','assets','config','foo','bar.json')
end
app.initialize!

Expand Down

0 comments on commit cab84c0

Please sign in to comment.