class Viter::Compiler

Attributes

webpacker[R]

Public Class Methods

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

Public Instance Methods

compile() click to toggle source
# File lib/viter/compiler.rb, line 22
def compile
  if stale?
    run_webpack.tap do |success|
      # We used to only record the digest on success
      # However, the output file is still written on error, meaning that the digest should still be updated.
      # If it's not, you can end up in a situation where a recompile doesn't take place when it should.
      # See https://github.com/rails/webpacker/issues/2113
      record_compilation_digest
    end
  else
    logger.debug "Everything's up-to-date. Nothing to do"
    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/viter/compiler.rb, line 38
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/viter/compiler.rb, line 43
def stale?
  !fresh?
end

Private Instance Methods

compilation_digest_path() click to toggle source
# File lib/viter/compiler.rb, line 109
def compilation_digest_path
  config.cache_path.join("last-compilation-digest-#{webpacker.env}")
end
default_watched_paths() click to toggle source
# File lib/viter/compiler.rb, line 99
def default_watched_paths
  [
    *config.additional_paths,
    "#{config.source_path}/**/*",
    'yarn.lock',
    'package.json',
    'config/webpack/**/*'
  ].freeze
end
last_compilation_digest() click to toggle source
# File lib/viter/compiler.rb, line 50
def last_compilation_digest
  compilation_digest_path.read if compilation_digest_path.exist? && config.public_manifest_path.exist?
rescue Errno::ENOENT, Errno::ENOTDIR
end
optionalRubyRunner() click to toggle source
# File lib/viter/compiler.rb, line 69
def optionalRubyRunner
  bin_webpack_path = config.root_path.join('bin/viter')
  first_line = File.readlines(bin_webpack_path).first.chomp
  /ruby/.match?(first_line) ? RbConfig.ruby : ""
end
record_compilation_digest() click to toggle source
# File lib/viter/compiler.rb, line 64
def record_compilation_digest
  config.cache_path.mkpath
  compilation_digest_path.write(watched_files_digest)
end
run_webpack() click to toggle source
# File lib/viter/compiler.rb, line 75
def run_webpack
  logger.info 'Viter Compiling...'

  stdout, stderr, status = Open3.capture3(
    webpack_env,
    "#{optionalRubyRunner} ./bin/webpack",
    chdir: File.expand_path(config.root_path)
  )

  if status.success?
    logger.info "Compiled all packs in #{config.public_output_path}"
    logger.error "#{stderr}" unless stderr.empty?

    if config.webpack_compile_output?
      logger.info stdout
    end
  else
    non_empty_streams = [stdout, stderr].delete_if(&:empty?)
    logger.error "Compilation failed:\n#{non_empty_streams.join("\n\n")}"
  end

  status.success?
end
watched_files_digest() click to toggle source
# File lib/viter/compiler.rb, line 55
def watched_files_digest
  warn "Webpacker::Compiler.watched_paths has been deprecated. Set additional_paths in webpacker.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
webpack_env() click to toggle source
# File lib/viter/compiler.rb, line 113
def webpack_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' => webpacker.config_path.to_s
  )
end