lib/svelte_on_rails/renderer/renderer.rb



module SvelteOnRails
  class Renderer

    require 'open3'

    def initialize(component_name, base_path: SvelteOnRails::Configuration.instance.components_folder_full)

      config = SvelteOnRails::Configuration.instance

      unless system("#{config.node_bin_path} --version > /dev/null 2>&1")
        raise "Node.js not found at '#{config.node_bin_path}'. Please configure SvelteOnRails.node_bin (e.g., to ~/.nvm/versions/node/vX.Y.Z/bin/node) or ensure 'node' is in the PATH. If using NVM, run `nvm which default` to find the path."
      end

      if !Dir.exist?(config.ssr_dist_folder) || config.watch_changes?
        SvelteOnRails::Lib::Utils.watch_changes_and_precompile
      end

      utils = SvelteOnRails::Lib::Utils
      @component_files = utils.component_files(component_name, base_path: base_path)

    end

    # render

    def render(props = {})
      require 'base64'
      require 'json'
      utils = SvelteOnRails::Lib::Utils
      cnf = SvelteOnRails::Configuration.instance

      cmd = [
        cnf.node_bin_path,
        File.join(utils.gem_app_dir, 'renderer', 'render.js'),
        @component_files[:compiled_file] + '.js',
        SvelteOnRails::Configuration.instance.rails_root
      ].join(' ')

      stdout, stderr, status = Open3.capture3(cmd, stdin_data: props.to_json, chdir: utils.gem_app_dir)


      unless status.to_s.match(/^pid [0-9]+ exit 0$/)
        cmp = "#{@component_files[:svelte_filename]} was returned «#{status.to_s}»\n\n"
        msg = "#{cmp}output from render.js (stderr) =>\n+++\n" + stderr + "+++\n\nRender Svelte Server-side =>\n#{cmd}\n\n"
        utils.puts_warning(msg)
      end


      begin
        res = JSON.parse(stdout)
        css_file = @component_files[:compiled_file] + '.css'
        if File.exist?(css_file)
          res['css'] = File.read(css_file)
        end


        return res
      rescue JSON::ParserError => e
        raise "Render Svelte Server-side: Expected JSON, got: «#{stdout}»"
      end
    end

  end
end