2013-12-19 12:33:17 +11:00
task 'assets:precompile:before' do
2013-11-04 09:58:34 +11:00
2014-05-15 15:52:09 +10:00
require 'uglifier'
2013-12-19 12:33:17 +11:00
unless %w{ profile production } . include? Rails . env
raise " rake assets:precompile should only be run in RAILS_ENV=production, you are risking unminified assets "
2013-11-04 09:58:34 +11:00
end
2013-12-19 12:33:17 +11:00
2014-02-07 11:36:44 +11:00
# Ensure we ALWAYS do a clean build
# We use many .erbs that get out of date quickly, especially with plugins
2014-02-07 21:43:05 +11:00
puts " Purging temp files "
2014-02-07 11:36:44 +11:00
` rm -fr #{ Rails . root } /tmp/cache `
2014-12-18 04:14:12 +11:00
if Rails . configuration . assets . js_compressor == :uglifier && ! ` which uglifyjs ` . empty? && ! ENV [ 'SKIP_NODE_UGLIFY' ]
$node_uglify = true
2014-12-12 18:53:26 +11:00
end
puts " Bundling assets "
2013-12-19 12:33:17 +11:00
# in the past we applied a patch that removed asset postfixes, but it is terrible practice
# leaving very complicated build issues
# https://github.com/rails/sprockets-rails/issues/49
2014-02-06 16:55:53 +11:00
require 'sprockets'
require 'digest/sha1'
2015-07-21 11:52:54 -07:00
# Needed for proper source maps with a CDN
load " #{ Rails . root } /lib/global_path.rb "
include GlobalPath
2014-03-04 16:54:58 +11:00
2014-12-18 04:14:12 +11:00
if $node_uglify
2016-04-19 14:28:10 +08:00
Rails . configuration . assets . js_compressor = nil
2016-04-19 12:19:07 +08:00
module :: Sprockets
# TODO: https://github.com/rails/sprockets-rails/pull/342
# Rails.configuration.assets.gzip = false
class Base
def skip_gzip?
true
2014-12-18 04:14:12 +11:00
end
2014-03-04 16:54:58 +11:00
end
2014-02-06 16:55:53 +11:00
end
end
2013-11-04 09:58:34 +11:00
end
2013-12-19 12:33:17 +11:00
2014-05-02 17:46:03 -04:00
task 'assets:precompile:css' = > 'environment' do
2015-10-13 10:48:21 +11:00
if ENV [ " DONT_PRECOMPILE_CSS " ] == " 1 "
STDERR . puts " Skipping CSS precompilation, ensure CSS lives in a shared directory across hosts "
else
STDERR . puts " Start compiling CSS: #{ Time . zone . now } "
RailsMultisite :: ConnectionManagement . each_connection do | db |
# Heroku precompiles assets before db migration, so tables may not exist.
# css will get precompiled during first request instead in that case.
if ActiveRecord :: Base . connection . table_exists? ( ColorScheme . table_name )
STDERR . puts " Compiling css for #{ db } "
[ :desktop , :mobile , :desktop_rtl , :mobile_rtl ] . each do | target |
STDERR . puts " target: #{ target } #{ DiscourseStylesheets . compile ( target ) } "
end
2014-06-12 14:41:37 -04:00
end
2014-05-02 17:46:03 -04:00
end
2015-10-12 17:31:37 +11:00
2015-10-13 10:48:21 +11:00
STDERR . puts " Done compiling CSS: #{ Time . zone . now } "
end
2014-05-02 17:46:03 -04:00
end
2014-12-12 18:53:26 +11:00
def assets_path
" #{ Rails . root } /public/assets "
end
def compress_node ( from , to )
to_path = " #{ assets_path } / #{ to } "
2016-02-05 13:05:47 +11:00
assets = cdn_relative_path ( " /assets " )
2016-02-05 14:59:33 +11:00
source_map_root = assets + ( ( d = File . dirname ( from ) ) == " . " ? " " : " / #{ d } " )
2015-07-21 11:52:54 -07:00
source_map_url = cdn_path " /assets/ #{ to } .map "
2014-12-12 18:53:26 +11:00
2015-07-21 11:52:54 -07:00
cmd = " uglifyjs ' #{ assets_path } / #{ from } ' -p relative -c -m -o ' #{ to_path } ' --source-map-root ' #{ source_map_root } ' --source-map ' #{ assets_path } / #{ to } .map' --source-map-url ' #{ source_map_url } ' "
2014-12-18 04:14:12 +11:00
STDERR . puts cmd
2015-09-22 11:28:15 +10:00
result = ` #{ cmd } 2>&1 `
unless $? . success?
STDERR . puts result
exit 1
end
2014-12-18 04:14:12 +11:00
2015-09-22 11:28:15 +10:00
result
2014-12-12 18:53:26 +11:00
end
def compress_ruby ( from , to )
data = File . read ( " #{ assets_path } / #{ from } " )
uglified , map = Uglifier . new ( comments : :none ,
2016-01-11 03:13:37 -08:00
screw_ie8 : true ,
2014-12-12 18:53:26 +11:00
source_filename : File . basename ( from ) ,
output_filename : File . basename ( to )
)
. compile_with_map ( data )
dest = " #{ assets_path } / #{ to } "
2015-07-21 11:52:54 -07:00
File . write ( dest , uglified << " \n // # sourceMappingURL= #{ cdn_path " /assets/ #{ to } .map " } " )
2014-12-12 18:53:26 +11:00
File . write ( dest + " .map " , map )
end
def gzip ( path )
STDERR . puts " gzip #{ path } "
2016-06-07 16:55:57 +10:00
STDERR . puts ` gzip -f -c -9 #{ path } > #{ path } .gz `
end
def brotli ( path )
if ENV [ 'COMPRESS_BROTLI' ]
STDERR . puts " brotli #{ path } "
2016-06-07 17:42:12 +10:00
STDERR . puts ` brotli --quality 11 --input #{ path } --output #{ path } .br `
STDERR . puts ` chmod +r #{ path } .br `
2016-06-07 16:55:57 +10:00
end
2014-12-12 18:53:26 +11:00
end
def compress ( from , to )
if @has_uglifyjs || = ! ` which uglifyjs ` . empty?
compress_node ( from , to )
else
compress_ruby ( from , to )
end
end
2016-04-20 09:34:30 +08:00
def concurrent?
2016-06-07 17:03:05 +10:00
if ENV [ " SPROCKETS_CONCURRENT " ] == " 1 "
2016-04-20 09:34:30 +08:00
concurrent_compressors = [ ]
yield ( Proc . new { | & block | concurrent_compressors << Concurrent :: Future . execute { block . call } } )
concurrent_compressors . each ( & :wait! )
else
yield ( Proc . new { | & block | block . call } )
end
end
2014-05-02 17:46:03 -04:00
task 'assets:precompile' = > 'assets:precompile:before' do
# Run after assets:precompile
Rake :: Task [ " assets:precompile:css " ] . invoke
2014-12-12 18:53:26 +11:00
2014-12-18 04:14:12 +11:00
if $node_uglify
2014-12-12 18:53:26 +11:00
puts " Compressing Javascript and Generating Source Maps "
manifest = Sprockets :: Manifest . new ( assets_path )
2015-02-20 15:48:45 -05:00
2016-04-20 09:34:30 +08:00
concurrent? do | proc |
to_skip = Rails . configuration . assets . skip_minification || [ ]
manifest . files
. select { | k , v | k =~ / \ .js$ / }
. each do | file , info |
path = " #{ assets_path } / #{ file } "
_file = ( d = File . dirname ( file ) ) == " . " ? " _ #{ file } " : " #{ d } /_ #{ File . basename ( file ) } "
_path = " #{ assets_path } / #{ _file } "
if File . exists? ( _path )
STDERR . puts " Skipping: #{ file } already compressed "
else
STDERR . puts " Compressing: #{ file } "
proc . call do
# We can specify some files to never minify
unless ( ENV [ " DONT_MINIFY " ] == " 1 " ) || to_skip . include? ( info [ 'logical_path' ] )
FileUtils . mv ( path , _path )
compress ( _file , file )
end
info [ " size " ] = File . size ( path )
info [ " mtime " ] = File . mtime ( path ) . iso8601
gzip ( path )
2016-06-07 16:55:57 +10:00
brotli ( path )
2016-04-20 09:34:30 +08:00
end
2015-02-20 15:48:45 -05:00
end
2016-04-20 09:34:30 +08:00
end
2014-12-12 18:53:26 +11:00
end
# protected
manifest . send :save
end
2014-12-18 04:14:12 +11:00
2014-05-02 17:46:03 -04:00
end