rake-minify

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

Installation

First, install the gem:

geminstallrake-minify

Or if your are using Bundler:

echo"gem 'rake-minify'">>Gemfilebundleinstall

Minifying a single JavaScript file

Open your Rakefile and add:

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

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)dodir("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")endendend

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)dodir("path/to/your/dir")do# we specify only the source directory
group("output-first-second.js")doadd("first.js",:minify=>false)# this file is not minified
add("second.js")endendend

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:

geminstallcoffee-script

Or if your are using Bundler:

echo"gem 'coffee-script'">>Gemfilebundleinstall

Then open your Rakefile and add:

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

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)dodir("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!'"endendend

With a group:

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

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.