lib/bridgetown-lit-renderer/builder.rb in bridgetown-lit-renderer-1.0.0 vs lib/bridgetown-lit-renderer/builder.rb in bridgetown-lit-renderer-1.1.0

- old
+ new

@@ -1,91 +1,41 @@ # frozen_string_literal: true module BridgetownLitRenderer - module Renderer - def self.reset_cache - @code_cache = {} - @lit_notice_printed = false - end - - def self.esbuild(code, site) - unless @lit_notice_printed - Bridgetown.logger.info "Lit SSR:", "Bundling with esbuild..." - @lit_notice_printed = true + class Builder < Bridgetown::Builder + def build # rubocop:todo Metrics/AbcSize, Metrics/MethodLength + BridgetownLitRenderer::Renderer.instance.site = site + BridgetownLitRenderer::Renderer.instance.reset + hook :site, :post_render do + BridgetownLitRenderer::Renderer.stop_node_server end - IO.popen(["node", site.in_root_dir("./config/lit-ssr.config.js")], "r+") do |pipe| - pipe.puts({ code: code }.to_json) - pipe.close_write - pipe.read - end - end - - def self.render(code, data:, entry:, site:) # rubocop:disable Metrics/MethodLength - @code_cache ||= {} - cache_key = "#{code}#{entry}" - - unless @code_cache[cache_key] - build_code = <<~JS - import { Readable } from "stream" - import { render } from "@lit-labs/ssr/lib/render-with-global-dom-shim.js" - import { html } from "lit" - import #{entry.to_json} - - const ssrResult = render(html` - #{code} - `); - - const _tmplStream = Readable.from(ssrResult) - - let _tmplOutput = "" - _tmplStream.on('data', function(chunk) { - _tmplOutput += chunk; - }); - - _tmplStream.on('end',function() { - process.stdout.write("====== SSR ======") // marker to ensure stray console outputs don't end up in HTML - process.stdout.write(_tmplOutput) - }); - JS - - @code_cache[cache_key] = esbuild(build_code, site) - end - - IO.popen(["node"], "r+") do |pipe| - pipe.puts "const data = #{data.to_json}; #{@code_cache[cache_key]}" - pipe.close_write - pipe.read - end.partition("====== SSR ======").last.html_safe - end - end - - class Builder < Bridgetown::Builder - def self.cache - @cache ||= Bridgetown::Cache.new("LitSSR") - end - - def build - BridgetownLitRenderer::Renderer.reset_cache helper "lit", helpers_scope: true do | data: {}, hydrate_root: true, entry: "./frontend/javascript/lit-components.js", &block | code = view.capture(&block) if hydrate_root - code = "<hydrate-root>#{code.sub(%r{\<([a-zA-Z]+-[a-zA-Z-]*)}, "<\\1 defer-hydration")}</hydrate-root>" # rubocop:disable Layout/LineLength + code = "<hydrate-root>#{code.sub(%r{<([a-zA-Z]+-[a-zA-Z-]*)}, "<\\1 defer-hydration")}</hydrate-root>" # rubocop:disable Layout/LineLength end - if site.config.disable_lit_caching - next BridgetownLitRenderer::Renderer.render(code, data: data, entry: entry, site: site) + render_fn = -> do + BridgetownLitRenderer::Renderer.instance.render( + code, + data: data, + entry: entry, + caching: !site.config.disable_lit_caching + ) end - entry_key = entry.start_with?("./") ? File.stat(site.in_root_dir(entry)).mtime : entry - BridgetownLitRenderer::Builder.cache.getset("#{code}#{data}#{entry_key}") do - BridgetownLitRenderer::Renderer.render(code, data: data, entry: entry, site: site) - end + next render_fn.() if site.config.disable_lit_caching + + entry_key = BridgetownLitRenderer::Renderer.instance.entry_key(entry) + BridgetownLitRenderer::Renderer.instance.cache.getset( + "output-#{code}#{data}#{entry_key}" + ) { render_fn.() } end end end end