You are here: Browse Railsplugins Asset Packager
= AssetPackager
JavaScript and CSS Asset Compression for Production Rails Apps
DescriptionWhen 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.
Because not all browsers will dependably cache JavaScript and CSS files with query string parameters, AssetPackager writes a timestamp or subversion revision stamp (if available) into the merged file names. Therefore files are correctly cached by the browser AND your users always get the latest version when you re-deploy.
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!
This Rails Plugin was inspired by Cal Henderson’s article “Serving JavaScript Fast” on Vitamin: http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
It also uses the Ruby JavaScript Minifier created by Douglas Crockford. http://www.crockford.com/javascript/jsmin.html
Key Features1. Download and install the plugin: ./script/plugin install http://sbecker.net/shared/plugins/asset_packager
2. 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 file packages.
IMPORTANT: JavaScript files can break once compressed if each statement doesn't end with a semi-colon. The minifier puts multiple statements on one line, so if the semi-colon is missing, the statement may no longer makes sense and cause a syntax error.
Example from a fresh rails app after running the rake task. (Stylesheets is blank because a default rails app has no stylesheets yet.):
Example with multiple merged files:
3. 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.
4. Use the helper functions whenever including these files in your application. See below for examples.
5. Potential warning: css compressor function currently removes CSS comments. This might blow away some CSS hackery. To disable comment removal, comment out /lib/synthesis/asset_package.rb line 176.
JavaScript ExamplesExample call: <%= javascript_include_merged ‘prototype’, ‘effects’, ‘controls’, ‘dragdrop’, ‘application’, ‘foo’, ‘bar’ %>
In development, this generates: <link href=”/stylesheets/screen.css” rel=”Stylesheet” type=”text/css” media=”screen” /> <link href=”/stylesheets/header.css” rel=”Stylesheet” type=”text/css” media=”screen” />
In production this generates: <link href=”/stylesheets/base_1150729166.css” rel=”Stylesheet” type=”text/css” media=”screen” />
Different CSS Media
All options for stylesheet_link_tag still work, so if you want to specify a different media type: <%= stylesheet_link_merged :secondary, ‘media’ => ‘print’ %>
License Copyright© 2006 Scott Becker – http://synthesis.sbecker.netPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly