lib/asset_hat/tasks/js.rb in asset_hat-0.2.1 vs lib/asset_hat/tasks/js.rb in asset_hat-0.3.0

- old
+ new

@@ -1,99 +1,143 @@ namespace :asset_hat do namespace :js do desc 'Minifies one JS file' - task :minify_file, :filepath, :verbose, :needs => :environment do |t, args| + task :minify_file, :filepath, :needs => :environment do |t, args| + type = 'js' + if args.filepath.blank? - raise 'Usage: rake asset_hat:js:minify_file[filepath.js]' and return + raise "Usage: rake asset_hat:#{type}:" + + "minify_file[filepath.#{type}]" and return end - args.with_defaults :verbose => false + verbose = (ENV['VERBOSE'] != 'false') # Defaults to `true` min_options = { - :engine => AssetHat.config['js']['engine'] + :engine => AssetHat.config[type]['engine'] }.reject { |k,v| v.blank? } - if args.verbose && args.filepath.match(/\.min\.js$/) - puts "#{args.filepath} is already minified." - exit 1 + if verbose && args.filepath.match(/\.min\.#{type}$/) + raise "#{args.filepath} is already minified." and return end input = File.open(args.filepath, 'r').read output = AssetHat::JS.minify(input, min_options) # Write minified content to file target_filepath = AssetHat::JS.min_filepath(args.filepath) File.open(target_filepath, 'w') { |f| f.write output } # Print results - puts "- Minified to #{target_filepath}" if args.verbose + puts "- Minified to #{target_filepath}" if verbose end desc 'Minifies one JS bundle' task :minify_bundle, :bundle, :needs => :environment do |t, args| + type = 'js' + if args.bundle.blank? - raise 'Usage: rake asset_hat:js:minify_bundle[application]' and return + raise "Usage: rake asset_hat:#{type}:" + + "minify_bundle[application]" and return end - config = AssetHat.config - old_bundle_size = 0.0 - new_bundle_size = 0.0 + config = AssetHat.config[type] + report_format = ([ENV['FORMAT']] & %w[long short dot])[0] || 'long' + $stdout.sync = true if report_format == 'dot' min_options = { - :engine => config['js']['engine'] + :engine => config['engine'] }.reject { |k,v| v.blank? } # Get bundle filenames - filenames = config['js']['bundles'][args.bundle] + filenames = config['bundles'][args.bundle].select(&:present?) if filenames.empty? - raise "No JS files are specified for the #{args.bundle} bundle in #{AssetHat::CONFIG_FILEPATH}." - return + raise "No #{type.upcase} files are specified for the " + + "#{args.bundle} bundle in #{AssetHat::CONFIG_FILEPATH}." and return end filepaths = filenames.map do |filename| - File.join('public', 'javascripts', "#{filename}.js") + parts = filename.split(File::SEPARATOR) + parts.last << '.' << type + File.join( + (parts.first.present? ? + AssetHat.assets_dir(type) : # Given path was relative + AssetHat::ASSETS_DIR), # Given path was absolute + parts + ) end bundle_filepath = AssetHat::JS.min_filepath(File.join( - 'public', 'javascripts', 'bundles', "#{args.bundle}.js")) + AssetHat.bundles_dir(type), "#{args.bundle}.#{type}")) # Concatenate and process output output = '' + old_bundle_size = 0.0 + new_bundle_size = 0.0 filepaths.each do |filepath| file_output = File.open(filepath, 'r').read old_bundle_size += file_output.size - unless filepath =~ /\.min\.js$/ # Already minified + unless filepath =~ /\.min\.#{type}$/ # Already minified file_output = AssetHat::JS.minify(file_output, min_options) end new_bundle_size += file_output.size output << file_output + "\n" end FileUtils.makedirs(File.dirname(bundle_filepath)) File.open(bundle_filepath, 'w') { |f| f.write output } # Print results - percent_saved = 1 - (new_bundle_size / old_bundle_size) - puts "\n Wrote JS bundle: #{bundle_filepath}" - filepaths.each do |filepath| - puts " contains: #{filepath}" + percent_saved = + "#{'%.1f' % ((1 - (new_bundle_size / old_bundle_size)) * 100)}%" + bundle_filepath.sub!(/^#{Rails.root}\//, '') + case report_format + when 'dot' + print '.' + when 'short' + puts "Minified #{percent_saved.rjust(6)}: #{bundle_filepath}" + else # 'long' + puts "\n Wrote #{type.upcase} bundle: #{bundle_filepath}" + filepaths.each do |filepath| + puts " contains: #{filepath.sub(/^#{Rails.root}\//, '')}" + end + if old_bundle_size > 0 + puts " MINIFIED: #{percent_saved}" + + (" (empty!)" if new_bundle_size == 0).to_s + + " (Engine: #{min_options[:engine]})" + end end - if old_bundle_size > 0 - engine = "(Engine: #{min_options[:engine]})" - puts " MINIFIED: #{'%.1f' % (percent_saved * 100)}% #{engine}" - end end desc 'Concatenates and minifies all JS bundles' - task :minify, :needs => :environment do + task :minify, :opts, :needs => :environment do |t, args| + args.with_defaults(:opts => {}) + opts = args.opts.reverse_merge(:show_intro => true, :show_outro => true) + type = 'js' + report_format = ENV['FORMAT'] + + if opts[:show_intro] + print "Minifying #{type.upcase}..." + if report_format == 'dot' + $stdout.sync = true + else + puts + end + end + # Get input bundles - config = AssetHat.config - if config['js'].blank? || config['js']['bundles'].blank? - puts "You need to set up JS bundles in #{AssetHat::CONFIG_FILEPATH}." - exit + config = AssetHat.config[type] + if config.blank? || config['bundles'].blank? + raise "You need to set up #{type.upcase} bundles " + + "in #{AssetHat::CONFIG_FILEPATH}." and return end - bundles = config['js']['bundles'].keys + bundles = config['bundles'].keys # Minify bundles bundles.each do |bundle| - Rake::Task['asset_hat:js:minify_bundle'].reenable - Rake::Task['asset_hat:js:minify_bundle'].invoke(bundle) + task = Rake::Task["asset_hat:#{type}:minify_bundle"] + task.reenable + task.invoke(bundle) + end + + if opts[:show_outro] + puts unless report_format == 'short' + puts 'Done.' end end end # namespace :js end # namespace :asset_hat