Skip to content

Commit

Permalink
Add support for minification using YUI Compressor 2.4.2 and the coffe…
Browse files Browse the repository at this point in the history
…e_machine gem.
  • Loading branch information
samleb committed Mar 19, 2010
1 parent 1216278 commit d796713
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -49,6 +49,7 @@ begin
gemspec.homepage = "http://github.com/codespeaks/modulr"
gemspec.files = FileList["Rakefile", "README.markdown", "LICENSE", "VERSION", "{lib,bin,assets,example}/**/*", "vendor/rkelly/**/*"]
gemspec.executable = "modulrize"
gemspec.add_dependency("coffee_machine")
end
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install jeweler"
Expand Down
34 changes: 33 additions & 1 deletion bin/modulrize
Expand Up @@ -9,6 +9,9 @@ options = {
opts = OptionParser.new do |opts|
opts.banner = 'Usage: modulrize program.js [options] > output.js'

opts.separator ''
opts.separator 'Options:'

opts.on('-o', '--output=FILE', 'Write the output to FILE. Defaults to stdout.') do |output|
options[:output] = File.open(output, 'w')
end
Expand All @@ -24,10 +27,39 @@ opts = OptionParser.new do |opts|
options[:lazy_eval] = modules
end

opts.on_tail('-h', '--help', 'Show this message.') do
opts.on('--minify', 'Minify output using YUI Compressor.') do |minify|
options[:minify] = minify
end

opts.on('-h', '--help', 'Show this message.') do
puts opts
exit
end

opts.separator ''
opts.separator 'Minification options (these are forwarded to YUI Compressor without the "minify-" prefix):'

{
'line-break COLUMN' => 'Insert a line break after the specified column number.',
'verbose' => 'Display informational messages and warnings.',
'nomunge' => 'Minify only, do not obfuscate.',
'preserve-semi' => 'Preserve all semicolons.',
'disable-optimizations' => 'Disable all micro optimizations.'
}.each do |option, message|
prefixed_option = option.sub(/\A(\[no-\])?/, '--\\1minify-')

def normalize_option(option)
option.gsub('-', '_').to_sym
end

opts.on(prefixed_option, message) do |value|
if !options[:minify] || options[:minify] == true
options[:minify] = {}
end
options[:minify][normalize_option(option)] = value
end
end

end

opts.parse!
Expand Down
12 changes: 11 additions & 1 deletion lib/modulr.rb
Expand Up @@ -10,13 +10,23 @@ class ModulrError < StandardError
require 'modulr/js_module'
require 'modulr/parser'
require 'modulr/collector'
require 'modulr/minifier'
require 'modulr/version'

PATH_TO_MODULR_JS = File.join(LIB_DIR, '..', 'assets', 'modulr.js')

def self.ize(input_filename, options = {})
collector = Collector.new(options)
collector.parse_file(input_filename)
collector.to_js
minify(collector.to_js, options[:minify])
end

protected
def self.minify(output, options)
if options
Minifier.minify(output, options == true ? {} : options)
else
output
end
end
end
41 changes: 41 additions & 0 deletions lib/modulr/minifier.rb
@@ -0,0 +1,41 @@
module Modulr
class MinifierError < ModulrError
end

class Minifier
YUI_COMPRESSOR_PATH = File.join(File.dirname(__FILE__), '..', '..', 'vendor', 'yuicompressor-2.4.2.jar').freeze

def self.minify(input, options = {})
new(options).minify(input)
end

def initialize(options = {})
@options = options
end

def minify(input)
run_yui_compressor do |pipe, stderr|
pipe.write(input)
pipe.close_write
output, error = pipe.read, stderr.read
raise MinifierError, error unless error.empty?
output
end
end

protected
def run_yui_compressor(&block)
require 'coffee_machine'
CoffeeMachine.run_jar(YUI_COMPRESSOR_PATH, :args => yui_compressor_args, &block)
end

def yui_compressor_args
args = ['--type js']
@options.each do |option, value|
args << "--#{option.to_s.gsub('_', '-')}"
args << value unless value == true || value == false
end
args.join(' ')
end
end
end
Binary file added vendor/yuicompressor-2.4.2.jar
Binary file not shown.

0 comments on commit d796713

Please sign in to comment.