Skip to content
This repository

Read digests of assets from manifest.yml when precompile is used #2746

Closed
wants to merge 2 commits into from

2 participants

Guillermo Iguaran Xavier Noria
Guillermo Iguaran
Owner

Same #2741 but for 3-1-stable

Xavier Noria
Owner
fxn commented

Could you please document this in the asset pipeline guide?

Guillermo Iguaran guilleiguaran CoC: if you are precompiling you want a manifest.yml, if you have a m…
…anifest.yml you want use it and don't fallback to live compiling
e43fa07
Guillermo Iguaran
Owner

Added also the commit of #2747

Guillermo Iguaran
Owner

Closed, I will open a new one with most recent changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Aug 30, 2011
Guillermo Iguaran guilleiguaran Read digests of assets from manifest.yml if config.assets.manifest is on 27ac20d
Guillermo Iguaran guilleiguaran CoC: if you are precompiling you want a manifest.yml, if you have a m…
…anifest.yml you want use it and don't fallback to live compiling
e43fa07
This page is out of date. Refresh to see the latest.
8 actionpack/lib/sprockets/assets.rake
@@ -18,7 +18,8 @@ namespace :assets do
18 18
19 19 config = Rails.application.config
20 20 env = Rails.application.assets
21   - target = Rails.root.join("public#{config.assets.prefix}")
  21 + target = Pathname.new(File.join(Rails.public_path, config.assets.prefix))
  22 + manifest = {}
22 23
23 24 if env.respond_to?(:each_logical_path)
24 25 config.assets.precompile.each do |path|
@@ -30,6 +31,7 @@ namespace :assets do
30 31 end
31 32
32 33 if asset = env.find_asset(logical_path)
  34 + manifest[logical_path] = asset.digest_path
33 35 filename = target.join(asset.digest_path)
34 36 mkdir_p filename.dirname
35 37 asset.write_to(filename)
@@ -43,6 +45,10 @@ namespace :assets do
43 45 assets << {:to => target}
44 46 env.precompile(*assets)
45 47 end
  48 +
  49 + File.open("#{target}/manifest.yml", 'w') do |f|
  50 + YAML.dump(manifest, f)
  51 + end
46 52 end
47 53 end
48 54
15 actionpack/lib/sprockets/helpers/rails_helper.rb
@@ -14,6 +14,7 @@ def asset_paths
14 14 paths = RailsHelper::AssetPaths.new(config, controller)
15 15 paths.asset_environment = asset_environment
16 16 paths.asset_prefix = asset_prefix
  17 + paths.asset_digests = asset_digests
17 18 paths
18 19 end
19 20 end
@@ -74,6 +75,10 @@ def asset_prefix
74 75 Rails.application.config.assets.prefix
75 76 end
76 77
  78 + def asset_digests
  79 + Rails.application.config.assets.digests
  80 + end
  81 +
77 82 # Override to specify an alternative asset environment for asset
78 83 # path generation. The environment should already have been mounted
79 84 # at the prefix returned by +asset_prefix+.
@@ -82,7 +87,9 @@ def asset_environment
82 87 end
83 88
84 89 class AssetPaths < ::ActionView::AssetPaths #:nodoc:
85   - attr_accessor :asset_environment, :asset_prefix
  90 + attr_accessor :asset_environment, :asset_prefix, :asset_digests
  91 +
  92 + class AssetNotPrecompiledError < StandardError; end
86 93
87 94 def compute_public_path(source, dir, ext=nil, include_host=true, protocol=nil)
88 95 super(source, asset_prefix, ext, include_host, protocol)
@@ -101,6 +108,12 @@ def asset_for(source, ext)
101 108 end
102 109
103 110 def digest_for(logical_path)
  111 + if asset_digests
  112 + digest = asset_digests[logical_path]
  113 + raise AssetNotPrecompiledError unless digest
  114 + return digest
  115 + end
  116 +
104 117 if asset = asset_environment[logical_path]
105 118 return asset.digest_path
106 119 end
4 actionpack/lib/sprockets/railtie.rb
@@ -26,6 +26,10 @@ class Railtie < ::Rails::Railtie
26 26 end
27 27 end
28 28
  29 + if File.exist?(path = File.join(Rails.public_path, config.assets.prefix, "manifest.yml"))
  30 + config.assets.digests = YAML.load_file(path)
  31 + end
  32 +
29 33 ActiveSupport.on_load(:action_view) do
30 34 include ::Sprockets::Helpers::RailsHelper
31 35
7 railties/lib/rails/application/configuration.rb
@@ -42,10 +42,9 @@ def initialize(*)
42 42 @assets.version = ''
43 43 @assets.debug = false
44 44 @assets.allow_debugging = false
45   -
46   - @assets.cache_store = [ :file_store, "#{root}/tmp/cache/assets/" ]
47   - @assets.js_compressor = nil
48   - @assets.css_compressor = nil
  45 + @assets.cache_store = [ :file_store, "#{root}/tmp/cache/assets/" ]
  46 + @assets.js_compressor = nil
  47 + @assets.css_compressor = nil
49 48 end
50 49
51 50 def compiled_asset_path
59 railties/test/application/assets_test.rb
@@ -62,6 +62,65 @@ def app
62 62 end
63 63 end
64 64
  65 + test "precompile creates a manifest file with all the assets listed" do
  66 + app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
  67 + app_file "app/assets/javascripts/application.js", "alert();"
  68 +
  69 + capture(:stdout) do
  70 + Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
  71 + end
  72 +
  73 + manifest = "#{app_path}/public/assets/manifest.yml"
  74 +
  75 + assets = YAML.load_file(manifest)
  76 + assert_match /application-([0-z]+)\.js/, assets["application.js"]
  77 + assert_match /application-([0-z]+)\.css/, assets["application.css"]
  78 + end
  79 +
  80 + test "assets do not require any assets group gem when manifest file is present" do
  81 + app_file "app/assets/javascripts/application.js", "alert();"
  82 +
  83 + ENV["RAILS_ENV"] = "production"
  84 + capture(:stdout) do
  85 + Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
  86 + end
  87 + manifest = "#{app_path}/public/assets/manifest.yml"
  88 + assets = YAML.load_file(manifest)
  89 + asset_path = assets["application.js"]
  90 +
  91 + require "#{app_path}/config/environment"
  92 +
  93 + # Checking if Uglifier is defined we can know if Sprockets was reached or not
  94 + assert !defined?(Uglifier)
  95 + get "/assets/#{asset_path}"
  96 + assert_match "alert()", last_response.body
  97 + assert !defined?(Uglifier)
  98 + end
  99 +
  100 + test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled" do
  101 + app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'app' %>"
  102 +
  103 + app_file "config/routes.rb", <<-RUBY
  104 + AppTemplate::Application.routes.draw do
  105 + match '/posts', :to => "posts#index"
  106 + end
  107 + RUBY
  108 +
  109 + ENV["RAILS_ENV"] = "production"
  110 + capture(:stdout) do
  111 + Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
  112 + end
  113 +
  114 + # Create file after of precompile
  115 + app_file "app/assets/javascripts/app.js", "alert();"
  116 +
  117 + require "#{app_path}/config/environment"
  118 + class ::PostsController < ActionController::Base ; end
  119 +
  120 + get '/posts'
  121 + assert_match /AssetNotPrecompiledError/, last_response.body
  122 + end
  123 +
65 124 test "precompile appends the md5 hash to files referenced with asset_path and run in the provided RAILS_ENV" do
66 125 app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
67 126

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.