Skip to content

Commit 1183308

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 1183308

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/sprockets/railtie.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ class Railtie < ::Rails::Railtie
6969

7070
class ManifestNeededError < StandardError
7171
def initialize
72-
msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" +
72+
manifest_path = Pathname.new(
73+
::Rails.application.config.assets.manifest ||
74+
::Rails.root.join("app/assets/config/manifest.js")
75+
)
76+
msg = "Expected to find a manifest file in `#{manifest_path}`\n" +
7377
"But did not, please create this file and use it to link any assets that need\n" +
7478
"to be rendered by your app:\n\n" +
7579
"Example:\n" +
@@ -102,8 +106,13 @@ def configure(&block)
102106

103107
initializer :set_default_precompile do |app|
104108
if using_sprockets4?
105-
raise ManifestNeededError unless ::Rails.root.join("app/assets/config/manifest.js").exist?
106-
app.config.assets.precompile += %w( manifest.js )
109+
manifest_path = Pathname.new(
110+
app.config.assets.manifest ||
111+
::Rails.root.join("app/assets/config/manifest.js")
112+
)
113+
raise ManifestNeededError unless manifest_path.exist?
114+
115+
app.config.assets.precompile += [manifest_path.to_s]
107116
else
108117
app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
109118
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)