Class: ReactOnRailsPro::AssetsPrecompile
- Inherits:
-
Object
- Object
- ReactOnRailsPro::AssetsPrecompile
- Includes:
- Singleton
- Defined in:
- lib/react_on_rails_pro/assets_precompile.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- UPLOAD_TIMEOUT_SECONDS =
Per-hash upload budget during assets:precompile. With RSC enabled this can be spent twice per deploy (server bundle + RSC bundle).
120
Class Method Summary collapse
Instance Method Summary collapse
- #build_bundles ⇒ Object
- #build_or_fetch_bundles ⇒ Object
- #bundles_cache_key ⇒ Object
- #cache_bundles ⇒ Object
- #convert_to_destination(source) ⇒ Object
- #copy_extra_files_to_cache_dir ⇒ Object
- #copy_file_to_extra_files_cache_dir(source_path) ⇒ Object
- #disable_precompile_cache? ⇒ Boolean
- #extra_files_path ⇒ Object
- #extract_extra_files_from_cache_dir ⇒ Object
- #fetch_and_unzip_cached_bundles ⇒ Object
- #fetch_bundles ⇒ Object
- #remote_bundle_cache_adapter ⇒ Object
- #remove_extra_files_cache_dir ⇒ Object
- #zipped_bundles_filename ⇒ Object
- #zipped_bundles_filepath ⇒ Object
Class Method Details
.call ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 69 def self.call instance.build_or_fetch_bundles return unless ReactOnRailsPro.configuration.node_renderer? # Symlink is the same-filesystem default (local dev, CI, Heroku-style same-dyno # deploys, bundle-caching restores). Docker image builds that run assets:precompile # should set ASSETS_PRECOMPILE_RENDERER_CACHE_MODE=copy to bake the cache into the # immutable artifact, or invoke `rake react_on_rails_pro:pre_seed_renderer_cache` # directly (which defaults to copy mode). ReactOnRailsPro::PreSeedRendererCache.call(mode: pre_seed_renderer_cache_mode) publish_current_bundle_if_configured end |
Instance Method Details
#build_bundles ⇒ Object
62 63 64 65 66 67 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 62 def build_bundles remote_bundle_cache_adapter.build rescue RuntimeError ReactOnRailsPro::Utils.rorp_puts "The custom config.remote_bundle_cache_adapter 'build' method raised an error:" raise end |
#build_or_fetch_bundles ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 238 def build_or_fetch_bundles if disable_precompile_cache? build_bundles return end begin bundles_fetched = fetch_and_unzip_cached_bundles rescue RuntimeError => e ReactOnRailsPro::Utils.rorp_puts "An error occurred while attempting to fetch cached bundles." ReactOnRailsPro::Utils.rorp_puts "This will be evaluated as a bundle cache miss." ReactOnRailsPro::Utils.rorp_puts e. puts e.backtrace.join('\n') bundles_fetched = false end return if bundles_fetched build_bundles begin cache_bundles rescue RuntimeError => e ReactOnRailsPro::Utils.rorp_puts "An error occurred while attempting to cache the built bundles." ReactOnRailsPro::Utils.rorp_puts e. puts e.backtrace.join('\n') end end |
#bundles_cache_key ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 33 def bundles_cache_key @bundles_cache_key ||= begin ReactOnRailsPro::Utils.rorp_puts "Calculating digest of bundle dependencies." starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) cache_dependencies = [Shakapacker.config.source_path.join("**", "*")] .union(ReactOnRailsPro.configuration.dependency_globs) # Note, digest_of_globs removes excluded globs digest = ReactOnRailsPro::Utils.digest_of_globs(cache_dependencies) # Include the NODE_ENV in the digest env_cache_keys = [ ReactOnRailsPro::VERSION, ENV.fetch("RAILS_ENV", nil), ENV.fetch("NODE_ENV", nil) ] if remote_bundle_cache_adapter.respond_to?(:cache_keys) env_cache_keys += remote_bundle_cache_adapter.cache_keys end env_cache_keys.compact.each { |value| digest.update(value) } result = digest.hexdigest ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) elapsed = (ending - starting).round(2) ReactOnRailsPro::Utils.rorp_puts "Completed calculating digest of bundle dependencies in #{elapsed} seconds." result end end |
#cache_bundles ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 365 def cache_bundles begin copy_extra_files_to_cache_dir public_output_path = Shakapacker.config.public_output_path ReactOnRailsPro::Utils.rorp_puts "Gzipping built bundles to #{zipped_bundles_filepath} with " \ "files in #{public_output_path}" Dir.chdir(public_output_path) do Rake.sh "tar -czf #{zipped_bundles_filepath} --auto-compress -C " \ "#{Shakapacker.config.public_output_path} ." end rescue StandardError => e ReactOnRailsPro::Utils.rorp_puts "An error occurred while attempting to zip the built bundles." ReactOnRailsPro::Utils.rorp_puts e. puts e.backtrace.join('\n') ensure remove_extra_files_cache_dir end ReactOnRailsPro::Utils.rorp_puts "Bundles will be uploaded to remote bundle cache as #{zipped_bundles_filename}" begin remote_bundle_cache_adapter.upload(zipped_bundles_filepath) rescue RuntimeError = "An error was raised by the custom config.remote_bundle_cache_adapter 'upload' " \ "method when called with zipped_bundles_filepath: #{zipped_bundles_filepath}" ReactOnRailsPro::Utils.rorp_puts raise end end |
#convert_to_destination(source) ⇒ Object
343 344 345 346 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 343 def convert_to_destination(source) new_file_name = source.relative_path_from(Rails.root).each_filename.to_a.join("---") extra_files_path.join(new_file_name) end |
#copy_extra_files_to_cache_dir ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 319 def copy_extra_files_to_cache_dir return unless remote_bundle_cache_adapter.respond_to?(:extra_files_to_cache) FileUtils.mkdir_p(extra_files_path) copied_extra_files_paths = [] remote_bundle_cache_adapter.extra_files_to_cache.each do |file_path| if file_path.file? copy_file_to_extra_files_cache_dir(file_path) copied_extra_files_paths.push(file_path.relative_path_from(Rails.root).to_s) else ReactOnRailsPro::Utils.rorp_puts "Extra file: #{file_path}, doesn't exist. Skipping" end end ReactOnRailsPro::Utils.rorp_puts "Copied extra files: #{copied_extra_files_paths.join(', ')} " \ "to extra_files cache dir" end |
#copy_file_to_extra_files_cache_dir(source_path) ⇒ Object
338 339 340 341 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 338 def copy_file_to_extra_files_cache_dir(source_path) destination_file_path = convert_to_destination(source_path) FileUtils.cp(source_path, destination_file_path) end |
#disable_precompile_cache? ⇒ Boolean
267 268 269 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 267 def disable_precompile_cache? ENV["DISABLE_PRECOMPILE_CACHE"] == "true" end |
#extra_files_path ⇒ Object
315 316 317 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 315 def extra_files_path Rails.root.join(Shakapacker.config.public_output_path, "extra_files") end |
#extract_extra_files_from_cache_dir ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 348 def extract_extra_files_from_cache_dir return unless File.exist?(extra_files_path) extracted_extra_files_paths = [] Dir.each_child(extra_files_path) do |file_name| file_path_parts = file_name.split("---") source_file_path = extra_files_path.join(file_name) destination_file_path = Rails.root.join(*file_path_parts) FileUtils.mv(source_file_path, destination_file_path) extracted_extra_files_paths.push(destination_file_path.relative_path_from(Rails.root).to_s) end ReactOnRailsPro::Utils.rorp_puts "Extracted extra files: #{extracted_extra_files_paths.join(', ')} " \ "from extra_files cache dir" remove_extra_files_cache_dir end |
#fetch_and_unzip_cached_bundles ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 292 def fetch_and_unzip_cached_bundles if File.exist?(zipped_bundles_filepath) ReactOnRailsPro::Utils.rorp_puts "Found a local cache of bundles: #{zipped_bundles_filepath}" result = true else result = fetch_bundles end if File.exist?(zipped_bundles_filepath) ReactOnRailsPro::Utils.rorp_puts "gunzipping bundle cache: #{zipped_bundles_filepath}" public_output_path = Shakapacker.config.public_output_path FileUtils.mkdir_p(public_output_path) Dir.chdir(public_output_path) do Rake.sh "tar -xzf #{zipped_bundles_filepath}" end ReactOnRailsPro::Utils.rorp_puts "gunzipped bundle cache: #{zipped_bundles_filepath} to #{public_output_path}" extract_extra_files_from_cache_dir end result end |
#fetch_bundles ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 271 def fetch_bundles ReactOnRailsPro::Utils.rorp_puts "Checking for a cached bundle: #{zipped_bundles_filename}" begin fetch_result = remote_bundle_cache_adapter.fetch(zipped_bundles_filename) rescue RuntimeError = "An error was raised by the custom config.remote_bundle_cache_adapter 'fetch' " \ "method when called with { zipped_bundles_filename: #{zipped_bundles_filename} }" ReactOnRailsPro::Utils.rorp_puts raise end if fetch_result ReactOnRailsPro::Utils.rorp_puts "Remote bundle cache detected. Bundles will be restored to local cache." File.binwrite(zipped_bundles_filepath, fetch_result) true else ReactOnRailsPro::Utils.rorp_puts "Remote bundle cache not found." false end end |
#remote_bundle_cache_adapter ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 13 def remote_bundle_cache_adapter unless ReactOnRailsPro.configuration.remote_bundle_cache_adapter.is_a?(Module) raise ReactOnRailsPro::Error, "config.remote_bundle_cache_adapter must have a module assigned" end ReactOnRailsPro.configuration.remote_bundle_cache_adapter end |
#remove_extra_files_cache_dir ⇒ Object
395 396 397 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 395 def remove_extra_files_cache_dir FileUtils.rm_f(extra_files_path) end |
#zipped_bundles_filename ⇒ Object
21 22 23 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 21 def zipped_bundles_filename "precompile-cache.#{bundles_cache_key}.production.gz" end |
#zipped_bundles_filepath ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/react_on_rails_pro/assets_precompile.rb', line 25 def zipped_bundles_filepath @zipped_bundles_filepath ||= begin FileUtils.mkdir_p(Rails.root.join("tmp", "bundle_cache")) Rails.root.join("tmp", "bundle_cache", zipped_bundles_filename) end end |