3 JavaScript and CSS Asset Compression for Production Rails Apps
7 When it comes time to deploy your new web application, instead of
8 sending down a dozen JavaScript and CSS files full of formatting
9 and comments, this Rails plugin makes it simple to merge and
10 compress JavaScript and CSS down into one or more files, increasing
11 speed and saving bandwidth.
13 When in development, it allows you to use your original versions
14 and retain formatting and comments for readability and debugging.
16 Because not all browsers will dependably cache JavaScript and CSS
17 files with query string parameters, AssetPackager writes a timestamp
18 or subversion revision stamp (if available) into the merged file names.
19 Therefore files are correctly cached by the browser AND your users
20 always get the latest version when you re-deploy.
22 This code is released under the MIT license (like Ruby). You’re free
23 to rip it up, enhance it, etc. And if you make any enhancements,
24 I’d like to know so I can add them back in. Thanks!
26 * Formerly known as MergeJS.
30 This Rails Plugin was inspired by Cal Henderson's article
31 "Serving JavaScript Fast" on Vitamin:
32 http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
34 It also uses the Ruby JavaScript Minifier created by
36 http://www.crockford.com/javascript/jsmin.html
40 * Merges and compresses JavaScript and CSS when running in production.
41 * Uses uncompressed originals when running in development.
42 * Handles caching correctly. (No querystring parameters - filename timestamps)
43 * Versions each package individually. Updates to files in one won't re-trigger downloading the others.
44 * Uses subversion revision numbers instead of timestamps if within a subversion controlled directory.
45 * Guarantees new version will get downloaded the next time you deploy.
49 * Rake Task for merging and compressing JavaScript and CSS files.
50 * Helper functions for including these JavaScript and CSS files in your views.
51 * YAML configuration file for mapping JavaScript and CSS files to merged versions.
52 * Rake Task for auto-generating the YAML file from your existing JavaScript files.
56 1. Download and install the plugin:
57 ./script/plugin install http://sbecker.net/shared/plugins/asset_packager
59 2. Run the rake task "asset:packager:create_yml" to generate the /config/asset_packages.yml
60 file the first time. You will need to reorder files under 'base' so dependencies are loaded
61 in correct order. Feel free to rename or create new file packages.
63 IMPORTANT: JavaScript files can break once compressed if each statement doesn't end with a semi-colon.
64 The minifier puts multiple statements on one line, so if the semi-colon is missing, the statement may no
65 longer makes sense and cause a syntax error.
67 Example from a fresh rails app after running the rake task. (Stylesheets is blank because a
68 default rails app has no stylesheets yet.):
81 Example with multiple merged files:
102 3. Run the rake task "asset:packager:build_all" to generate the compressed, merged versions
103 for each package. Whenever you rearrange the yaml file, you'll need to run this task again.
104 Merging and compressing is expensive, so this is something we want to do once, not every time
105 your app starts. Thats why its a rake task.
107 4. Use the helper functions whenever including these files in your application. See below for examples.
109 5. Potential warning: css compressor function currently removes CSS comments. This might blow
110 away some CSS hackery. To disable comment removal, comment out /lib/synthesis/asset_package.rb line 176.
112 == JavaScript Examples
115 <%= javascript_include_merged 'prototype', 'effects', 'controls', 'dragdrop', 'application', 'foo', 'bar' %>
117 In development, this generates:
118 <script type="text/javascript" src="/javascripts/prototype.js"></script>
119 <script type="text/javascript" src="/javascripts/effects.js"></script>
120 <script type="text/javascript" src="/javascripts/controls.js"></script>
121 <script type="text/javascript" src="/javascripts/dragdrop.js"></script>
122 <script type="text/javascript" src="/javascripts/application.js"></script>
123 <script type="text/javascript" src="/javascripts/foo.js"></script>
124 <script type="text/javascript" src="/javascripts/bar.js"></script>
126 In production, this generates:
127 <script type="text/javascript" src="/javascripts/base_1150571523.js"></script>
128 <script type="text/javascript" src="/javascripts/secondary_1150729166.js"></script>
130 Now supports symbols and :defaults as well:
131 <%= javascript_include_merged :defaults %>
132 <%= javascript_include_merged :foo, :bar %>
134 == Stylesheet Examples
137 <%= stylesheet_link_merged 'screen', 'header' %>
139 In development, this generates:
140 <link href="/stylesheets/screen.css" media="screen" rel="Stylesheet" type="text/css" />
141 <link href="/stylesheets/header.css" media="screen" rel="Stylesheet" type="text/css" />
143 In production this generates:
144 <link href="/stylesheets/base_1150729166.css" media="screen" rel="Stylesheet" type="text/css" />
146 == Different CSS Media
148 All options for stylesheet_link_tag still work, so if you want to specify a different media type:
149 <%= stylesheet_link_merged :secondary, 'media' => 'print' %>
153 So you want to run the tests eh? Ok, then listen:
155 This plugin has a full suite of tests. But you've gotta follow a
156 certain procedure. If you do, all tests should pass. You cant run
157 them all at once (unfortunately) by running "rake" because the
158 tests depend on ENV['RAILS_ENV'] being different, and as far as I
159 know, this can't be changed in a running script once it is set. So,
160 we run them separately. Observe:
164 > ./script/plugin install http://sbecker.net/shared/plugins/asset_packager
165 > cd vendor/plugins/asset_packager/test/
166 > ruby asset_packager_test.rb # all pass
167 > ruby asset_package_helper_development_test.rb # all pass
168 > ruby asset_package_helper_production_test.rb # all pass
171 Copyright (c) 2006 Scott Becker - http://synthesis.sbecker.net
172 Contact Email: becker.scott@gmail.com
174 Permission 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:
176 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
178 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.