-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change re-introduces compressed file generation and parallel file writing. Gzip file generation was taken out in this PR: sstephenson#589 This was then discussed in this issue: #26 It was decided that compressing assets to maximize compression ratio at compile time was a valuable feature and within the scope of sprockets. This is one possible implementation. Assets are written to disk in parallel using `Concurrent::Future`, since the gzip file cannot be generated until the original file is written to disk, it must process that file first. Speed impacts of writing files in parallel vary based on the number of assets being written to disk, disk speed, and IO contention. Gzipping can be turned off at the environment level. cc @fxn
- Loading branch information
Showing
8 changed files
with
184 additions
and
17 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
# Building an Asset Processing Framework | ||
|
||
This guide is for using a Sprockets::Environment to process assets. You would use this class directly if you were building a feature similar to Rail's asset pipeline. If you aren't building an asset processing frameworks, you will want to refer to the [End User Asset Generation](end_user_asset_generation.md) guide instead. For a reference use of `Sprockets::Environemnt` see [sprockets-rails](github.com/rails/sprockets-rails). | ||
This guide is for using a Sprockets::Environment to process assets. You would use this class directly if you were building a feature similar to Rail's asset pipeline. If you aren't building an asset processing frameworks, you will want to refer to the [End User Asset Generation](end_user_asset_generation.md) guide instead. For a reference use of `Sprockets::Environemnt` see [sprockets-rails](github.com/rails/Sprockets-rails). | ||
|
||
## Gzip | ||
|
||
By default when Sprockets generates a compiled asset file it will also produce a gzipped copy of that file. Sprockets only gzips non-binary files such as CSS, JavaScript, and SVG files. | ||
|
||
For example if Sprockets is generating | ||
|
||
``` | ||
application-12345.css | ||
``` | ||
|
||
Then it will also generate a compressed copy in | ||
|
||
``` | ||
application-12345.css.gz | ||
``` | ||
|
||
You can disable this behavior `Sprockets::Environemnt#gzip=` to something falsey for example: | ||
|
||
```ruby | ||
env = Sprockets::Environment.new(".") | ||
env.gzip = false | ||
``` | ||
|
||
## WIP | ||
|
||
This guide is a work in progress. There are many different groups of people who interact with sprockets. Some only need to know directive syntax to put in their asset files, some are building features like the Rails asset pipeline, and some are plugging into sprockets and writing things like preprocessors. The goal of these guides are to provide task specific guidance to make the expected behavior explicit. If you are using sprockets and you find missing information in these guides, please consider submitting a pull request with updated information. | ||
This guide is a work in progress. There are many different groups of people who interact with Sprockets. Some only need to know directive syntax to put in their asset files, some are building features like the Rails asset pipeline, and some are plugging into Sprockets and writing things like preprocessors. The goal of these guides are to provide task specific guidance to make the expected behavior explicit. If you are using Sprockets and you find missing information in these guides, please consider submitting a pull request with updated information. | ||
|
||
These guides live in [guides](/guides). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Sprockets | ||
module Utils | ||
class Gzip | ||
# Private: Generates a gzipped file based off of reference file. | ||
def initialize(asset) | ||
@content_type = asset.content_type | ||
@mtime = asset.mtime | ||
@source = asset.source | ||
@charset = asset.charset | ||
end | ||
|
||
# Private: Returns whether or not an asset can be compressed. | ||
# | ||
# We want to compress any file that is text based. | ||
# You do not want to compress binary | ||
# files as they may already be compressed and running them | ||
# through a compression algorithm would make them larger. | ||
# | ||
# Return Boolean. | ||
def can_compress?(mime_types) | ||
# The "charset" of a mime type is present if the value is | ||
# encoded text. We can check this value to see if the asset | ||
# can be compressed. | ||
# | ||
# SVG images are text but do not have | ||
# a charset defined, this is special cased. | ||
@charset || @content_type == "image/svg+xml".freeze | ||
end | ||
|
||
# Private: Opposite of `can_compress?`. | ||
# | ||
# Returns Boolean. | ||
def cannot_compress?(mime_types) | ||
!can_compress?(mime_types) | ||
end | ||
|
||
# Private: Generates a gzipped file based off of reference asset. | ||
# | ||
# Compresses the target asset's contents and puts it into a file with | ||
# the same name plus a `.gz` extension in the same folder as the original. | ||
# Does not modify the target asset. | ||
# | ||
# Returns nothing. | ||
def compress(target) | ||
PathUtils.atomic_write("#{target}.gz") do |f| | ||
gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION) | ||
gz.mtime = @mtime.to_i | ||
gz.write(@source) | ||
gz.close | ||
end | ||
|
||
nil | ||
end | ||
end | ||
end | ||
end |
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