Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use minor version dependencies now, and making the README pretty
- Loading branch information
1 parent
a3b82c9
commit 4a2ca6d
Showing
3 changed files
with
92 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,118 @@ | ||
# Rack::Pack | ||
Rack::Pack | ||
========== | ||
|
||
Rack::Pack is a piece of Rack Middleware that packages and optionally compresses assets such as javascripts and stylesheets into single files. In a development environment, assets will be packaged on each request if there have been changes to the source files. In a production environment, assets will only be packaged one time, and only if there have been changes. | ||
|
||
### Why? | ||
|
||
I've tried a dozen different asset packaging solutions including AssetPackager, BundleFu, Jammit, Sprockets, etc...none of which were quite what I wanted. I didn't need any helpers, controllers, embedded images, rake tasks, or Yaml config files. I just wanted something to take my assets and package them into one file, and you're looking at it. | ||
|
||
## Installation | ||
Installation | ||
------------ | ||
|
||
gem install rack-pack | ||
``` bash | ||
$ gem install rack-pack | ||
``` | ||
|
||
## Basic Usage | ||
|
||
require 'rack-pack' | ||
use Rack::Pack | ||
Basic Usage | ||
----------- | ||
|
||
or in Rails: | ||
``` ruby | ||
require 'rack-pack' | ||
use Rack::Pack | ||
``` | ||
|
||
# Gemfile | ||
gem 'rack-pack' | ||
or in Rails: | ||
|
||
# config/application.rb | ||
config.middleware.use Rack::Pack | ||
``` ruby | ||
# Gemfile | ||
gem 'rack-pack' | ||
|
||
# config/application.rb | ||
config.middleware.use Rack::Pack | ||
``` | ||
|
||
### Packages | ||
Packages | ||
-------- | ||
|
||
Two files will be packaged out of the box: `javascripts/application.js` & `stylesheets/application.css`. Rack::Pack will look in `vendor/javascripts`, `app/javascripts`, & `./javascripts` for any .js files and `vendor/stylesheets`, `app/stylesheets`, & `./stylesheets` for any .css files. These files will be packaged in the order they're found. | ||
|
||
To create your own packages, pass in the name of the output file and the source files as options: | ||
|
||
use Rack::Pack, 'js/main.js' => [ | ||
'vendor/javascripts/jquery.js', | ||
'vendor/javascripts/swfobject.js, | ||
'app/javascripts/misc.js', | ||
'app/javascripts/main.js' | ||
] | ||
# Creates a 'public/js/main.js' file | ||
|
||
``` ruby | ||
use Rack::Pack, 'js/main.js' => [ | ||
'vendor/javascripts/jquery.js', | ||
'vendor/javascripts/swfobject.js, | ||
'app/javascripts/misc.js', | ||
'app/javascripts/main.js' | ||
] | ||
# Creates a 'public/js/main.js' file | ||
``` | ||
Notice how the output file is relative to the public dir. By default this is just `'public'`, but this can be changed using the `:public_dir` option: | ||
|
||
use Rack::Pack, :public_dir => 'html', 'js/main.js' => %w(js/plugins.js js/main.js) | ||
# Creates a 'html/js/main.js' file | ||
``` ruby | ||
use Rack::Pack, :public_dir => 'html', 'js/main.js' => %w(js/plugins.js js/main.js) | ||
# Creates a 'html/js/main.js' file | ||
``` | ||
You can also pass a glob string for the source files. This string will be used to search for new files on each request. The downside is the source files will be concatenated in the order they're found. | ||
|
||
use Rack::Pack, 'assets/scripts.js' => 'app/js/**/*.js' | ||
|
||
``` ruby | ||
use Rack::Pack, 'assets/scripts.js' => 'app/js/**/*.js' | ||
``` | ||
|
||
In fact, this is how the default packages are declared: | ||
|
||
use Rack::Pack, | ||
'javascripts/application.js' => '{vendor,app,.}/javascripts/*.js', | ||
'stylesheets/application.css' => '{vendor,app,.}/stylesheets/*.css' | ||
|
||
``` ruby | ||
use Rack::Pack, | ||
'javascripts/application.js' => '{vendor,app,.}/javascripts/*.js', | ||
'stylesheets/application.css' => '{vendor,app,.}/stylesheets/*.css' | ||
``` | ||
|
||
Beautiful, isn't it? I don't think you can get simpler than that. No Yaml config files or rake tasks. You'll set it up once then forget about it completely. Well unless you have to add a new source file and you were explicity setting your source files for a package, but whatever. | ||
### Compression | ||
Compression | ||
----------- | ||
What good would an asset packager be without compression? Rack::Pack determines which javascript compressor you want to use based on which one has been required. | ||
|
||
require 'packr' | ||
use Rack::Pack | ||
# would use Packr | ||
``` ruby | ||
require 'packr' | ||
use Rack::Pack | ||
# would use Packr | ||
``` | ||
or in Rails: | ||
|
||
# Gemfile | ||
gem 'jsmin' | ||
# config/application.rb | ||
config.middleware.use Rack::Pack | ||
# would use JSMin | ||
``` ruby | ||
# Gemfile | ||
gem 'jsmin' | ||
To pass options to the javascript compressor just use the `:js_compressor` option: | ||
# config/application.rb | ||
config.middleware.use Rack::Pack | ||
# would use JSMin | ||
``` | ||
require 'packr' | ||
use Rack::Pack, :js_compression => { :shrink_vars => true } | ||
To pass options to the javascript compressor just use the `:js_compressor` option: | ||
``` ruby | ||
require 'packr' | ||
use Rack::Pack, :js_compression => { :shrink_vars => true } | ||
``` | ||
By default, packages are only compressed in a production environment. If for some reason you want them to always be compressed, pass the `:always_compress` option: | ||
``` ruby | ||
use Rack::Pack, :always_compress => true | ||
``` | ||
use Rack::Pack, :always_compress => true | ||
|
||
## Heroku and other read-only filesystems | ||
Heroku and other read-only filesystems | ||
-------------------------------------- | ||
Because Rack::Pack relies on writing the packaged files, it won't package anything on Heroku. But, you could just check in your packaged files and push them to Heroku. I'll look into other options for future versions. | ||
## Copyright | ||
Copyright | ||
--------- | ||
Copyright (c) 2010 [Peter Browne](http://petebrowne.com). See LICENSE for details. | ||
Copyright (c) 2011 [Peter Browne](http://petebrowne.com). See LICENSE for details. |
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,5 +1,5 @@ | ||
module Rack | ||
class Pack | ||
VERSION = '0.3.1' | ||
VERSION = '0.3.2' | ||
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