asset_hat 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +38 -19
- data/README.rdoc +99 -41
- data/Rakefile +17 -10
- data/VERSION.yml +3 -3
- data/app/helpers/asset_hat_helper.rb +40 -20
- data/asset_hat.gemspec +105 -101
- data/doc/classes/AssetHat.html +173 -59
- data/doc/classes/AssetHat/CSS.html +35 -26
- data/doc/classes/AssetHat/CSS/Engines.html +13 -12
- data/doc/classes/AssetHat/JS.html +11 -11
- data/doc/classes/AssetHat/JS/Engines.html +10 -10
- data/doc/classes/AssetHat/JS/Vendors.html +9 -5
- data/doc/classes/AssetHatHelper.html +18 -14
- data/doc/created.rid +1 -1
- data/doc/files/HISTORY.html +66 -24
- data/doc/files/LICENSE.html +1 -1
- data/doc/files/README_rdoc.html +112 -37
- data/doc/files/app/helpers/asset_hat_helper_rb.html +1 -1
- data/doc/files/lib/asset_hat/capistrano_rb.html +1 -1
- data/doc/files/lib/asset_hat/css_rb.html +1 -1
- data/doc/files/lib/asset_hat/js/vendors_rb.html +1 -1
- data/doc/files/lib/asset_hat/js_rb.html +1 -1
- data/doc/files/lib/asset_hat/tasks/css_rb.html +1 -1
- data/doc/files/lib/asset_hat/tasks/js_rb.html +1 -1
- data/doc/files/lib/asset_hat/tasks_rb.html +1 -1
- data/doc/files/lib/asset_hat/vcs_rb.html +1 -1
- data/doc/files/lib/asset_hat/version_rb.html +1 -1
- data/doc/files/lib/asset_hat_rb.html +1 -1
- data/doc/fr_method_index.html +54 -46
- data/lib/asset_hat.rb +100 -16
- data/lib/asset_hat/capistrano.rb +4 -3
- data/lib/asset_hat/css.rb +69 -20
- data/lib/asset_hat/js.rb +2 -2
- data/lib/asset_hat/js/vendors.rb +14 -10
- data/lib/asset_hat/tasks.rb +22 -3
- data/lib/asset_hat/tasks/css.rb +118 -74
- data/lib/asset_hat/tasks/js.rb +79 -35
- data/lib/asset_hat/vcs.rb +19 -0
- data/public/stylesheets/bundles/ssl/css-bundle-1.min.css +3 -0
- data/public/stylesheets/bundles/ssl/css-bundle-2.min.css +3 -0
- data/public/stylesheets/bundles/ssl/css-bundle-3.min.css +3 -0
- data/rails/init.rb +1 -16
- data/test/asset_hat_helper_test.rb +172 -28
- data/test/asset_hat_test.rb +167 -25
- metadata +54 -21
- data/.gitignore +0 -2
data/lib/asset_hat/js.rb
CHANGED
@@ -30,13 +30,13 @@ module AssetHat
|
|
30
30
|
|
31
31
|
engine = options[:engine].to_sym
|
32
32
|
unless ENGINES.include?(engine)
|
33
|
-
raise %
|
33
|
+
raise %{
|
34
34
|
Unknown JS minification engine '#{engine}'.
|
35
35
|
Allowed: #{ENGINES.map{ |e| "'#{e}'" }.join(', ')}
|
36
36
|
}.strip.gsub(/\s+/, ' ') and return
|
37
37
|
end
|
38
38
|
|
39
|
-
AssetHat::JS::Engines.send(engine, input_string)
|
39
|
+
AssetHat::JS::Engines.send(engine, input_string).strip
|
40
40
|
end
|
41
41
|
|
42
42
|
# Swappable JavaScript minification engines.
|
data/lib/asset_hat/js/vendors.rb
CHANGED
@@ -33,6 +33,8 @@ module AssetHat
|
|
33
33
|
# points to {Google's CDN}[http://code.google.com/apis/ajaxlibs/].
|
34
34
|
# Options:
|
35
35
|
#
|
36
|
+
# [ssl] Boolean for whether to include vendor JS via HTTPS. Defaults
|
37
|
+
# to false.
|
36
38
|
# [version] The vendor version, e.g., '1.4.0' for jQuery 1.4. By
|
37
39
|
# default, each vendor version is taken from
|
38
40
|
# <code>config/assets.yml</code>; use this option to override
|
@@ -41,10 +43,12 @@ module AssetHat
|
|
41
43
|
vendor_config =
|
42
44
|
AssetHat.config['js']['vendors'][vendor.to_s] rescue nil
|
43
45
|
use_local = ActionController::Base.consider_all_requests_local
|
46
|
+
use_ssl = !!options[:ssl]
|
44
47
|
version = options[:version] || vendor_config['version'] rescue nil
|
45
48
|
|
46
49
|
unless use_local
|
47
50
|
src = vendor_config['remote_url'] rescue nil
|
51
|
+
src = (vendor_config['remote_ssl_url'] rescue nil) if use_ssl
|
48
52
|
end
|
49
53
|
|
50
54
|
if src.blank?
|
@@ -52,45 +56,45 @@ module AssetHat
|
|
52
56
|
when :jquery
|
53
57
|
src = use_local || version.blank? ?
|
54
58
|
"#{['jquery', version].compact.join('-')}.min.js" :
|
55
|
-
"http://ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js"
|
59
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js"
|
56
60
|
when :jquery_ui
|
57
61
|
src = use_local || version.blank? ?
|
58
62
|
"#{['jquery-ui', version].compact.join('-')}.min.js" :
|
59
|
-
"http://ajax.googleapis.com/ajax/libs/jqueryui/#{version}/jquery-ui.min.js"
|
63
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/jqueryui/#{version}/jquery-ui.min.js"
|
60
64
|
when :prototype
|
61
65
|
# Prototype currently doesn't provide an official minified version.
|
62
66
|
src = use_local || version.blank? ?
|
63
67
|
"#{['prototype', version].compact.join('-')}.js" :
|
64
|
-
"http://ajax.googleapis.com/ajax/libs/prototype/#{version}/prototype.js"
|
68
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/prototype/#{version}/prototype.js"
|
65
69
|
when :scriptaculous
|
66
70
|
# script.aculo.us currently doesn't provide an official minified version.
|
67
71
|
src = use_local || version.blank? ?
|
68
72
|
"#{['scriptaculous', version].compact.join('-')}.js" :
|
69
|
-
"http://ajax.googleapis.com/ajax/libs/scriptaculous/#{version}/scriptaculous.js"
|
73
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/scriptaculous/#{version}/scriptaculous.js"
|
70
74
|
when :mootools
|
71
75
|
src = use_local || version.blank? ?
|
72
76
|
"#{['mootools', version].compact.join('-')}.min.js" :
|
73
|
-
"http://ajax.googleapis.com/ajax/libs/mootools/#{version}/mootools-yui-compressed.js"
|
77
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/mootools/#{version}/mootools-yui-compressed.js"
|
74
78
|
when :dojo
|
75
79
|
src = use_local || version.blank? ?
|
76
80
|
"#{['dojo', version].compact.join('-')}.min.js" :
|
77
|
-
"http://ajax.googleapis.com/ajax/libs/dojo/#{version}/dojo/dojo.xd.js"
|
81
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/dojo/#{version}/dojo/dojo.xd.js"
|
78
82
|
when :swfobject
|
79
83
|
src = use_local || version.blank? ?
|
80
84
|
"#{['swfobject', version].compact.join('-')}.min.js" :
|
81
|
-
"http://ajax.googleapis.com/ajax/libs/swfobject/#{version}/swfobject.js"
|
85
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/swfobject/#{version}/swfobject.js"
|
82
86
|
when :yui
|
83
87
|
src = use_local || version.blank? ?
|
84
88
|
"#{['yui', version].compact.join('-')}.min.js" :
|
85
|
-
"http://ajax.googleapis.com/ajax/libs/yui/#{version}/build/yuiloader/yuiloader-min.js"
|
89
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/yui/#{version}/build/yuiloader/yuiloader-min.js"
|
86
90
|
when :ext_core
|
87
91
|
src = use_local || version.blank? ?
|
88
92
|
"#{['ext_core', version].compact.join('-')}.min.js" :
|
89
|
-
"http://ajax.googleapis.com/ajax/libs/ext-core/#{version}/ext-core.js"
|
93
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/ext-core/#{version}/ext-core.js"
|
90
94
|
when :webfont
|
91
95
|
src = use_local || version.blank? ?
|
92
96
|
"#{['webfont', version].compact.join('-')}.min.js" :
|
93
|
-
"http://ajax.googleapis.com/ajax/libs/webfont/#{version}/webfont.js"
|
97
|
+
"http#{'s' if use_ssl}://ajax.googleapis.com/ajax/libs/webfont/#{version}/webfont.js"
|
94
98
|
else nil # TODO: Write to log
|
95
99
|
end
|
96
100
|
end
|
data/lib/asset_hat/tasks.rb
CHANGED
@@ -4,17 +4,36 @@ require 'asset_hat/tasks/js'
|
|
4
4
|
namespace :asset_hat do
|
5
5
|
|
6
6
|
desc 'Minifies all CSS and JS bundles'
|
7
|
-
task :minify =>
|
7
|
+
task :minify, :needs => :environment do
|
8
|
+
format = ENV['FORMAT']
|
9
|
+
print 'Minifying CSS/JS...'
|
10
|
+
puts unless format == 'dot'
|
11
|
+
|
12
|
+
%w[css js].each do |type|
|
13
|
+
task = Rake::Task["asset_hat:#{type}:minify"]
|
14
|
+
task.reenable
|
15
|
+
task.invoke(:show_intro => false, :show_outro => false)
|
16
|
+
end
|
17
|
+
puts unless format == 'short'
|
18
|
+
puts 'Done.'
|
19
|
+
end
|
20
|
+
|
21
|
+
namespace :minify do
|
22
|
+
%w[css js].each do |type|
|
23
|
+
desc "Alias for asset_hat:#{type}:minify"
|
24
|
+
task type.to_sym => "asset_hat:#{type}:minify"
|
25
|
+
end
|
26
|
+
end
|
8
27
|
|
9
28
|
desc 'Prepare configuration file'
|
10
|
-
task :config do
|
29
|
+
task :config, :needs => :environment do
|
11
30
|
template_filepath = File.join(File.dirname(__FILE__), '..', '..',
|
12
31
|
AssetHat::RELATIVE_CONFIG_FILEPATH)
|
13
32
|
target_filepath = AssetHat::CONFIG_FILEPATH
|
14
33
|
|
15
34
|
if File.exists?(target_filepath)
|
16
35
|
print "Replace #{target_filepath}? (y/n) "
|
17
|
-
response =
|
36
|
+
response = $stdin.gets.chomp
|
18
37
|
unless response.downcase == 'y'
|
19
38
|
puts 'Aborted.' ; exit
|
20
39
|
end
|
data/lib/asset_hat/tasks/css.rb
CHANGED
@@ -1,66 +1,56 @@
|
|
1
1
|
namespace :asset_hat do
|
2
2
|
namespace :css do
|
3
3
|
|
4
|
-
# desc 'Adds mtimes to asset URLs in CSS'
|
5
|
-
# task :add_asset_mtimes, :filename, :verbose do |t, args|
|
6
|
-
# if args.filename.blank?
|
7
|
-
# raise 'Usage: rake asset_hat:css:add_asset_mtimes[filename.css]' and return
|
8
|
-
# end
|
9
|
-
#
|
10
|
-
# args.with_defaults :verbose => true
|
11
|
-
#
|
12
|
-
# css = File.open(args.filename, 'r') { |f| f.read }
|
13
|
-
# css = AssetHat::CSS.add_asset_mtimes(css)
|
14
|
-
# File.open(args.filename, 'w') { |f| f.write css }
|
15
|
-
#
|
16
|
-
# puts "- Added asset mtimes to #{args.filename}" if args.verbose
|
17
|
-
# end
|
18
|
-
|
19
4
|
desc 'Adds commit IDs to asset URLs in CSS for cache busting'
|
20
|
-
task :add_asset_commit_ids, :filename, :
|
5
|
+
task :add_asset_commit_ids, :filename, :needs => :environment do |t, args|
|
21
6
|
if args.filename.blank?
|
22
|
-
raise 'Usage: rake asset_hat:css:
|
7
|
+
raise 'Usage: rake asset_hat:css:' +
|
8
|
+
'add_asset_commit_ids[filename.css]' and return
|
23
9
|
end
|
24
10
|
|
25
|
-
|
11
|
+
verbose = (ENV['VERBOSE'] != 'false') # Defaults to `true`
|
26
12
|
|
27
13
|
css = File.open(args.filename, 'r') { |f| f.read }
|
28
14
|
css = AssetHat::CSS.add_asset_commit_ids(css)
|
29
15
|
File.open(args.filename, 'w') { |f| f.write css }
|
30
16
|
|
31
|
-
puts "- Added asset commit IDs to #{args.filename}" if
|
17
|
+
puts "- Added asset commit IDs to #{args.filename}" if verbose
|
32
18
|
end
|
33
19
|
|
34
20
|
desc 'Adds hosts to asset URLs in CSS'
|
35
|
-
task :add_asset_hosts, :filename, :
|
21
|
+
task :add_asset_hosts, :filename, :needs => :environment do |t, args|
|
36
22
|
if args.filename.blank?
|
37
|
-
raise 'Usage: rake asset_hat:css:
|
23
|
+
raise 'Usage: rake asset_hat:css:' +
|
24
|
+
'add_asset_hosts[filename.css]' and return
|
38
25
|
end
|
39
26
|
|
40
|
-
|
27
|
+
verbose = (ENV['VERBOSE'] != 'false') # Defaults to `true`
|
41
28
|
|
42
29
|
asset_host = ActionController::Base.asset_host
|
43
30
|
if asset_host.blank?
|
44
|
-
raise "This environment (#{ENV['RAILS_ENV']})
|
45
|
-
|
31
|
+
raise "This environment (#{ENV['RAILS_ENV']}) " +
|
32
|
+
"doesn't have an `asset_host` configured." and return
|
46
33
|
end
|
47
34
|
|
48
35
|
css = File.open(args.filename, 'r') { |f| f.read }
|
49
36
|
css = AssetHat::CSS.add_asset_hosts(css, asset_host)
|
50
37
|
File.open(args.filename, 'w') { |f| f.write css }
|
51
38
|
|
52
|
-
puts "- Added asset hosts to #{args.filename}" if
|
39
|
+
puts "- Added asset hosts to #{args.filename}" if verbose
|
53
40
|
end
|
54
41
|
|
55
42
|
desc 'Minifies one CSS file'
|
56
|
-
task :minify_file, :filepath, :
|
43
|
+
task :minify_file, :filepath, :needs => :environment do |t, args|
|
44
|
+
type = 'css'
|
45
|
+
|
57
46
|
if args.filepath.blank?
|
58
|
-
raise
|
47
|
+
raise "Usage: rake asset_hat:#{type}:" +
|
48
|
+
"minify_file[path/to/filename.#{type}]" and return
|
59
49
|
end
|
60
50
|
|
61
|
-
|
51
|
+
verbose = (ENV['VERBOSE'] != 'false') # Defaults to `true`
|
62
52
|
min_options = {
|
63
|
-
:engine => AssetHat.config[
|
53
|
+
:engine => AssetHat.config[type]['engine']
|
64
54
|
}.reject { |k,v| v.blank? }
|
65
55
|
|
66
56
|
input = File.open(args.filepath, 'r').read
|
@@ -71,79 +61,133 @@ namespace :asset_hat do
|
|
71
61
|
File.open(target_filepath, 'w') { |f| f.write output }
|
72
62
|
|
73
63
|
# Print results
|
74
|
-
puts "- Minified to #{target_filepath}" if
|
64
|
+
puts "- Minified to #{target_filepath}" if verbose
|
75
65
|
end
|
76
66
|
|
77
67
|
desc 'Minifies one CSS bundle'
|
78
68
|
task :minify_bundle, :bundle, :needs => :environment do |t, args|
|
69
|
+
type = 'css'
|
70
|
+
|
79
71
|
if args.bundle.blank?
|
80
|
-
raise
|
72
|
+
raise "Usage: rake asset_hat:#{type}:" +
|
73
|
+
"minify_bundle[application]" and return
|
81
74
|
end
|
82
75
|
|
83
|
-
config = AssetHat.config
|
84
|
-
|
85
|
-
|
76
|
+
config = AssetHat.config[type]
|
77
|
+
report_format = ([ENV['FORMAT']] & %w[long short dot])[0] || 'long'
|
78
|
+
$stdout.sync = (report_format == 'dot')
|
86
79
|
min_options = {
|
87
|
-
:engine => config['
|
80
|
+
:engine => config['engine']
|
88
81
|
}.reject { |k,v| v.blank? }
|
89
82
|
|
90
83
|
# Get bundle filenames
|
91
|
-
filenames = config['
|
84
|
+
filenames = config['bundles'][args.bundle].select(&:present?)
|
92
85
|
if filenames.empty?
|
93
|
-
raise "No
|
94
|
-
|
86
|
+
raise "No #{type.upcase} files are specified for the " +
|
87
|
+
"#{args.bundle} bundle in #{AssetHat::CONFIG_FILEPATH}." and return
|
95
88
|
end
|
96
89
|
filepaths = filenames.map do |filename|
|
97
|
-
|
90
|
+
parts = filename.split(File::SEPARATOR)
|
91
|
+
parts.last << '.' << type
|
92
|
+
File.join(
|
93
|
+
(parts.first.present? ?
|
94
|
+
AssetHat.assets_dir(type) : # Given path was relative
|
95
|
+
AssetHat::ASSETS_DIR), # Given path was absolute
|
96
|
+
parts
|
97
|
+
)
|
98
98
|
end
|
99
|
-
bundle_filepath = AssetHat::CSS.min_filepath(File.join(
|
100
|
-
'public', 'stylesheets', 'bundles', "#{args.bundle}.css"))
|
101
99
|
|
102
|
-
#
|
103
|
-
output = ''
|
100
|
+
# Check whether app has special SSL asset host
|
104
101
|
asset_host = ActionController::Base.asset_host
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
file_output = AssetHat::CSS.minify(file_output, min_options)
|
110
|
-
file_output = AssetHat::CSS.add_asset_commit_ids(file_output)
|
111
|
-
if asset_host.present?
|
112
|
-
file_output = AssetHat::CSS.add_asset_hosts(file_output, asset_host)
|
113
|
-
end
|
114
|
-
|
115
|
-
new_bundle_size += file_output.size
|
116
|
-
output << file_output + "\n"
|
102
|
+
output_options_array = [{:ssl => false}]
|
103
|
+
if AssetHat.ssl_asset_host_differs?
|
104
|
+
# The bundle needs a second version, which uses the asset host via SSL
|
105
|
+
output_options_array << {:ssl => true}
|
117
106
|
end
|
118
|
-
FileUtils.makedirs(File.dirname(bundle_filepath))
|
119
|
-
File.open(bundle_filepath, 'w') { |f| f.write output }
|
120
107
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
108
|
+
output_options_array.each do |output_options|
|
109
|
+
|
110
|
+
# Concatenate and process output
|
111
|
+
bundle_filepath = AssetHat::CSS.min_filepath(File.join(
|
112
|
+
AssetHat.bundles_dir(type, output_options.slice(:ssl)),
|
113
|
+
"#{args.bundle}.#{type}"))
|
114
|
+
old_bundle_size = 0.0
|
115
|
+
new_bundle_size = 0.0
|
116
|
+
output = ''
|
117
|
+
filepaths.each do |filepath|
|
118
|
+
file_output = File.open(filepath, 'r').read
|
119
|
+
old_bundle_size += file_output.size
|
120
|
+
|
121
|
+
file_output = AssetHat::CSS.minify(file_output, min_options)
|
122
|
+
file_output = AssetHat::CSS.add_asset_commit_ids(file_output)
|
123
|
+
if asset_host.present?
|
124
|
+
file_output = AssetHat::CSS.add_asset_hosts(
|
125
|
+
file_output, asset_host, output_options.slice(:ssl))
|
126
|
+
end
|
127
|
+
|
128
|
+
new_bundle_size += file_output.size
|
129
|
+
output << file_output + "\n"
|
130
|
+
end
|
131
|
+
FileUtils.makedirs(File.dirname(bundle_filepath))
|
132
|
+
File.open(bundle_filepath, 'w') { |f| f.write output }
|
133
|
+
|
134
|
+
# Print results
|
135
|
+
percent_saved =
|
136
|
+
"#{'%.1f' % ((1 - (new_bundle_size / old_bundle_size)) * 100)}%"
|
137
|
+
bundle_filepath.sub!(/^#{Rails.root}\//, '')
|
138
|
+
case report_format
|
139
|
+
when 'dot'
|
140
|
+
print '.'
|
141
|
+
when 'short'
|
142
|
+
puts "Minified #{percent_saved.rjust(6)}: #{bundle_filepath}"
|
143
|
+
else # 'long'
|
144
|
+
puts "\nWrote #{type.upcase} bundle: #{bundle_filepath}"
|
145
|
+
filepaths.each do |filepath|
|
146
|
+
puts " contains: #{filepath.sub(/^#{Rails.root}\//, '')}"
|
147
|
+
end
|
148
|
+
if old_bundle_size > 0
|
149
|
+
puts " MINIFIED: #{percent_saved}" +
|
150
|
+
(" (empty!)" if new_bundle_size == 0).to_s +
|
151
|
+
" (Engine: #{min_options[:engine]})"
|
152
|
+
end
|
153
|
+
end
|
130
154
|
end
|
131
155
|
end
|
132
156
|
|
133
157
|
desc 'Concatenates and minifies all CSS bundles'
|
134
|
-
task :minify, :needs => :environment do
|
158
|
+
task :minify, :opts, :needs => :environment do |t, args|
|
159
|
+
args.with_defaults(:opts => {})
|
160
|
+
opts = args.opts.reverse_merge(:show_intro => true, :show_outro => true)
|
161
|
+
type = 'css'
|
162
|
+
report_format = ENV['FORMAT']
|
163
|
+
|
164
|
+
if opts[:show_intro]
|
165
|
+
print "Minifying #{type.upcase}..."
|
166
|
+
if report_format == 'dot'
|
167
|
+
$stdout.sync = true
|
168
|
+
else
|
169
|
+
puts
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
135
173
|
# Get input bundles
|
136
|
-
config = AssetHat.config
|
137
|
-
if config
|
138
|
-
|
139
|
-
|
174
|
+
config = AssetHat.config[type]
|
175
|
+
if config.blank? || config['bundles'].blank?
|
176
|
+
raise "You need to set up #{type.upcase} bundles " +
|
177
|
+
"in #{AssetHat::CONFIG_FILEPATH}." and return
|
140
178
|
end
|
141
|
-
bundles = config['
|
179
|
+
bundles = config['bundles'].keys
|
142
180
|
|
143
181
|
# Minify bundles
|
144
182
|
bundles.each do |bundle|
|
145
|
-
Rake::Task[
|
146
|
-
|
183
|
+
task = Rake::Task["asset_hat:#{type}:minify_bundle"]
|
184
|
+
task.reenable
|
185
|
+
task.invoke(bundle)
|
186
|
+
end
|
187
|
+
|
188
|
+
if opts[:show_outro]
|
189
|
+
puts unless report_format == 'short'
|
190
|
+
puts 'Done.'
|
147
191
|
end
|
148
192
|
end
|
149
193
|
|
data/lib/asset_hat/tasks/js.rb
CHANGED
@@ -2,19 +2,21 @@ namespace :asset_hat do
|
|
2
2
|
namespace :js do
|
3
3
|
|
4
4
|
desc 'Minifies one JS file'
|
5
|
-
task :minify_file, :filepath, :
|
5
|
+
task :minify_file, :filepath, :needs => :environment do |t, args|
|
6
|
+
type = 'js'
|
7
|
+
|
6
8
|
if args.filepath.blank?
|
7
|
-
raise
|
9
|
+
raise "Usage: rake asset_hat:#{type}:" +
|
10
|
+
"minify_file[filepath.#{type}]" and return
|
8
11
|
end
|
9
12
|
|
10
|
-
|
13
|
+
verbose = (ENV['VERBOSE'] != 'false') # Defaults to `true`
|
11
14
|
min_options = {
|
12
|
-
:engine => AssetHat.config[
|
15
|
+
:engine => AssetHat.config[type]['engine']
|
13
16
|
}.reject { |k,v| v.blank? }
|
14
17
|
|
15
|
-
if
|
16
|
-
|
17
|
-
exit 1
|
18
|
+
if verbose && args.filepath.match(/\.min\.#{type}$/)
|
19
|
+
raise "#{args.filepath} is already minified." and return
|
18
20
|
end
|
19
21
|
|
20
22
|
input = File.open(args.filepath, 'r').read
|
@@ -25,40 +27,52 @@ namespace :asset_hat do
|
|
25
27
|
File.open(target_filepath, 'w') { |f| f.write output }
|
26
28
|
|
27
29
|
# Print results
|
28
|
-
puts "- Minified to #{target_filepath}" if
|
30
|
+
puts "- Minified to #{target_filepath}" if verbose
|
29
31
|
end
|
30
32
|
|
31
33
|
desc 'Minifies one JS bundle'
|
32
34
|
task :minify_bundle, :bundle, :needs => :environment do |t, args|
|
35
|
+
type = 'js'
|
36
|
+
|
33
37
|
if args.bundle.blank?
|
34
|
-
raise
|
38
|
+
raise "Usage: rake asset_hat:#{type}:" +
|
39
|
+
"minify_bundle[application]" and return
|
35
40
|
end
|
36
41
|
|
37
|
-
config = AssetHat.config
|
38
|
-
|
39
|
-
|
42
|
+
config = AssetHat.config[type]
|
43
|
+
report_format = ([ENV['FORMAT']] & %w[long short dot])[0] || 'long'
|
44
|
+
$stdout.sync = true if report_format == 'dot'
|
40
45
|
min_options = {
|
41
|
-
:engine => config['
|
46
|
+
:engine => config['engine']
|
42
47
|
}.reject { |k,v| v.blank? }
|
43
48
|
|
44
49
|
# Get bundle filenames
|
45
|
-
filenames = config['
|
50
|
+
filenames = config['bundles'][args.bundle].select(&:present?)
|
46
51
|
if filenames.empty?
|
47
|
-
raise "No
|
48
|
-
|
52
|
+
raise "No #{type.upcase} files are specified for the " +
|
53
|
+
"#{args.bundle} bundle in #{AssetHat::CONFIG_FILEPATH}." and return
|
49
54
|
end
|
50
55
|
filepaths = filenames.map do |filename|
|
51
|
-
|
56
|
+
parts = filename.split(File::SEPARATOR)
|
57
|
+
parts.last << '.' << type
|
58
|
+
File.join(
|
59
|
+
(parts.first.present? ?
|
60
|
+
AssetHat.assets_dir(type) : # Given path was relative
|
61
|
+
AssetHat::ASSETS_DIR), # Given path was absolute
|
62
|
+
parts
|
63
|
+
)
|
52
64
|
end
|
53
65
|
bundle_filepath = AssetHat::JS.min_filepath(File.join(
|
54
|
-
|
66
|
+
AssetHat.bundles_dir(type), "#{args.bundle}.#{type}"))
|
55
67
|
|
56
68
|
# Concatenate and process output
|
57
69
|
output = ''
|
70
|
+
old_bundle_size = 0.0
|
71
|
+
new_bundle_size = 0.0
|
58
72
|
filepaths.each do |filepath|
|
59
73
|
file_output = File.open(filepath, 'r').read
|
60
74
|
old_bundle_size += file_output.size
|
61
|
-
unless filepath =~ /\.min
|
75
|
+
unless filepath =~ /\.min\.#{type}$/ # Already minified
|
62
76
|
file_output = AssetHat::JS.minify(file_output, min_options)
|
63
77
|
end
|
64
78
|
new_bundle_size += file_output.size
|
@@ -68,31 +82,61 @@ namespace :asset_hat do
|
|
68
82
|
File.open(bundle_filepath, 'w') { |f| f.write output }
|
69
83
|
|
70
84
|
# Print results
|
71
|
-
percent_saved =
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
puts "
|
85
|
+
percent_saved =
|
86
|
+
"#{'%.1f' % ((1 - (new_bundle_size / old_bundle_size)) * 100)}%"
|
87
|
+
bundle_filepath.sub!(/^#{Rails.root}\//, '')
|
88
|
+
case report_format
|
89
|
+
when 'dot'
|
90
|
+
print '.'
|
91
|
+
when 'short'
|
92
|
+
puts "Minified #{percent_saved.rjust(6)}: #{bundle_filepath}"
|
93
|
+
else # 'long'
|
94
|
+
puts "\n Wrote #{type.upcase} bundle: #{bundle_filepath}"
|
95
|
+
filepaths.each do |filepath|
|
96
|
+
puts " contains: #{filepath.sub(/^#{Rails.root}\//, '')}"
|
97
|
+
end
|
98
|
+
if old_bundle_size > 0
|
99
|
+
puts " MINIFIED: #{percent_saved}" +
|
100
|
+
(" (empty!)" if new_bundle_size == 0).to_s +
|
101
|
+
" (Engine: #{min_options[:engine]})"
|
102
|
+
end
|
79
103
|
end
|
80
104
|
end
|
81
105
|
|
82
106
|
desc 'Concatenates and minifies all JS bundles'
|
83
|
-
task :minify, :needs => :environment do
|
107
|
+
task :minify, :opts, :needs => :environment do |t, args|
|
108
|
+
args.with_defaults(:opts => {})
|
109
|
+
opts = args.opts.reverse_merge(:show_intro => true, :show_outro => true)
|
110
|
+
type = 'js'
|
111
|
+
report_format = ENV['FORMAT']
|
112
|
+
|
113
|
+
if opts[:show_intro]
|
114
|
+
print "Minifying #{type.upcase}..."
|
115
|
+
if report_format == 'dot'
|
116
|
+
$stdout.sync = true
|
117
|
+
else
|
118
|
+
puts
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
84
122
|
# Get input bundles
|
85
|
-
config = AssetHat.config
|
86
|
-
if config
|
87
|
-
|
88
|
-
|
123
|
+
config = AssetHat.config[type]
|
124
|
+
if config.blank? || config['bundles'].blank?
|
125
|
+
raise "You need to set up #{type.upcase} bundles " +
|
126
|
+
"in #{AssetHat::CONFIG_FILEPATH}." and return
|
89
127
|
end
|
90
|
-
bundles = config['
|
128
|
+
bundles = config['bundles'].keys
|
91
129
|
|
92
130
|
# Minify bundles
|
93
131
|
bundles.each do |bundle|
|
94
|
-
Rake::Task[
|
95
|
-
|
132
|
+
task = Rake::Task["asset_hat:#{type}:minify_bundle"]
|
133
|
+
task.reenable
|
134
|
+
task.invoke(bundle)
|
135
|
+
end
|
136
|
+
|
137
|
+
if opts[:show_outro]
|
138
|
+
puts unless report_format == 'short'
|
139
|
+
puts 'Done.'
|
96
140
|
end
|
97
141
|
end
|
98
142
|
|