DEV: Allow us to use Ember CLI assets in production

This adds an optional ENV variable, `EMBER_CLI_PROD_ASSETS`. If truthy,
compiling production assets will be done via Ember CLI and will replace
the assets Rails would otherwise use.
This commit is contained in:
Robin Ward
2021-05-05 09:02:48 -04:00
parent a341dba5d9
commit 18c5e9338f
6 changed files with 110 additions and 7 deletions

View File

@ -18,9 +18,7 @@ task 'assets:precompile:before' do
# is recompiled
Emoji.clear_cache
if !`which terser`.empty? && !ENV['SKIP_NODE_UGLIFY']
$node_uglify = true
end
$node_compress = `which terser`.present? && !ENV['SKIP_NODE_UGLIFY']
unless ENV['USE_SPROCKETS_UGLIFY']
$bypass_sprockets_uglify = true
@ -36,6 +34,15 @@ task 'assets:precompile:before' do
require 'sprockets'
require 'digest/sha1'
if ENV['EMBER_CLI_PROD_ASSETS']
# Remove the assets that Ember CLI will handle for us
Rails.configuration.assets.precompile.reject! do |asset|
asset.is_a?(String) &&
(%w(application.js admin.js ember_jquery.js pretty-text-bundle.js start-discourse.js vendor.js).include?(asset) ||
asset.start_with?("discourse/tests"))
end
end
end
task 'assets:precompile:css' => 'environment' do
@ -165,7 +172,7 @@ def max_compress?(path, locales)
end
def compress(from, to)
if $node_uglify
if $node_compress
compress_node(from, to)
else
compress_ruby(from, to)
@ -216,8 +223,66 @@ def copy_maxmind(from_path, to_path)
end
end
def copy_ember_cli_assets
ember_dir = "app/assets/javascripts/discourse"
ember_cli_assets = "#{ember_dir}/dist/assets/"
assets = {}
files = {}
system("yarn --cwd #{ember_dir} run ember build -prod")
# Copy assets and generate manifest data
Dir["#{ember_cli_assets}**/*"].each do |f|
if f !~ /test/ && File.file?(f)
rel_file = f.sub(ember_cli_assets, "")
digest = f.scan(/\-([a-f0-9]+)\./)[0][0]
dest = "public/assets"
dest_sub = dest
if rel_file =~ /^([a-z\-\_]+)\//
dest_sub = "#{dest}/#{Regexp.last_match[1]}"
end
FileUtils.mkdir_p(dest_sub) unless Dir.exists?(dest_sub)
log_file = File.basename(rel_file).sub("-#{digest}", "")
# It's simpler to serve the file as `application.js`
if log_file == "discourse.js"
log_file = "application.js"
rel_file.sub!(/^discourse/, "application")
end
res = FileUtils.cp(f, "#{dest}/#{rel_file}")
assets[log_file] = rel_file
files[rel_file] = {
"logical_path" => log_file,
"mtime" => File.mtime(f).iso8601(9),
"size" => File.size(f),
"digest" => digest,
"integrity" => "sha384-#{Base64.encode64(Digest::SHA384.digest(File.read(f))).chomp}"
}
end
end
# Update manifest file
manifest_result = Dir["public/assets/.sprockets-manifest-*.json"]
if manifest_result && manifest_result.size == 1
json = JSON.parse(File.read(manifest_result[0]))
json['files'].merge!(files)
json['assets'].merge!(assets)
File.write(manifest_result[0], json.to_json)
end
end
task 'test_ember_cli_copy' do
copy_ember_cli_assets
end
task 'assets:precompile' => 'assets:precompile:before' do
copy_ember_cli_assets if ENV['EMBER_CLI_PROD_ASSETS']
refresh_days = GlobalSetting.refresh_maxmind_db_during_precompile_days
if refresh_days.to_i > 0
@ -261,6 +326,7 @@ task 'assets:precompile' => 'assets:precompile:before' do
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|