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 12f3382
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 9 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 @@ -102,8 +102,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.new(manifest_path) 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
12 changes: 10 additions & 2 deletions test/test_railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,21 @@ 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!

assert manifest = app.assets_manifest
assert_match %r{config/foo/bar\.json$}, manifest.path
assert_match %r{app/assets/config/foo/bar\.json$}, manifest.path
assert_match %r{public/assets$}, manifest.dir
end

Expand Down

0 comments on commit 12f3382

Please sign in to comment.