rake-minify

Rake Minify is an extremely simple solution for minifying javascript and coffeescript files using a rake task.

<img src=“https://secure.travis-ci.org/mcollina/rake-minify.png?branch=master” alt=“Build Status” />

Installation

First, install the gem:

gem install rake-minify

Or if your are using Bundler:

echo "gem 'rake-minify'" >> Gemfile
bundle install

Minifying a single JavaScript file

Open your Rakefile and add:

require 'rake/minify'
Rake::Minify.new(:minify_single) do
  dir("path/to/your/dir") do # we specify only the source directory
    add("output.js", "source.js") # the output file name is full path
  end
end

This task minify the file “path/to/your/dir/source.js” into the file “output.js”.

Minify multiple JavaScript files

Open your Rakefile and add:

require 'rake/minify'
Rake::Minify.new(:minify_multiple) do
  dir("path/to/your/dir") do # we specify only the source directory
    group("output-multi.js") do # the output file name is full path
      add("first.js")
      add("second.js")
    end
  end
end

This task packs and minify the files “path/to/your/dir/first.js” and “path/to/your/dir/second.js” into the file “output-multi.js”.

Combine only multiple JavaScript files

Open your Rakefile and add:

require 'rake/minify'
Rake::Minify.new(:minify_and_combine) do
  dir("path/to/your/dir") do # we specify only the source directory
    group("output-first-second.js") do
      add("first.js", :minify => false) # this file is not minified
      add("second.js")
    end
  end
end

CoffeeScript support

The rake-minify gem now supports even CoffeeScript files to be compiled, minified and bundled, just include them like javascript files.

CoffeeScript support requires the coffe-script gem, so install the gem:

gem install coffee-script

Or if your are using Bundler:

echo "gem 'coffee-script'" >> Gemfile
bundle install

Then open your Rakefile and add:

require 'rake/minify'
Rake::Minify.new(:minify_single) do
  dir("path/to/your/dir") do # we specify only the source directory
    add("output.js", "source.coffee") # the coffee file is compiled and minified
  end
end

Bare switch

Since version 0.3.0 rake-minify support the configuration of the coffee script bare switch (see jashkenas.github.com/coffee-script/#installation for details). However in the 0.2.x series that flag defaulted to true, now it defaults to false.

You can specify it:

require 'rake/minify'
Rake::Minify.new(:minify_single) do
  # the coffee file is compiled and minified
  # without the top-level function safety wrapper.
  add("output.js", "source.coffee", :bare => true)
end

Combine from ruby block

With a single file:

require 'rake/minify'
Rake::Minify.new(:minify_single) do
  dir("path/to/your/dir") do # we specify only the source directory
    add("output.js") do
      # this is evaluated during the build
      "var a = 'hello js!'"
    end 
  end
end

With a group:

require 'rake/minify'
Rake::Minify.new(:minify_and_combine) do
  dir("path/to/your/dir") do # we specify only the source directory
    group("output-first-second.js") do
      add(:minify => false) do
        # this is evaluated during the build
        "var a = 'hello js!'"
      end 

      add(:coffeescript => true) do
        # this is evaluated as coffescript
        "(a -> 'hello coffee!')()"
      end
    end
  end
end

Contributing to rake-minify

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Matteo Collina. See LICENSE.txt for further details.