class ReactOnRails::TestHelper::WebpackAssetsStatusChecker

Attributes

generated_assets_full_path[R]

source_path is typically configured in the (shaka/web)packer.yml file for ‘source_path` or for legacy React on Rails, it’s /client, where all client files go

source_path[R]

source_path is typically configured in the (shaka/web)packer.yml file for ‘source_path` or for legacy React on Rails, it’s /client, where all client files go

Public Class Methods

new( generated_assets_full_path: required("generated_assets_full_path"), source_path: required("source_path"), webpack_generated_files: required("webpack_generated_files") ) click to toggle source
# File lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, line 18
def initialize(
  generated_assets_full_path: required("generated_assets_full_path"),
  source_path: required("source_path"),
  webpack_generated_files: required("webpack_generated_files")
)
  @generated_assets_full_path = generated_assets_full_path
  @source_path = source_path
  @webpack_generated_files = webpack_generated_files
end

Public Instance Methods

stale_generated_files(files) click to toggle source
# File lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, line 32
def stale_generated_files(files)
  manifest_needed = ReactOnRails::PackerUtils.using_packer? &&
                    !ReactOnRails::PackerUtils.manifest_exists?

  return ["manifest.json"] if manifest_needed

  most_recent_mtime = Utils.find_most_recent_mtime(files)
  all_compiled_assets.each_with_object([]) do |webpack_generated_file, stale_gen_list|
    if !File.exist?(webpack_generated_file) ||
       File.mtime(webpack_generated_file) < most_recent_mtime
      stale_gen_list << webpack_generated_file
    end
    stale_gen_list
  end
end
stale_generated_webpack_files() click to toggle source
# File lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, line 28
def stale_generated_webpack_files
  stale_generated_files(client_files)
end

Private Instance Methods

all_compiled_assets() click to toggle source
# File lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, line 50
def all_compiled_assets
  @all_compiled_assets ||= begin
    webpack_generated_files = @webpack_generated_files.map do |bundle_name|
      if bundle_name == ReactOnRails.configuration.server_bundle_js_file
        ReactOnRails::Utils.server_bundle_js_file_path
      else
        ReactOnRails::Utils.bundle_js_file_path(bundle_name)
      end
    end

    if webpack_generated_files.present?
      webpack_generated_files
    else
      file_list = make_file_list(make_globs(generated_assets_full_path)).to_ary
      puts "V" * 80
      puts "Please define config.webpack_generated_files (array) so the test helper knows " \
           "which files are required. If you are using Shakapacker, you typically need to only " \
           "include 'manifest.json'."
      puts "Detected the possible following files to check for webpack compilation in " \
           "#{generated_assets_full_path}"
      puts file_list.join("\n")
      puts "^" * 80
      file_list
    end
  end
end
assets_exist?() click to toggle source
# File lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, line 85
def assets_exist?
  !all_compiled_assets.empty?
end
client_files() click to toggle source
# File lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, line 77
def client_files
  @client_files ||= make_file_list(make_globs(source_path)).to_ary
end
make_file_list(glob) click to toggle source
# File lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, line 89
def make_file_list(glob)
  FileList.new(glob) do |fl|
    fl.exclude(%r{/node_modules})
    fl.exclude(".DS_Store")
    fl.exclude(".keep")
    fl.exclude("thumbs.db")
    fl.exclude(".")
    fl.exclude("..")
  end
end
make_globs(dirs) click to toggle source
# File lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, line 81
def make_globs(dirs)
  Array(dirs).map { |dir| File.join(dir, "**", "*") }
end