class Webpacker::Compiler

Public Class Methods

new(webpacker) click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 15
def initialize(webpacker)
  @webpacker = webpacker
end

Public Instance Methods

compile() click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 19
def compile
  if stale?
    record_compilation_digest
    run_webpack
  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/webpacker-for-component/compiler.rb, line 29
def fresh?
  watched_files_digest == last_compilation_digest
end
stale?() click to toggle source

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

# File lib/webpacker-for-component/compiler.rb, line 34
def stale?
  !fresh?
end

Private Instance Methods

compilation_digest_path() click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 71
def compilation_digest_path
  config.cache_path.join(".last-compilation-digest")
end
default_watched_paths() click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 67
def default_watched_paths
  ["#{config.source_path}/**/*", "yarn.lock", "package.json", "config/webpack/**/*"].freeze
end
last_compilation_digest() click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 39
def last_compilation_digest
  compilation_digest_path.read if compilation_digest_path.exist? && config.public_manifest_path.exist?
end
record_compilation_digest() click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 48
def record_compilation_digest
  config.cache_path.mkpath
  compilation_digest_path.write(watched_files_digest)
end
run_webpack() click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 53
def run_webpack
  logger.info "Compiling…"

  sterr, stdout, status = Open3.capture3(webpack_env, "#{RbConfig.ruby} ./bin/webpack")

  if status.success?
    logger.info "Compiled all packs in #{config.public_output_path}"
  else
    logger.error "Compilation failed:\n#{sterr}\n#{stdout}"
  end

  status.success?
end
watched_files_digest() click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 43
def watched_files_digest
  files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
  Digest::SHA1.hexdigest(files.map { |f| "#{File.basename(f)}/#{File.mtime(f).utc.to_i}" }.join("/"))
end
webpack_env() click to toggle source
# File lib/webpacker-for-component/compiler.rb, line 75
def webpack_env
  env.merge("NODE_ENV" => @webpacker.env, "ASSET_HOST" => ActionController::Base.helpers.compute_asset_host)
end