此代碼段包含所有JS/CSS文件,不含寶石,下:應用程序/資產,供應商/資產,LIB /資產,除非它們是局部的文件(例如, 「_file.sass」)。它也有一個策略,包括來自Gems的資產,這些資源並不包含在每個頁面中。
# These assets are from Gems which aren't included in every page.
# So they must be included here
# instead of in the application.js and css manifests.
config.assets.precompile += %w(a-gem.css a-gem.js b-gem.js)
# This block includes all js/css files, excluding gems,
# under: app/assets, vendor/assets, lib/assets
# unless they are partial files (e.g. "_file.sass")
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
aps = %w(/app/assets /vendor/assets /lib/assets)
if aps.any? {|ap| full_path.starts_with?("#{Rails.root}#{ap}")} &&
!path.starts_with?('_')
puts "\tIncluding: " + full_path.slice(Rails.root.to_path.size..-1)
true
else
puts "\tExcluding: " + full_path
false
end
else
false
end
}
雖然,你可能不希望這樣做,因爲你很可能是預編譯寶石資產的兩倍(基本上任何已在您的application.js或CSS \ = require'd)。該片段包括了所有JS/CSS文件,包括寶石,下:應用程序/資產,供應商/資產,LIB /資產,除非它們是局部的文件(例如, 「_file.sass」)
# This block includes all js/css files, including gems,
# under: app/assets, vendor/assets, lib/assets
# and excluding partial files (e.g. "_file.sass")
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
asset_paths = %w(app/assets vendor/assets lib/assets)
if (asset_paths.any? {|ap| full_path.include? ap}) &&
!path.starts_with?('_')
puts "\tIncluding: " + full_path
true
else
puts "\tExcluding: " + full_path
false
end
else
false
end
}
可能重複的[如何使用config.assets.precompile目錄而不是單個文件?](http://stackoverflow.com/questions/12613980/how-do-i-use-config-assets-precompile -for-directories-rather-single-files) – Jason 2014-10-16 13:45:01