class SvelteOnRails::Lib::Utils
def self.precompile(last_mtime = nil)
def self.precompile(last_mtime = nil) config = SvelteOnRails::Configuration.instance Dir.chdir(config.rails_root) do # run build cmd = "./node_modules/.bin/vite build --config vite-ssr.config.ts" stdout, stderr, status = Open3.capture3(cmd) warnings = stderr.to_s.split("\n") errors_matcher = Regexp.new('(Could not resolve|failed to resolve)') error_lines = warnings.select { |e| e.match(errors_matcher) } have_error = error_lines.present? || status.to_s.match(/exit 1/) # error handling if stderr.present? red_background = "\033[97;41m" light_blue_background = "\033[30;106m" clear_colors = "\033[0m" msg = " +++ #{have_error ? 'ERROR' : 'WARNING'} compiling Svelte components#{have_error ? ' failed' : ''} («#{cmd}») +++ " puts "#{have_error ? red_background : light_blue_background}#{msg}#{clear_colors}" warnings.each do |e| if e.match(errors_matcher) red_font = "\033[31m" puts "#{red_background} #{clear_colors}#{red_font} #{e}#{clear_colors}" else puts "#{light_blue_background} #{clear_colors}#{e}" end end if have_error puts "#{red_background} +++ End of error message +++ #{clear_colors}" else puts "#{light_blue_background} +++ End of compiling warnings +++ #{clear_colors}" end puts "#{have_error ? red_background : light_blue_background} +++ Run «npm run build:ssr» on the console to see the original error message +++ #{clear_colors}" if have_error cl_str = if error_lines.present? "#{error_lines.join("\n")}\n\n" end raise "Svelte components compilation failed\n\n#{cl_str}Full message:\n+++\n#{stderr}+++\n\nYou can run «npm run build:ssr» on the console to see the original error message\n" end end puts stdout end unless Dir.exist?(config.ssr_dist_folder) raise "Could not find dist folder: #{config.ssr_dist_folder}" end if last_mtime mtime_path = config.ssr_dist_folder.join('last_mtime') File.write(mtime_path, last_mtime.to_s) end end