Skip to content

Commit 8b87902

Browse files
committed
Allow using a custom assets configuration manifest location
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__) ```
1 parent 7ab889c commit 8b87902

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

lib/sprockets/railtie.rb

+15-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" +
@@ -99,11 +99,22 @@ def configure(&block)
9999
config.assets.prefix = "/assets"
100100
config.assets.manifest = nil
101101
config.assets.quiet = false
102+
config.assets.config_manifest = nil
103+
104+
initializer :set_config_manifest_path do |app|
105+
config_manifest = if app.config.assets.config_manifest.present?
106+
Pathname.new(app.config.assets.config_manifest)
107+
else
108+
::Rails.root.join("app/assets/config/manifest.js")
109+
end
110+
raise ManifestNeededError, config_manifest unless config_manifest.exist?
111+
112+
app.config.assets.config_manifest = config_manifest.to_s
113+
end
102114

103115
initializer :set_default_precompile do |app|
104116
if using_sprockets4?
105-
raise ManifestNeededError unless ::Rails.root.join("app/assets/config/manifest.js").exist?
106-
app.config.assets.precompile += %w( manifest.js )
117+
app.config.assets.precompile += [app.config.assets.config_manifest]
107118
else
108119
app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
109120
end

test/test_railtie.rb

+35-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ def silence_stderr
1212
$stderr.reopen orig_stderr
1313
end
1414

15+
def create_config_manifest(dir = "app/assets/config")
16+
Dir.chdir(app.root) do
17+
FileUtils.mkdir_p(dir)
18+
File.open("#{ dir }/manifest.js", "w") do |f|
19+
f << ""
20+
end
21+
end
22+
end
23+
1524
class TestBoot < Minitest::Test
1625
include ActiveSupport::Testing::Isolation
1726

@@ -39,13 +48,7 @@ def setup
3948
@app.config.active_support.deprecation = :notify
4049
ActionView::Base # load ActionView
4150

42-
Dir.chdir(app.root) do
43-
dir = "app/assets/config"
44-
FileUtils.mkdir_p(dir)
45-
File.open("#{ dir }/manifest.js", "w") do |f|
46-
f << ""
47-
end
48-
end
51+
create_config_manifest
4952
end
5053

5154
def test_initialize
@@ -295,6 +298,31 @@ def test_sprockets_context_helper
295298
assert_nil env.context_class.config.asset_host
296299
end
297300

301+
def test_default_config_manifest_path
302+
app.initialize!
303+
assert_match %r{app/assets/config.manifest.js$}, app.config.assets.config_manifest
304+
end
305+
306+
def test_custom_config_manifest_path
307+
create_config_manifest("my/custom/path")
308+
app.configure do
309+
config.assets.config_manifest = 'my/custom/path/manifest.js'
310+
end
311+
app.initialize!
312+
313+
assert_match %r{my/custom/path/manifest.js$}, app.config.assets.config_manifest
314+
end
315+
316+
def test_config_manifest_raises_when_does_not_exist
317+
app.configure do
318+
config.assets.config_manifest = 'not/found/manifest.js'
319+
end
320+
321+
assert_raises Sprockets::Railtie::ManifestNeededError do
322+
app.initialize!
323+
end
324+
end
325+
298326
def test_manifest_path
299327
app.configure do
300328
config.assets.manifest = Rails.root.join('config','foo','bar.json')

0 commit comments

Comments
 (0)