Skip to content

[v4] Troubleshooting issue with Slim #2409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down Expand Up @@ -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?
Expand Down
7 changes: 6 additions & 1 deletion lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions spec/components/slim_component_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class SlimComponentPreview < ViewComponent::Preview
def default
render(SlimComponent.new(message: "Hello"))
end
end
Loading