Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
JavaScript and CSS Asset Compression for Production Rails Apps
Ruby JavaScript
Branch: master

This branch is 5 commits ahead of sbecker:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
CHANGELOG
README
Rakefile
about.yml
init.rb
install.rb

README

= JSPackager

JavaScript Compression for Production Rails Apps. Still Work in progress!!!

== Description

When it comes time to deploy your new web application, instead of 
sending down a dozen JavaScript files full of formatting
and comments, this Rails plugin makes it simple to merge and 
compress JavaScript 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!

== Credit

This Rails Plugin is fork of Scott Becker's AssetPackager (http://github.com/sbecker/asset_packager).

It also uses the Ruby JavaScript Minifier created by 
Douglas Crockford.
www.crockford.com

== Key Features

* Merges and compresses JavaScript 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 files in your views.
* YAML configuration file for mapping JavaScript 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:
   rails plugin install git://github.com/burmajam/js_packager.git

2. Run the rake task "js:packager:create_yml" to generate the /config/js_packages.yml
file the first time. You will need to reorder files under 'application' so dependencies are loaded 
in correct order. Feel free to rename or create new file packages.

3. Also /app/javascripts folder is created. It is root for js files that will be compressed

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.

== Examples of config/js_packages.yml

Example from a fresh rails app after running the rake task:

--- 
base:
- prototype
- effects
- dragdrop
- controls
- application
- controllers/*
- views/**/*

Multiple packages:

---
base:
- prototype
- effects
- controls
- dragdrop
- application
secondary:
- foo
- bar
- views/**/*


3. Run the rake task "js:packager:build" 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. That's 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.

4. Use the helper functions whenever including these files in your application. See below for examples.

== JavaScript Examples

Example call (based on above /config/js_packages.yml):
  <%= javascript_include_merged :base %>

In development, this generates: #NOT done yet
  <script type="text/javascript" src="/javascripts/prototype.js?1228027240"></script>
  <script type="text/javascript" src="/javascripts/effects.js?1228027240"></script>
  <script type="text/javascript" src="/javascripts/controls.js?1228027240"></script>
  <script type="text/javascript" src="/javascripts/dragdrop.js?1228027240"></script>
  <script type="text/javascript" src="/javascripts/application.js?1228027240"></script>

In production, this generates: 
  <script type="text/javascript" src="/javascripts/base_packaged.js?123456789"></script>

== Rake tasks

rake js:packager:build       # Merge and compress .js files
rake js:packager:create_yml       # Generate js_packages.yml from existing assets
rake js:packager:delete_builds       # Delete all js builds

== Running the tests

This plugin has a full suite of tests. But since they
depend on rails, it has to be run in the context of a
rails app, in the vendor/plugins directory. Observe:

> rails newtestapp
> cd newtestapp
> ./script/plugin install ./script/plugin install git://github.com/burmajam/js_packager.git
> rake test:plugins PLUGIN=js_packager # all tests pass

== License
Copyright (c) 2010 Milan Burmaja - http://github.com/burmajam

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:

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.
Something went wrong with that request. Please try again.