Revert "Revert "PERF: Skip compressing locales for faster rebuilds (#7501)" (#7502)"

Revert “Revert “PERF: Skip compressing locales for faster rebuilds (#7501)” (#7502)”

This reverts commit 6ea811c923c2d96e4d71332c300422653909d890.

diff --git a/config/environments/production.rb b/config/environments/production.rb
index c12ed4e..fd344fa 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -12,7 +12,8 @@ Discourse::Application.configure do
   # Disable Rails's static asset server (Apache or nginx will already do this)
   config.public_file_server.enabled = GlobalSetting.serve_static_assets || false
 
-  config.assets.js_compressor = :uglifier
+  require 'uglifier'
+  config.assets.js_compressor = Uglifier.new(harmony: true)
 
   # stuff should be pre-compiled
   config.assets.compile = false
diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake
index fe37448..47ee8f5 100644
--- a/lib/tasks/assets.rake
+++ b/lib/tasks/assets.rake
@@ -16,7 +16,7 @@ task 'assets:precompile:before' do
   # is recompiled
   Emoji.clear_cache
 
-  if Rails.configuration.assets.js_compressor == :uglifier && !`which uglifyjs`.empty? && !ENV['SKIP_NODE_UGLIFY']
+  if !`which uglifyjs`.empty? && !ENV['SKIP_NODE_UGLIFY']
     $node_uglify = true
   end
 
@@ -79,7 +79,7 @@ def compress_node(from, to)
   source_map_root = assets + ((d = File.dirname(from)) == "." ? "" : "/#{d}")
   source_map_url = cdn_path "/assets/#{to}.map"
 
-  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}'"
+  cmd = "uglifyjs '#{assets_path}/#{from}' -p relative -m -c -o '#{to_path}' --source-map-root '#{source_map_root}' --source-map '#{assets_path}/#{to}.map' --source-map-url '#{source_map_url}'"
 
   STDERR.puts cmd
   result = `#{cmd} 2>&1`
@@ -128,6 +128,16 @@ def brotli(path)
   raise "chmod failed: exit code #{$?.exitstatus}" if $?.exitstatus != 0
 end
 
+def should_compress?(path, locales)
+  return false if Rails.configuration.assets.skip_minification.include? path
+  return true unless path.include? "locales/"
+
+  path_locale = path.delete_prefix("locales/").delete_suffix(".js")
+  return true if locales.include? path_locale
+
+  false
+end
+
 def compress(from, to)
   if $node_uglify
     compress_node(from, to)
@@ -163,10 +173,15 @@ task 'assets:precompile' => 'assets:precompile:before' do
 
   if $bypass_sprockets_uglify
     puts "Compressing Javascript and Generating Source Maps"
+    startAll = Process.clock_gettime(Process::CLOCK_MONOTONIC)
     manifest = Sprockets::Manifest.new(assets_path)
+    locales = Set.new(["en"])
+
+    RailsMultisite::ConnectionManagement.each_connection do |db|
+      locales.add(SiteSetting.default_locale)
+    end
 
     concurrent? do |proc|
-      to_skip = Rails.configuration.assets.skip_minification || []
       manifest.files
         .select { |k, v| k =~ /\.js$/ }
         .each do |file, info|
@@ -182,8 +197,7 @@ task 'assets:precompile' => 'assets:precompile:before' do
               start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
               STDERR.puts "#{start} Compressing: #{file}"
 
-              # We can specify some files to never minify
-              unless (ENV["DONT_MINIFY"] == "1") || to_skip.include?(info['logical_path'])
+              if should_compress?(info["logical_path"], locales)
                 FileUtils.mv(path, _path)
                 compress(_file, file)
               end
@@ -191,7 +205,7 @@ task 'assets:precompile' => 'assets:precompile:before' do
               info["size"] = File.size(path)
               info["mtime"] = File.mtime(path).iso8601
               gzip(path)
-              brotli(path)
+              brotli(path) if should_compress?(info["logical_path"], locales)
 
               STDERR.puts "Done compressing #{file} : #{(Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).round(2)} secs"
               STDERR.puts
@@ -200,6 +214,9 @@ task 'assets:precompile' => 'assets:precompile:before' do
       end
     end
 
+    STDERR.puts "Done compressing all JS files : #{(Process.clock_gettime(Process::CLOCK_MONOTONIC) - startAll).round(2)} secs"
+    STDERR.puts
+
     # protected
     manifest.send :save

GitHub sha: d9ab722d