Skip to content

Commit 12f3382

Browse files
committed
Allow using a custom assets manifest via app config
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.
1 parent 7ab889c commit 12f3382

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/sprockets/railtie.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class Railtie < ::Rails::Railtie
6868
include Sprockets::Rails::Utils
6969

7070
class ManifestNeededError < StandardError
71-
def initialize
72-
msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" +
71+
def initialize(manifest_path)
72+
msg = "Expected to find a manifest file in `#{manifest_path}`\n" +
7373
"But did not, please create this file and use it to link any assets that need\n" +
7474
"to be rendered by your app:\n\n" +
7575
"Example:\n" +
@@ -102,8 +102,13 @@ def configure(&block)
102102

103103
initializer :set_default_precompile do |app|
104104
if using_sprockets4?
105-
raise ManifestNeededError unless ::Rails.root.join("app/assets/config/manifest.js").exist?
106-
app.config.assets.precompile += %w( manifest.js )
105+
manifest_path = Pathname.new(
106+
app.config.assets.manifest ||
107+
::Rails.root.join("app/assets/config/manifest.js")
108+
)
109+
raise ManifestNeededError.new(manifest_path) unless manifest_path.exist?
110+
111+
app.config.assets.precompile += [manifest_path.to_s]
107112
else
108113
app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
109114
end

test/test_railtie.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,21 @@ def test_sprockets_context_helper
296296
end
297297

298298
def test_manifest_path
299+
Dir.chdir(app.root) do
300+
dir = "app/assets/config/foo"
301+
FileUtils.mkdir_p(dir)
302+
File.open("#{ dir }/bar.json", "w") do |f|
303+
f << ""
304+
end
305+
end
306+
299307
app.configure do
300-
config.assets.manifest = Rails.root.join('config','foo','bar.json')
308+
config.assets.manifest = Rails.root.join('app','assets','config','foo','bar.json')
301309
end
302310
app.initialize!
303311

304312
assert manifest = app.assets_manifest
305-
assert_match %r{config/foo/bar\.json$}, manifest.path
313+
assert_match %r{app/assets/config/foo/bar\.json$}, manifest.path
306314
assert_match %r{public/assets$}, manifest.dir
307315
end
308316

0 commit comments

Comments
 (0)