Permalink
Browse files

Added support for minifying CSS and JavaScript files. These functions…

… do not have tests written yet. Added initial files for HTML rendering.
  • Loading branch information...
Charles Jolley
Charles Jolley committed Jan 19, 2009
1 parent 8580f36 commit 24ee81669ee9f813e376ea3bfbd20dd3c3982290
View
@@ -42,7 +42,10 @@ mode :all do
:combine_stylesheet => true,
# by default all targets autobuild
- :autobuild => true
+ :autobuild => true,
+
+ # by default do not minify javacript or stylesheets
+ :minify => false
end
@@ -60,3 +63,9 @@ mode :debug do
end
+mode :production do
+ # minify JS && CSS in production mode
+ config :all, :minify => true
+end
+
+
View
@@ -32,18 +32,27 @@ namespace :build do
desc "builds an html file, possibly executing render tasks"
build_task :html do
+ SC::Builder::Html.build ENTRY, DST_PATH
end
- namespace :combine do
+ desc "combines several source files into a single target, using the ordered_entries property if it exists"
+ build_task :combine do
+ SC::Builder::Combine.build ENTRY, DST_PATH
+ end
+
+ namespace :minify do
- desc "combines several css files into a single css file"
+ desc "Minifies a CSS file by invoking CSSmin"
build_task :css do
+ SC::Builder::Minify.build ENTRY, DST_PATH, :css
end
-
- desc "combines several javascript files into a single javascript file"
+
+ desc "minifies a JavaScript file by invoking the YUI compressor"
build_task :javascript do
+ SC::Builder::Minify.build ENTRY, DST_PATH, :javascript
end
end
+
end
View
@@ -123,7 +123,7 @@ namespace :manifest do
namespace :prepare_build_tasks do
desc "main entrypoint for preparing all build tasks. This should invoke all needed tasks"
- task :all => %w(css javascript sass) #%w(tests html image combine)
+ task :all => %w(css javascript sass combine minify) #%w(tests html image)
desc "executes prerequisites needed before one of the subtasks can be invoked. All subtasks that have this as a prereq"
task :setup => %w(manifest:catalog manifest:hide_buildfiles manifest:localize)
@@ -196,6 +196,10 @@ namespace :manifest do
css_entries = {}
javascript_entries = {}
MANIFEST.entries.each do |entry|
+ # we can only combine entries with a resource property.
+ next if entry.resource.nil?
+
+ # look for CSS or JS type entries
case entry.entry_type
when :css
(css_entries[entry.resource] ||= []) << entry
@@ -207,21 +211,23 @@ namespace :manifest do
# build combined CSS entry
css_entries.each do |resource_name, entries|
MANIFEST.add_composite resource_name.ext('css'),
- :build_task => 'build:combine:css',
- :source_entries => entries,
- :hide_entries => CONFIG.combine_stylesheet,
- :ordered_entries => SC::Helper::EntrySorter.sort(entries)
+ :build_task => 'build:combine',
+ :source_entries => entries,
+ :hide_entries => CONFIG.combine_stylesheet,
+ :ordered_entries => SC::Helper::EntrySorter.sort(entries),
+ :entry_type => :css
end
# build combined JS entry
javascript_entries.each do |resource_name, entries|
resource_name = resource_name.ext('js')
pf = (resource_name == 'javascript.js') ? %w(lproj/strings.js core.js utils.js) : []
MANIFEST.add_composite resource_name,
- :build_task => 'build:combine:javascript',
- :source_entries => entries,
- :hide_entries => CONFIG.combine_javascript,
- :ordered_entries => SC::Helper::EntrySorter.sort(entries, pf)
+ :build_task => 'build:combine',
+ :source_entries => entries,
+ :hide_entries => CONFIG.combine_javascript,
+ :ordered_entries => SC::Helper::EntrySorter.sort(entries, pf),
+ :entry_type => :javascript
end
end
@@ -279,6 +285,26 @@ namespace :manifest do
end
end
+ desc "creates transform entries for all css and Js entries to minify them if needed"
+ task :minify => %w(setup javascript css combine sass) do
+
+ minify_css = CONFIG.minify_css
+ minify_css = CONFIG.minify if minify_css.nil?
+
+ minify_javascript = CONFIG.minify_javascript
+ minify_javascript = CONFIG.minify if minify_javascript.nil?
+
+ MANIFEST.entries.dup.each do |entry|
+ case entry.entry_type
+ when :css
+ MANIFEST.add_transform(entry, :build_task => 'build:minify:css') if minify_css
+ when :javascript
+ MANIFEST.add_transform(entry, :build_task => 'build:minify:javascript') if minify_javascript
+ end
+ end
+
+ end
+
desc "..."
task :image => :setup do
end
View
@@ -10,10 +10,12 @@ namespace :render do
desc "renders erubis including .rhtml and .html.erb files"
build_task :erubis do
+ SC::RenderEngine::Erubis.new(CONTEXT).compile(SRC_PATH)
end
desc "renders haml files"
build_task :haml do
+ SC::RenderEngine::Haml.new(CONTEXT).compile(SRC_PATH)
end
end
View
@@ -34,7 +34,6 @@ namespace :target do
TARGET.target_name.to_s))
TARGET.build_number = TARGET.compute_build_number
-
end
end
@@ -36,7 +36,7 @@ def readlines(src_path)
# joins the array of lines. this is where you can also do any final
# post-processing on the build
def joinlines(lines)
- lines * "\n"
+ lines * ""
end
# writes the passed lines to the named file
@@ -0,0 +1,28 @@
+require 'fileutils'
+
+module SC
+
+ # This builder combines several javascript files into a single file. It is
+ # used to prepare a single javascript file for production use. This build
+ # tool expects the javascript files to have already been processed for any
+ # build directives such sc_static().
+ #
+ class Builder::Combine < Builder
+
+ def build(dst_path)
+ lines = []
+ entries = entry.ordered_entries || entry.source_entries
+ entries.each do |entry|
+ src_path = entry.stage!.staging_path
+ next unless File.exist?(src_path)
+
+ lines << "/* >>>>>>>>>> BEGIN #{entry.filename} */\n"
+ lines += readlines(src_path)
+ lines << "\n"
+ end
+ writelines dst_path, lines
+ end
+
+ end
+
+end
@@ -0,0 +1,15 @@
+module SC
+
+ # Builds an HTML files. This will setup an HtmlContext and then invokes
+ # the render engines for each source before finally rendering the layout.
+ class Builder::Html < Builder
+
+ def build(dst_path)
+ ### TODO
+ lines = readlines(entry.source_path).map { |l| rewrite_inline_code(l) }
+ writelines dst_path, lines
+ end
+
+ end
+
+end
@@ -0,0 +1,57 @@
+require 'fileutils'
+
+module SC
+
+ # This builder combines several javascript files into a single file. It is
+ # used to prepare a single javascript file for production use. This build
+ # tool expects the javascript files to have already been processed for any
+ # build directives such sc_static().
+ #
+ class Builder::Minify < Builder
+
+ # main entry called by build tasls
+ def self.build(entry, dst_path, kind)
+ new(entry, kind).build(dst_path)
+ end
+
+ def initialize(entry=nil, kind=nil)
+ super(entry)
+ @kind = kind
+ end
+
+ # override this method in subclasses to actually do build
+ def build(dst_path)
+ send("build_#{@kind}".to_sym, dst_path)
+ end
+
+ def build_css(dst_path)
+ lines = readlines(entry.source_path)
+ options = {
+ :preserveComments => false,
+ :preserveNewlines => false,
+ :preserveSpaces => true,
+ :preserveColors => false,
+ :skipMisc => false
+ }
+ output = SC::Helper::CSSPacker.new.compress(lines.join, options)
+ writelines dst_path, [output]
+ end
+
+ # Minify some javascript by invoking the YUI compressor.
+ def build_javascript(dst_path)
+ yui_root = File.expand_path(File.join(LIBPATH, '..', 'vendor', 'yui-compressor'))
+ jar_path = File.join(yui_root, 'yuicompressor-2.4.2.jar')
+ filecompress = "java -jar " + jar_path + " --charset utf-8 " + entry.source_path + " -o " + dst_path
+ SC.logger.info 'Compressing with YUI .... '+ dst_path
+ SC.logger.debug `#{filecompress}`
+
+ if $?.exitstatus != 0
+ SC.logger.fatal("!!!!YUI compressor failed, please check that your js code is valid and doesn't contain reserved statements like debugger;")
+ SC.logger.fatal("!!!!Failed compressing ... "+ dst_path)
+ end
+
+ end
+
+ end
+
+end
Oops, something went wrong.

0 comments on commit 24ee816

Please sign in to comment.