diff --git a/lib/view_component/compiler.rb b/lib/view_component/compiler.rb index 130ecbde3..e53ed740a 100644 --- a/lib/view_component/compiler.rb +++ b/lib/view_component/compiler.rb @@ -28,6 +28,10 @@ def compile(raise_errors: false, force: false) # early exit return if compiled? && !force + # TODO: Needs to reset the template errors in case of Slim "lazy-loaded" (?) + reset_template_errors + # TODO: Needs to reset the templates in case of Slim "lazy-loaded" + reset_templates gather_templates if self.class.__vc_development_mode && @templates.any?(&:requires_compiled_superclass?) @@ -165,6 +169,14 @@ def template_errors end end + def reset_template_errors + @_template_errors = nil + end + + def reset_templates + @templates = nil + end + def gather_templates @templates ||= if @component.__vc_inline_template.present? diff --git a/lib/view_component/engine.rb b/lib/view_component/engine.rb index 73f9cd4d6..a13c23619 100644 --- a/lib/view_component/engine.rb +++ b/lib/view_component/engine.rb @@ -77,7 +77,12 @@ class Engine < Rails::Engine # :nodoc: initializer "view_component.eager_load_actions" do ActiveSupport.on_load(:after_initialize) do - ViewComponent::Base.descendants.each(&:__vc_compile) if Rails.application.config.eager_load + # ViewComponent::Base.descendants.each(&:__vc_compile) if Rails.application.config.eager_load + + ViewComponent::Base.descendants.each do |c| + # FIXME: trying to not precompile SlimComponent to validate the specs + c.__vc_compile if c != SlimComponent + end end end diff --git a/spec/components/slim_component_spec.rb b/spec/components/slim_component_spec.rb new file mode 100644 index 000000000..f060b110a --- /dev/null +++ b/spec/components/slim_component_spec.rb @@ -0,0 +1,37 @@ +describe SlimComponent do + + before do + ViewComponent::CompileCache.invalidate! + + # FIXME: found a better way to "simulate" Slim delay initialization + ActionView::Template.unregister_template_handler(:slim) + Slim.send(:remove_const, :RailsTemplate) if defined?(Slim::RailsTemplate) + end + + context "when slim has not been loaded yet" do + it "raise error" do + expect { render_preview(:default) }.to raise_error(/Couldn't find a template file or inline render method for SlimComponent/) + end + end + + context "when slim has been lazy loaded" do + before do + require "slim" + + # Same stuff as to simulate lazy load: https://github.com/slim-template/slim/blob/main/lib/slim/railtie.rb#L7 + Slim::RailsTemplate = Temple::Templates::Rails( + Slim::Engine, + register_as: :slim, + generator: Temple::Generators::RailsOutputBuffer, + disable_capture: true, + streaming: true + ) + end + + it "renders the component" do + render_preview(:default) + + expect(page).to have_css "div.slim-div", text: "Hello" + end + end +end diff --git a/test/sandbox/test/components/previews/slim_component_preview.rb b/test/sandbox/test/components/previews/slim_component_preview.rb new file mode 100644 index 000000000..1317a7e45 --- /dev/null +++ b/test/sandbox/test/components/previews/slim_component_preview.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class SlimComponentPreview < ViewComponent::Preview + def default + render(SlimComponent.new(message: "Hello")) + end +end