-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect asset changes by saving source_digests in manifest.yml, and speed up non-digest assets #7866
Closed
ndbroadbent
wants to merge
1
commit into
rails:3-2-stable
from
ndbroadbent:rails3_stable_assets_speedup
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
actionpack/lib/sprockets/static_non_digest_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
require 'fileutils' | ||
|
||
module Sprockets | ||
class StaticNonDigestGenerator | ||
|
||
DIGEST_REGEX = /-([0-9a-f]{32})\./ | ||
|
||
attr_accessor :env, :target, :paths | ||
|
||
def initialize(env, target, paths, options = {}) | ||
@env = env | ||
@target = target | ||
@paths = paths | ||
@digests = options.fetch(:digests, {}) | ||
|
||
# Parse digests from digests hash | ||
@asset_digests = Hash[*@digests.map {|file, digest_file| | ||
[file, digest_file[DIGEST_REGEX, 1]] | ||
}.flatten] | ||
end | ||
|
||
|
||
# Generate non-digest assets by making a copy of the digest asset, | ||
# with digests stripped from js and css. The new files are also gzipped. | ||
# Other assets are copied verbatim. | ||
def generate | ||
start_time = Time.now.to_f | ||
|
||
env.each_logical_path(paths) do |logical_path| | ||
digest_path = @digests[logical_path] | ||
abs_digest_path = "#{@target}/#{digest_path}" | ||
abs_logical_path = "#{@target}/#{logical_path}" | ||
|
||
mtime = File.mtime(abs_digest_path) | ||
|
||
# Remove known digests from css & js | ||
if abs_digest_path.match(/\.(?:js|css)$/) | ||
asset_body = File.read(abs_digest_path) | ||
|
||
# Find all hashes in the asset body with a leading '-' | ||
asset_body.gsub!(DIGEST_REGEX) do |match| | ||
# Only remove if known digest | ||
$1.in?(@asset_digests.values) ? '.' : match | ||
end | ||
|
||
# Write non-digest file | ||
File.open abs_logical_path, 'w' do |f| | ||
f.write asset_body | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may change the file's encoding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have just tested with some Japanese characters, and didn't see any problems with encoding. |
||
# Set modification and access times | ||
File.utime(File.atime(abs_digest_path), mtime, abs_logical_path) | ||
|
||
# Also write gzipped asset | ||
File.open("#{abs_logical_path}.gz", 'wb') do |f| | ||
gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION) | ||
gz.mtime = mtime.to_i | ||
gz.write asset_body | ||
gz.close | ||
end | ||
|
||
env.logger.debug "Stripped digests, copied to #{logical_path}, and created gzipped asset" | ||
|
||
else | ||
# Otherwise, treat file as binary and copy it. | ||
# Ignore paths that have no digests, such as READMEs | ||
unless abs_digest_path == abs_logical_path | ||
FileUtils.cp_r abs_digest_path, abs_logical_path, :remove_destination => true | ||
env.logger.debug "Copied binary asset to #{logical_path}" | ||
|
||
# Copy gzipped asset if exists | ||
if File.exist? "#{abs_digest_path}.gz" | ||
FileUtils.cp_r "#{abs_digest_path}.gz", "#{abs_logical_path}.gz", :remove_destination => true | ||
env.logger.debug "Copied gzipped asset to #{logical_path}.gz" | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
elapsed_time = ((Time.now.to_f - start_time) * 1000).to_i | ||
env.logger.debug "Generated non-digest assets in #{elapsed_time}ms" | ||
end | ||
|
||
private | ||
|
||
def compile_path?(logical_path) | ||
paths.each do |path| | ||
case path | ||
when Regexp | ||
return true if path.match(logical_path) | ||
when Proc | ||
return true if path.call(logical_path) | ||
else | ||
return true if File.fnmatch(path.to_s, logical_path) | ||
end | ||
end | ||
false | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above -- always cleaning before generating assets means we we're orphaning references to old assets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Asset cleaning has been removed from the pull request, please see above