class Webpacker::Compiler
def compilation_digest_path
def compilation_digest_path config.cache_path.join("last-compilation-digest-#{webpacker.env}") end
def compile
def compile if stale? run_webpack.tap do |success| record_compilation_digest if success end else true end end
def default_watched_paths
def default_watched_paths [ *config.resolved_paths_globbed, "#{config.source_path.relative_path_from(config.root_path)}/**/*", "yarn.lock", "package.json", "config/webpack/**/*" ].freeze end
def fresh?
def fresh? watched_files_digest == last_compilation_digest end
def initialize(webpacker)
def initialize(webpacker) @webpacker = webpacker end
def last_compilation_digest
def last_compilation_digest compilation_digest_path.read if compilation_digest_path.exist? && config.public_manifest_path.exist? rescue Errno::ENOENT, Errno::ENOTDIR end
def record_compilation_digest
def record_compilation_digest config.cache_path.mkpath compilation_digest_path.write(watched_files_digest) end
def run_webpack
def run_webpack logger.info "Compiling…" stdout, stderr, status = Open3.capture3( webpack_env, "#{RbConfig.ruby} ./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? else logger.error "Compilation failed:\n#{stderr}" end if config.webpack_compile_output? logger.info stdout end status.success? end
def stale?
def stale? !fresh? end
def watched_files_digest
def watched_files_digest 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
def webpack_env
def webpack_env return env unless defined?(ActionController::Base) env.merge("WEBPACKER_ASSET_HOST" => ENV.fetch("WEBPACKER_ASSET_HOST", ActionController::Base.helpers.compute_asset_host), "WEBPACKER_RELATIVE_URL_ROOT" => ENV.fetch("WEBPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root)) end