Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for minification using YUI Compressor 2.4.2 and the coffe…
…e_machine gem.
- Loading branch information
Showing
5 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.