Created
April 21, 2010 07:24
-
-
Save cjohansen/373535 to your computer and use it in GitHub Desktop.
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
require "juicer" | |
require "juicer/merger/javascript_merger" | |
require "juicer/minifyer/yui_compressor" | |
require "pathname" | |
class JuicyScript | |
def initialize(script) | |
@script = script | |
end | |
def merged(force = false) | |
merger = Juicer::Merger::JavaScriptMerger.new(path) | |
output = StringIO.new | |
merger.save(output) | |
output(output.rewind && output.read, merger.files) | |
end | |
def minified(force = false) | |
script = path("min.js") | |
File.delete(script) if force && File.exists?(script) | |
`juicer merge -s -o #{script} #{path}` if !File.exists?(script) | |
output(File.read(script)) | |
end | |
def path(suffix = "js") | |
script = @script.sub(/((\.min)?\.js)?$/, ".#{suffix}") | |
script = script !~ /^lib/ ? "src/#{script}" : script | |
File.join(self.class.root, script) | |
end | |
def relative_path(script = path) | |
script = script.sub("#{self.class.root}/", "") | |
end | |
def output(contents, dependencies = nil) | |
dependencies ||= Juicer::JavaScriptDependencyResolver.new.resolve(path) | |
dependencies = dependencies.collect { |f| self.relative_path(f) } | |
<<-JS | |
/** | |
* #{relative_path} as packaged by Juicer | |
* | |
* #{dependencies.join("\n * ")} | |
* | |
* Total: #{contents.length / 1024.0} kbytes | |
*/ | |
#{contents} | |
JS | |
end | |
def self.root | |
@@root ||= File.join(Rails.root, "public/design/js") | |
end | |
def self.compressor | |
return @@compressor if @@compressor | |
juicer_home = File.join(Juicer.home, "lib/yui_compressor/bin") | |
@@compressor = Juicer::Minifyer::YuiCompressor.new({ :bin_path => juicer_home }) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment