DEV: refactor rake asset precompile tasks (#24045)

* DEV: refactor rake asset precompile tasks

add a separate ember build task that does not depend on rails env
allowing us to compile assets without db+redis connections

rename EMBER_CLI_COMPILE_DONE to SKIP_EMBER_CLI_COMPILE
better semantics in build steps
This commit is contained in:
Jeff Wong 2023-10-22 18:55:09 -07:00 committed by GitHub
parent 8c07bbe084
commit 441a330f1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,13 @@
# frozen_string_literal: true
task "assets:precompile:before": "environment" do
require "uglifier"
require "open3"
task "assets:precompile:prereqs" do
unless %w[profile production].include? Rails.env
raise "rake assets:precompile should only be run in RAILS_ENV=production, you are risking unminified assets"
end
end
if ENV["EMBER_CLI_COMPILE_DONE"] != "1"
task "assets:precompile:build" do
if ENV["SKIP_EMBER_CLI_COMPILE"] != "1"
compile_command = "yarn --cwd app/assets/javascripts/discourse run ember build"
heap_size_limit = check_node_heap_size_limit
@ -25,15 +24,27 @@ task "assets:precompile:before": "environment" do
compile_command = "EMBER_ENV=production #{compile_command}" if ENV["EMBER_ENV"].nil?
only_ember_precompile_build_remaining = (ARGV.last == "assets:precompile:build")
only_assets_precompile_remaining = (ARGV.last == "assets:precompile")
if only_assets_precompile_remaining
# Using exec to free up Rails app memory during ember build
exec "#{compile_command} && EMBER_CLI_COMPILE_DONE=1 bin/rake assets:precompile"
# Using exec to free up Rails app memory during ember build
if only_ember_precompile_build_remaining
exec "#{compile_command}
elsif only_assets_precompile_remaining
exec " #{compile_command} && SKIP_EMBER_CLI_COMPILE=1 bin/rake assets:precompile"
else
system compile_command, exception: true
end
end
end
task "assets:precompile:before": %w[
environment
assets:precompile:prereqs
assets:precompile:build
] do
require "uglifier"
require "open3"
# Ensure we ALWAYS do a clean build
# We use many .erbs that get out of date quickly, especially with plugins
@ -214,11 +225,7 @@ def max_compress?(path, locales)
end
def compress(from, to)
if $node_compress
compress_node(from, to)
else
compress_ruby(from, to)
end
$node_compress ? compress_node(from, to) : compress_ruby(from, to)
end
def concurrent?
@ -266,7 +273,14 @@ task "assets:precompile:compress_js": "environment" do
.reject { |k, v| k =~ %r{/workbox-.*'/} }
.each do |file, info|
path = "#{assets_path}/#{file}"
_file = (d = File.dirname(file)) == "." ? "_#{file}" : "#{d}/_#{File.basename(file)}"
_file =
(
if (d = File.dirname(file)) == "."
"_#{file}"
else
"#{d}/_#{File.basename(file)}"
end
)
_path = "#{assets_path}/#{_file}"
max_compress = max_compress?(info["logical_path"], locales)
if File.exist?(_path)