Skip to content

Commit 94a0332

Browse files
committed
Merge pull request rails#136 from johnnyshields/fix-manifest-path
Fix config.assets.manifest Rake task and Railtie
2 parents 10b73e9 + c492a11 commit 94a0332

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### Unreleased
2+
3+
* Fix issues related `config.assets.manifest` option, including issues with `assets:precompile` Rake task.
4+
5+
*Johnny Shields*
6+
7+
18
### 2.1.3
29

310
* Correct NameError on Sprockets::Rails::VERSION.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Defaults to `/assets`. Changes the directory to compile assets to.
8585

8686
**`config.assets.manifest`**
8787

88-
Defines the full path to be used for the asset precompiler's manifest file. Defaults to using the `config.assets.prefix` directory within the public folder.
88+
Defines the full path to be used for the asset precompiler's manifest file. Defaults to a randomly-generated filename in the `config.assets.prefix` directory within the public folder.
8989

9090
**`config.assets.digest`**
9191

@@ -134,6 +134,7 @@ The following plugins provide some extras for the Sprockets Asset Pipeline.
134134
* Unmanaged asset paths and urls fallback to linking to public/. This should make it easier to work with both compiled assets and simple static assets. As a side effect, there will never be any "asset not precompiled errors" when linking to missing assets. They will just link to a public file which may or may not exist.
135135
* JS and CSS compressors must be explicitly set. Magic detection has been removed to avoid loading compressors in environments where you want to avoid loading any of the asset libraries. Assign `config.assets.js_compressor = :uglifier` or `config.assets.css_compressor = :sass` for the standard compressors.
136136
* The manifest file is now in a JSON format. Since it lives in public/ by default, the initial filename is also randomized to obfuscate public access to the resource.
137+
* `config.assets.manifest` (if used) must now include the manifest filename, e.g. `Rails.root.join('config/manifest.json')`. It cannot be a directory.
137138
* Two cleanup tasks. `rake assets:clean` is now a safe cleanup that only removes older assets that are no longer used. While `rake assets:clobber` nukes the entire `public/assets` directory and clears your filesystem cache. The clean task allows for rolling deploys that may still be linking to an old asset while the new assets are being built.
138139

139140

lib/sprockets/rails/task.rb

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ def assets
3737
end
3838
end
3939

40+
def manifest
41+
if app
42+
Sprockets::Manifest.new(index, output, app.config.assets.manifest)
43+
else
44+
super
45+
end
46+
end
47+
4048
def cache_path
4149
if app
4250
"#{app.config.root}/tmp/cache/assets"

lib/sprockets/railtie.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def configure(&block)
6666
config.after_initialize do |app|
6767
config = app.config
6868

69-
manifest_path = config.assets.manifest || File.join(config.paths['public'].first, config.assets.prefix)
69+
manifest_assets_path = File.join(config.paths['public'].first, config.assets.prefix)
7070

7171
# Configuration options that should invalidate
7272
# the Sprockets cache when changed.
@@ -99,9 +99,9 @@ def configure(&block)
9999

100100
if config.assets.compile
101101
self.assets_environment = app.assets
102-
self.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_path)
102+
self.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_assets_path, config.assets.manifest)
103103
else
104-
self.assets_manifest = Sprockets::Manifest.new(manifest_path)
104+
self.assets_manifest = Sprockets::Manifest.new(manifest_assets_path, config.assets.manifest)
105105
end
106106
end
107107

test/test_railtie.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ def test_sprockets_context_helper
194194

195195
def test_manifest_path
196196
app.configure do
197-
config.assets.manifest = Rails.root.join('config','foo')
197+
config.assets.manifest = Rails.root.join('config','foo','bar.json')
198198
end
199199
app.initialize!
200200

201-
assert_match %r{config/foo/manifest-.*.json}, ActionView::Base.assets_manifest.path
201+
assert_match %r{config/foo/bar\.json$}, ActionView::Base.assets_manifest.path
202+
assert_match %r{public/assets$}, ActionView::Base.assets_manifest.dir
202203
end
203204

204205
def test_manifest_path_respects_rails_public_path
@@ -207,6 +208,7 @@ def test_manifest_path_respects_rails_public_path
207208
end
208209
app.initialize!
209210

210-
assert_match %r{test_public/assets/manifest-.*.json}, ActionView::Base.assets_manifest.path
211+
assert_match %r{test_public/assets/manifest-.*\.json$}, ActionView::Base.assets_manifest.path
212+
assert_match %r{test_public/assets$}, ActionView::Base.assets_manifest.dir
211213
end
212214
end

test/test_task.rb

+27-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ def setup
1616
@assets = Sprockets::Environment.new
1717
@assets.append_path FIXTURES_PATH
1818

19-
@dir = File.join(Dir::tmpdir, 'rails/task')
19+
@dir = File.join(Dir::tmpdir, 'rails', 'task')
2020

21-
@manifest = Sprockets::Manifest.new(@assets, @dir)
21+
@manifest_file = File.join(Dir::tmpdir, 'rails', 'manifest', 'custom-manifest.json')
22+
@manifest = Sprockets::Manifest.new(@assets, @dir, @manifest_file)
2223

2324
@environment_ran = false
2425
# Stub Rails environment task
@@ -39,6 +40,10 @@ def teardown
3940

4041
FileUtils.rm_rf(@dir)
4142
assert Dir["#{@dir}/*"].empty?
43+
44+
manifest_dir = File.dirname(@manifest_file)
45+
FileUtils.rm_rf(manifest_dir)
46+
assert Dir["#{manifest_dir}/*"].empty?
4247
end
4348

4449
def test_precompile
@@ -49,6 +54,26 @@ def test_precompile
4954

5055
@rake['assets:precompile'].invoke
5156

57+
assert @environment_ran
58+
assert File.exist?(@manifest_file)
59+
assert File.exist?("#{@dir}/#{digest_path}")
60+
end
61+
62+
def test_precompile_without_manifest
63+
Sprockets::Rails::Task.new do |t|
64+
t.environment = @assets
65+
t.manifest = Sprockets::Manifest.new(@assets, @dir, nil)
66+
t.assets = ['foo.js', 'foo-modified.js']
67+
t.log_level = :fatal
68+
end
69+
70+
assert !@environment_ran
71+
72+
digest_path = @assets['foo.js'].digest_path
73+
assert !File.exist?("#{@dir}/#{digest_path}")
74+
75+
@rake['assets:precompile'].invoke
76+
5277
assert @environment_ran
5378
assert Dir["#{@dir}/manifest-*.json"].first
5479
assert File.exist?("#{@dir}/#{digest_path}")

0 commit comments

Comments
 (0)