Permalink
Browse files

Allow assets to be concatenated unprocessed to generate a digest of t…

…heir sources.

This lets us determine if an asset's sources have changed.
If not, then we don't need to recompile and recompress that asset.
If :process => false is passed as an option, the only processors
that are run are Sprockets Directives and ERB.
Other processors, such as coffeescript and compression, are removed.
  • Loading branch information...
ndbroadbent committed Sep 26, 2012
1 parent 2daf79b commit 675f65749fc795790d5dcf11e7bc2bd2a50fba27
View
@@ -13,6 +13,8 @@ def self.from_hash(environment, hash)
BundledAsset
when 'ProcessedAsset'
ProcessedAsset
+ when 'UnprocessedAsset'
+ UnprocessedAsset
when 'StaticAsset'
StaticAsset
else
@@ -29,7 +29,7 @@ def search_paths
# Reverse guess logical path for fully expanded path.
#
# This has some known issues. For an example if a file is
- # shaddowed in the path, but is required relatively, its logical
+ # shadowed in the path, but is required relatively, its logical
# path will be incorrect.
def logical_path
if root_path = environment.paths.detect { |path| pathname.to_s[path] }
@@ -0,0 +1,138 @@
+require 'sprockets/asset'
+require 'sprockets/utils'
+
+module Sprockets
+ # `AssetWithDependencies` is the base class for `ProcessedAsset` and `UnprocessedAsset`.
+ class AssetWithDependencies < Asset
+
+ # :dependency_digest is used internally to check equality
+ attr_reader :dependency_digest, :source
+
+
+ # Initialize asset from serialized hash
+ def init_with(environment, coder, asset_options = {})
+ asset_options[:bundle] = false
+
+ super(environment, coder)
+
+ @source = coder['source']
+ @dependency_digest = coder['dependency_digest']
+
+ @required_assets = coder['required_paths'].map { |p|
+ p = expand_root_path(p)
+
+ unless environment.paths.detect { |path| p[path] }
+ raise UnserializeError, "#{p} isn't in paths"
+ end
+
+ p == pathname.to_s ? self : environment.find_asset(p, asset_options)
+ }
+ @dependency_paths = coder['dependency_paths'].map { |h|
+ DependencyFile.new(expand_root_path(h['path']), h['mtime'], h['digest'])
+ }
+ end
+
+ # Serialize custom attributes.
+ def encode_with(coder)
+ super
+
+ coder['source'] = source
+ coder['dependency_digest'] = dependency_digest
+
+ coder['required_paths'] = required_assets.map { |a|
+ relativize_root_path(a.pathname).to_s
+ }
+ coder['dependency_paths'] = dependency_paths.map { |d|
+ { 'path' => relativize_root_path(d.pathname).to_s,
+ 'mtime' => d.mtime.iso8601,
+ 'digest' => d.digest }
+ }
+ end
+
+ # Checks if Asset is stale by comparing the actual mtime and
+ # digest to the inmemory model.
+ def fresh?(environment)
+ # Check freshness of all declared dependencies
+ @dependency_paths.all? { |dep| dependency_fresh?(environment, dep) }
+ end
+
+ protected
+ class DependencyFile < Struct.new(:pathname, :mtime, :digest)
+ def initialize(pathname, mtime, digest)
+ pathname = Pathname.new(pathname) unless pathname.is_a?(Pathname)
+ mtime = Time.parse(mtime) if mtime.is_a?(String)
+ super
+ end
+
+ def eql?(other)
+ other.is_a?(DependencyFile) &&
+ pathname.eql?(other.pathname) &&
+ mtime.eql?(other.mtime) &&
+ digest.eql?(other.digest)
+ end
+
+ def hash
+ pathname.to_s.hash
+ end
+ end
+
+ private
+ def build_required_assets(environment, context, asset_options = {})
+ @required_assets = resolve_dependencies(environment, context._required_paths + [pathname.to_s], asset_options) -
+ resolve_dependencies(environment, context._stubbed_assets.to_a, asset_options)
+ end
+
+ def resolve_dependencies(environment, paths, asset_options = {})
+ asset_options[:bundle] = false
+ assets = []
+ cache = {}
+
+ paths.each do |path|
+ if path == self.pathname.to_s
+ unless cache[self]
+ cache[self] = true
+ assets << self
+ end
+ elsif asset = environment.find_asset(path, asset_options)
+ asset.required_assets.each do |asset_dependency|
+ unless cache[asset_dependency]
+ cache[asset_dependency] = true
+ assets << asset_dependency
+ end
+ end
+ end
+ end
+
+ assets
+ end
+
+ def build_dependency_paths(environment, context, asset_options = {})
+ asset_options[:bundle] = false
+ dependency_paths = {}
+
+ context._dependency_paths.each do |path|
+ dep = DependencyFile.new(path, environment.stat(path).mtime, environment.file_digest(path).hexdigest)
+ dependency_paths[dep] = true
+ end
+
+ context._dependency_assets.each do |path|
+ if path == self.pathname.to_s
+ dep = DependencyFile.new(pathname, environment.stat(path).mtime, environment.file_digest(path).hexdigest)
+ dependency_paths[dep] = true
+ elsif asset = environment.find_asset(path, asset_options)
+ asset.dependency_paths.each do |d|
+ dependency_paths[d] = true
+ end
+ end
+ end
+
+ @dependency_paths = dependency_paths.keys
+ end
+
+ def compute_dependency_digest(environment)
+ required_assets.inject(environment.digest) { |digest, asset|
+ digest.update asset.digest
+ }.hexdigest
+ end
+ end
+end
View
@@ -2,6 +2,7 @@
require 'sprockets/bundled_asset'
require 'sprockets/caching'
require 'sprockets/processed_asset'
+require 'sprockets/unprocessed_asset'
require 'sprockets/processing'
require 'sprockets/server'
require 'sprockets/static_asset'
@@ -246,18 +247,23 @@ def build_asset(logical_path, pathname, options)
if attributes_for(pathname).processors.any?
if options[:bundle] == false
circular_call_protection(pathname.to_s) do
- ProcessedAsset.new(index, logical_path, pathname)
+ if options[:process] == false
+ UnprocessedAsset.new(index, logical_path, pathname)
+ else
+ ProcessedAsset.new(index, logical_path, pathname)
+ end
end
else
- BundledAsset.new(index, logical_path, pathname)
+ BundledAsset.new(index, logical_path, pathname, options)
end
else
StaticAsset.new(index, logical_path, pathname)
end
end
def cache_key_for(path, options)
- "#{path}:#{options[:bundle] ? '1' : '0'}"
+ options[:process] = true unless options.key?(:process)
+ "#{path}:#{options[:bundle] ? '1' : '0'}:#{options[:process] ? '1' : '0'}"
end
def circular_call_protection(path)
@@ -10,21 +10,25 @@ module Sprockets
class BundledAsset < Asset
attr_reader :source
- def initialize(environment, logical_path, pathname)
+ def initialize(environment, logical_path, pathname, options = {})
super(environment, logical_path, pathname)
+ @process = options.fetch(:process, true)
- @processed_asset = environment.find_asset(pathname, :bundle => false)
- @required_assets = @processed_asset.required_assets
+ @asset = environment.find_asset(pathname, :bundle => false, :process => @process)
+ @required_assets = @asset.required_assets
+ @dependency_paths = @asset.dependency_paths
@source = ""
# Explode Asset into parts and gather the dependency bodies
to_a.each { |dependency| @source << dependency.to_s }
- # Run bundle processors on concatenated source
- context = environment.context_class.new(environment, logical_path, pathname)
- @source = context.evaluate(pathname, :data => @source,
- :processors => environment.bundle_processors(content_type))
+ if @process
+ # Run bundle processors on concatenated source
+ context = environment.context_class.new(environment, logical_path, pathname)
+ @source = context.evaluate(pathname, :data => @source,
+ :processors => environment.bundle_processors(content_type))
+ end
@mtime = to_a.map(&:mtime).max
@length = Rack::Utils.bytesize(source)
@@ -35,10 +39,10 @@ def initialize(environment, logical_path, pathname)
def init_with(environment, coder)
super
- @processed_asset = environment.find_asset(pathname, :bundle => false)
- @required_assets = @processed_asset.required_assets
+ @asset = environment.find_asset(pathname, :bundle => false)
+ @required_assets = @asset.required_assets
- if @processed_asset.dependency_digest != coder['required_assets_digest']
+ if @asset.dependency_digest != coder['required_assets_digest']
raise UnserializeError, "processed asset belongs to a stale environment"
end
@@ -50,19 +54,19 @@ def encode_with(coder)
super
coder['source'] = source
- coder['required_assets_digest'] = @processed_asset.dependency_digest
+ coder['required_assets_digest'] = @asset.dependency_digest
end
# Get asset's own processed contents. Excludes any of its required
# dependencies but does run any processors or engines on the
# original file.
def body
- @processed_asset.source
+ @asset.source
end
# Return an `Array` of `Asset` files that are declared dependencies.
def dependencies
- to_a.reject { |a| a.eql?(@processed_asset) }
+ to_a.reject { |a| a.eql?(@asset) }
end
# Expand asset into an `Array` of parts.
@@ -73,7 +77,7 @@ def to_a
# Checks if Asset is stale by comparing the actual mtime and
# digest to the inmemory model.
def fresh?(environment)
- @processed_asset.fresh?(environment)
+ @asset.fresh?(environment)
end
end
end
@@ -12,7 +12,7 @@
module Sprockets
class Environment < Base
- # `Environment` should initialized with your application's root
+ # `Environment` should be initialized with your application's root
# directory. This should be the same as your Rails or Rack root.
#
# env = Environment.new(Rails.root)
@@ -71,8 +71,9 @@ def index
# Cache `find_asset` calls
def find_asset(path, options = {})
options[:bundle] = true unless options.key?(:bundle)
+ options[:process] = true unless options.key?(:process)
- # Ensure inmemory cached assets are still fresh on every lookup
+ # Ensure in-memory cached assets are still fresh on every lookup
if (asset = @assets[cache_key_for(path, options)]) && asset.fresh?(self)
asset
elsif asset = index.find_asset(path, options)
View
@@ -54,7 +54,9 @@ def file_digest(pathname)
# Cache `find_asset` calls
def find_asset(path, options = {})
- options[:bundle] = true unless options.key?(:bundle)
+ options[:bundle] = true unless options.key?(:bundle)
+ options[:process] = true unless options.key?(:process)
+
if asset = @assets[cache_key_for(path, options)]
asset
elsif asset = super
Oops, something went wrong.

0 comments on commit 675f657

Please sign in to comment.