Skip to content
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

feat: Add vite_picture_tag for Rails 7.1 #409

Merged
merged 2 commits into from Oct 5, 2023

Conversation

mattbrictson
Copy link
Contributor

Description πŸ“–

This commit adds a vite_picture_tag helper that handles resolving assets via Vite, but otherwise behaves the same as the Rails built-in picture_tag. It is only exposed for Rails >= 7.1.0. Fixes #386

Background πŸ“œ

Rails 7.1 introduced a picture_tag helper.1

The Fix πŸ”¨

Added vite_picture_tag to ViteRails::TagHelpers with a corresponding test.

Footnotes

  1. https://github.com/rails/rails/pull/48100 ↩

actioncable (6.1.3.1)
actionpack (= 6.1.3.1)
activesupport (= 6.1.3.1)
actioncable (7.1.0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ—’οΈ I updated the lock file to be able to test against Rails 7.1.0. Let me know if I should split this into a separate commit or PR.

@@ -79,6 +79,17 @@ def vite_image_tag(name, **options)
image_tag(vite_asset_path(name), options)
end

if defined?(Rails) && Rails.gem_version >= Gem::Version.new('7.1.0')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ€” I had to add the defined? check to work around certain test failures. I assume that Rails will be defined when this file is loaded in a real app. Is there a better way to do this?

Copy link
Owner

@ElMassimo ElMassimo Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That approach would have worked, but changed my mind as I'd like to provide more information in case users see it in the docs and try to use it in an older version of Rails.

Rails 7.1 introduced a `picture_tag` helper.[^1] This commit adds a
corresponding `vite_picture_tag` helper that handles resolving assets
via Vite, but otherwise behaves the same as `picture_tag`. It is only
exposed for Rails >= 7.1.0.

[^1]: rails/rails#48100
@ElMassimo ElMassimo merged commit 4e3762a into ElMassimo:main Oct 5, 2023
13 checks passed
@ElMassimo
Copy link
Owner

ElMassimo commented Oct 5, 2023

Nice addition, thanks Matt!

Released in vite_rails-3.0.17.

@mattbrictson mattbrictson deleted the features/vite_picture_tag branch October 5, 2023 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: vite_picture_tag
2 participants