Skip to content

Commit 4f4b387

Browse files
committed
Merge pull request #329 from rails/schneems/sprockets4
Better Errors
2 parents 44a8592 + af64798 commit 4f4b387

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ class AssetNotPrecompiled < StandardError
1111
def initialize(source)
1212
msg =
1313
if using_sprockets4?
14-
"Asset `#{source}` was not declared to be precompiled in production.\n" +
15-
"Declare links to your assets in `assets/config/manifest.js`.\n" +
16-
"Examples:\n" +
17-
"`//= link ../javascripts/application.js`\n" +
18-
"`//= link_directory ../javascripts .js`\n" +
19-
"`//= link_directory ../stylesheets .css`\n" +
20-
"`//= link_tree ../javascripts .js`\n" +
21-
"`//= link_tree ../images`\n"
14+
"Asset `#{ source }` was not declared to be precompiled in production.\n" +
15+
"Declare links to your assets in `app/assets/config/manifest.js`.\n\n" +
16+
" //= link #{ source }\n" +
17+
"and restart your server"
2218
else
2319
"Asset was not declared to be precompiled in production.\n" +
2420
"Add `Rails.application.config.assets.precompile += " +

lib/sprockets/railtie.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ module Sprockets
6666
class Railtie < ::Rails::Railtie
6767
include Sprockets::Rails::Utils
6868

69+
class ManifestNeededError < StandardError
70+
def initialize
71+
msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" +
72+
"But did not, please create this file and use it to link any assets that need\n" +
73+
"to be rendered by your app:\n\n" +
74+
"Example:\n" +
75+
" //= link_tree ../images\n" +
76+
" //= link_directory ../javascripts .js\n" +
77+
" //= link_directory ../stylesheets .css\n" +
78+
"and restart your server"
79+
super msg
80+
end
81+
end
82+
6983
LOOSE_APP_ASSETS = lambda do |logical_path, filename|
7084
filename.start_with?(::Rails.root.join("app/assets").to_s) &&
7185
!['.js', '.css', ''].include?(File.extname(logical_path))
@@ -80,13 +94,19 @@ def configure(&block)
8094
config.assets = OrderedOptions.new
8195
config.assets._blocks = []
8296
config.assets.paths = []
97+
config.assets.precompile = []
8398
config.assets.prefix = "/assets"
8499
config.assets.manifest = nil
85-
if using_sprockets4?
86-
config.assets.precompile = %w( manifest.js )
87-
else
88-
config.assets.precompile = [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
100+
101+
initializer :set_default_precompile do |app|
102+
if using_sprockets4?
103+
raise ManifestNeededError if !::Rails.root.join("app/assets/config/manifest.js").exist?
104+
app.config.assets.precompile += %w( manifest.js )
105+
else
106+
app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
107+
end
89108
end
109+
90110
config.assets.version = ""
91111
config.assets.debug = false
92112
config.assets.compile = true

test/test_railtie.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ def setup
3838
@app.config.middleware ||= Rails::Configuration::MiddlewareStackProxy.new
3939
@app.config.active_support.deprecation = :notify
4040
ActionView::Base # load ActionView
41+
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
4149
end
4250

4351
def test_initialize
@@ -72,7 +80,7 @@ def test_defaults_to_compile_assets_with_env_and_manifest_available
7280
assert_equal ROOT, env.root
7381
assert_equal "", env.version
7482
assert env.cache
75-
assert_equal [], env.paths
83+
assert_equal ["#{ROOT}/app/assets/config"], env.paths
7684
assert_nil env.js_compressor
7785
assert_nil env.css_compressor
7886
end
@@ -120,7 +128,7 @@ def test_copies_paths
120128
app.initialize!
121129

122130
assert env = app.assets
123-
assert_equal ["#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
131+
assert_equal ["#{ROOT}/app/assets/config", "#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
124132
env.paths.sort
125133
end
126134

@@ -179,7 +187,7 @@ def test_configure
179187
app.initialize!
180188

181189
assert env = app.assets
182-
assert_equal ["#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
190+
assert_equal ["#{ROOT}/app/assets/config", "#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
183191
env.paths.sort
184192
end
185193

@@ -340,7 +348,7 @@ def test_direct_build_environment_call
340348
assert_kind_of Sprockets::Environment, env
341349

342350
assert_equal ROOT, env.root
343-
assert_equal ["#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
351+
assert_equal ["#{ROOT}/app/assets/config", "#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
344352
env.paths.sort
345353
end
346354
end

0 commit comments

Comments
 (0)