Skip to content

Commit

Permalink
Better Errors
Browse files Browse the repository at this point in the history
Raise a descriptive error if specific config file `app/assets/config/manifest.js` is not found when using Sprockets 4.

The `link ../javascripts/application.js` link does not work for me on codetriage.com but `link application.js` does.

The error when a source isn't found should only show how to add that specific source for maximum clarity.
  • Loading branch information
schneems committed Mar 17, 2016
1 parent 44a8592 commit af64798
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
12 changes: 4 additions & 8 deletions lib/sprockets/rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ class AssetNotPrecompiled < StandardError
def initialize(source)
msg =
if using_sprockets4?
"Asset `#{source}` was not declared to be precompiled in production.\n" +
"Declare links to your assets in `assets/config/manifest.js`.\n" +
"Examples:\n" +
"`//= link ../javascripts/application.js`\n" +
"`//= link_directory ../javascripts .js`\n" +
"`//= link_directory ../stylesheets .css`\n" +
"`//= link_tree ../javascripts .js`\n" +
"`//= link_tree ../images`\n"
"Asset `#{ source }` was not declared to be precompiled in production.\n" +
"Declare links to your assets in `app/assets/config/manifest.js`.\n\n" +
" //= link #{ source }\n" +
"and restart your server"
else
"Asset was not declared to be precompiled in production.\n" +
"Add `Rails.application.config.assets.precompile += " +
Expand Down
28 changes: 24 additions & 4 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ module Sprockets
class Railtie < ::Rails::Railtie
include Sprockets::Rails::Utils

class ManifestNeededError < StandardError
def initialize
msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" +
"But did not, please create this file and use it to link any assets that need\n" +
"to be rendered by your app:\n\n" +
"Example:\n" +
" //= link_tree ../images\n" +
" //= link_directory ../javascripts .js\n" +
" //= link_directory ../stylesheets .css\n" +
"and restart your server"
super msg
end
end

LOOSE_APP_ASSETS = lambda do |logical_path, filename|
filename.start_with?(::Rails.root.join("app/assets").to_s) &&
!['.js', '.css', ''].include?(File.extname(logical_path))
Expand All @@ -80,13 +94,19 @@ def configure(&block)
config.assets = OrderedOptions.new
config.assets._blocks = []
config.assets.paths = []
config.assets.precompile = []
config.assets.prefix = "/assets"
config.assets.manifest = nil
if using_sprockets4?
config.assets.precompile = %w( manifest.js )
else
config.assets.precompile = [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]

initializer :set_default_precompile do |app|
if using_sprockets4?
raise ManifestNeededError if !::Rails.root.join("app/assets/config/manifest.js").exist?
app.config.assets.precompile += %w( manifest.js )
else
app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
end
end

config.assets.version = ""
config.assets.debug = false
config.assets.compile = true
Expand Down
16 changes: 12 additions & 4 deletions test/test_railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def setup
@app.config.middleware ||= Rails::Configuration::MiddlewareStackProxy.new
@app.config.active_support.deprecation = :notify
ActionView::Base # load ActionView

Dir.chdir(app.root) do
dir = "app/assets/config"
FileUtils.mkdir_p(dir)
File.open("#{ dir }/manifest.js", "w") do |f|
f << ""
end
end
end

def test_initialize
Expand Down Expand Up @@ -72,7 +80,7 @@ def test_defaults_to_compile_assets_with_env_and_manifest_available
assert_equal ROOT, env.root
assert_equal "", env.version
assert env.cache
assert_equal [], env.paths
assert_equal ["#{ROOT}/app/assets/config"], env.paths
assert_nil env.js_compressor
assert_nil env.css_compressor
end
Expand Down Expand Up @@ -120,7 +128,7 @@ def test_copies_paths
app.initialize!

assert env = app.assets
assert_equal ["#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
assert_equal ["#{ROOT}/app/assets/config", "#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
env.paths.sort
end

Expand Down Expand Up @@ -179,7 +187,7 @@ def test_configure
app.initialize!

assert env = app.assets
assert_equal ["#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
assert_equal ["#{ROOT}/app/assets/config", "#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
env.paths.sort
end

Expand Down Expand Up @@ -340,7 +348,7 @@ def test_direct_build_environment_call
assert_kind_of Sprockets::Environment, env

assert_equal ROOT, env.root
assert_equal ["#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
assert_equal ["#{ROOT}/app/assets/config", "#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
env.paths.sort
end
end

0 comments on commit af64798

Please sign in to comment.