class RailsVite::Compiler

Attributes

rails_vite[R]

Public Class Methods

new(rails_vite) click to toggle source
# File lib/rails_vite/compiler.rb, line 14
def initialize(rails_vite)
  @rails_vite = rails_vite
end

Public Instance Methods

compile() click to toggle source
# File lib/rails_vite/compiler.rb, line 18
def compile
  if true #stale?
    run_vite.tap do |success|
      #record_compilation_digest
    end
  else
    true
  end
end
fresh?() click to toggle source

Returns true if all the compiled packs are up to date with the underlying asset files.

# File lib/rails_vite/compiler.rb, line 29
def fresh?
  last_compilation_digest&.== watched_files_digest
end
stale?() click to toggle source

Returns true if the compiled packs are out of date with the underlying asset files.

# File lib/rails_vite/compiler.rb, line 34
def stale?
  !fresh?
end

Private Instance Methods

compilation_digest_path() click to toggle source
# File lib/rails_vite/compiler.rb, line 95
def compilation_digest_path
  config.cache_path.join("last-compilation-digest-#{rails_vite.env}")
end
default_watched_paths() click to toggle source
# File lib/rails_vite/compiler.rb, line 85
def default_watched_paths
  [
    *config.additional_paths,
    "#{config.source_path}/**/*",
    'yarn.lock',
    'package.json',
    'config/vite/**/*'
  ].freeze
end
last_compilation_digest() click to toggle source
# File lib/rails_vite/compiler.rb, line 39
def last_compilation_digest
  compilation_digest_path.read if compilation_digest_path.exist? && config.public_manifest_path.exist?
rescue Errno::ENOENT, Errno::ENOTDIR
end
record_compilation_digest() click to toggle source
# File lib/rails_vite/compiler.rb, line 53
def record_compilation_digest
  config.cache_path.mkpath
  compilation_digest_path.write(watched_files_digest)
end
ruby_runner() click to toggle source
# File lib/rails_vite/compiler.rb, line 58
def ruby_runner
  bin_webpack_path = config.root_path.join('bin/vite')
  first_line = File.readlines(bin_webpack_path).first.chomp
  /ruby/.match?(first_line) ? RbConfig.ruby : ''
end
run_vite() click to toggle source
# File lib/rails_vite/compiler.rb, line 64
def run_vite
  logger.info 'RailsVite Compiling...'

  stdout, stderr, status = Open3.capture3(
    vite_env,
    "#{ruby_runner} ./bin/vite build",
    chdir: File.expand_path(config.root_path)
  )

  if status.success?
    logger.info "Compiled all packs in #{config.root_path}"
    logger.error "#{stderr}" unless stderr.empty?
    logger.info stdout
  else
    non_empty_streams = [stdout, stderr].delete_if(&:empty?)
    logger.error "Compilation failed:\n#{non_empty_streams.join("\n\n")}"
  end

  status.success?
end
vite_env() click to toggle source
# File lib/rails_vite/compiler.rb, line 99
def vite_env
  return env unless defined?(ActionController::Base)

  env.merge(
    'VITER_ASSET_HOST' => ENV.fetch('VITER_ASSET_HOST', ActionController::Base.helpers.compute_asset_host),
    'VITER_RELATIVE_URL_ROOT' => ENV.fetch('VITER_RELATIVE_URL_ROOT', ActionController::Base.relative_url_root),
    'VITER_CONFIG' => rails_vite.config_path.to_s
  )
end
watched_files_digest() click to toggle source
# File lib/rails_vite/compiler.rb, line 44
def watched_files_digest
  warn "Webpacker::Compiler.watched_paths has been deprecated. Set additional_paths in rails_vite.yml instead." unless watched_paths.empty?
  Dir.chdir File.expand_path(config.root_path) do
    files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
    file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
    Digest::SHA1.hexdigest(file_ids.join("/"))
  end
end