Asset_packager, boost your rails app with a simply plugin
Description
When it comes time to deploy your new web application, instead of sending down a dozen javascript and css files full of formatting and comments, this Rails plugin makes it simple to merge and compress JavaScript and CSS down into one or more files, increasing speed and saving bandwidth.
When in development, it allows you to use your original versions and retain formatting and comments for readability and debugging.
This code is released under the MIT license (like Ruby). You’re free to rip it up, enhance it, etc. And if you make any enhancements, I’d like to know so I can add them back in. Thanks!
Key Features
- Merges and compresses JavaScript and CSS when running in production.
- Uses uncompressed originals when running in development.
- Generates packages on demand in production
Components
- Rake tasks for managing packages
- Helper functions for including these JavaScript and CSS files in your views.
- YAML configuration file for mapping JavaScript and CSS files to packages.
- Rake Task for auto-generating the YAML file from your existing JavaScript files.
How to Use
1. Download and install the plugin:
script/plugin install git://github.com/sbecker/asset_packager.git2. Run the rake task asset:packager:create_yml to generate the /config/asset_packages.yml file the first time. You will need to reorder files under ‘base’ so dependencies are loaded in correct order. Feel free to rename or create new packages.
Examples of config/asset_packages.yml
Example from a fresh rails app after running the rake task. (Stylesheets is blank because a default rails app has no stylesheets yet):
---
javascripts:
- base:
- prototype
- effects
- dragdrop
- controls
- application
stylesheets:
- base: []Multiple packages:
---
javascripts:
- base:
- prototype
- effects
- controls
- dragdrop
- application
- secondary:
- foo
- bar
stylesheets:
- base:
- screen
- header
- secondary:
- foo
- bar3. Run the rake task asset:packager:build_all to generate the compressed, merged versions for each package. Whenever you rearrange the yaml file, you’ll need to run this task again.
Merging and compressing is expensive, so this is something we want to do once, not every time your app starts. Thats why its a rake task. You can run this task via Capistrano when deploying to avoid an initially slow request the first time a page is generated.
Note: The package will be generated on the fly if it doesn’t yet exist, so you don’t need to run the rake task when deploying, its just recommended for speeding up initial requests.
3. Use the helper function whenever including these files in your application. See examples Below.
Javascript Examples:
Example calls
- either by package name:
<%= javascript_include_merged :base %>- or by the individual assets:
<%= javascript_include_merged 'prototype', 'effects', 'controls', 'dragdrop', 'application' %>Output in development:
Output in production:
Symbols work too, as does :defaults
<%= javascript_include_merged :defaults %>
<%= javascript_include_merged :foo, :bar %>Stylesheet Examples
Example call:
- either by package name:
<%= stylesheet_link_merged :base %>- or by the individual assets:
<%= stylesheet_link_merged 'screen', 'header' %>Output in development:
Output in production:
Capistrano 2.X Integration
Building the packages at deployment time will help speed up initial requests. This is not strictly necessary as packages will be built on the fly if they are not found.
If you want to run the asset:packager:build_all task at deployment time within Capistrano, add this task to your Capistrano recipe:
namespace :deploy do
desc "Create asset packages for production"
task :after_update_code, :roles => [:web] do
run <<-EOF
cd #{release_path} && rake asset:packager:build_all
EOF
end
end-Contributions
- Source Code – The source for Asset Packager is now hosted on GitHub – http://github.com/sbecker/asset_packager
Thanks to Scott Becker!
- Person:
- Programming Language:
- Tags:
- Add new comment
- 124 reads
- Feed: barison.org
- Original article


Recent comments
1 year 23 weeks ago
1 year 23 weeks ago
1 year 25 weeks ago
1 year 27 weeks ago
1 year 42 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 46 weeks ago
1 year 48 weeks ago