Module: ViteRails::TagHelpers
- Defined in:
- lib/vite_rails/tag_helpers.rb
Overview
Public: Allows to render HTML tags for scripts and styles processed by Vite.
Instance Method Summary collapse
-
#vite_asset_path(name, **options) ⇒ Object
Public: Resolves the path for the specified Vite asset.
-
#vite_client_tag ⇒ Object
Public: Renders a script tag for vite/client to enable HMR in development.
-
#vite_image_tag(name, **options) ⇒ Object
Public: Renders an <img> tag for the specified Vite asset.
-
#vite_javascript_tag(*names, type: 'module', asset_type: :javascript, integrity: false, skip_preload_tags: false, skip_style_tags: false, crossorigin: 'anonymous', media: 'screen', **options) ⇒ Object
Public: Renders a <script> tag for the specified Vite entrypoints.
-
#vite_react_refresh_tag ⇒ Object
Public: Renders a script tag to enable HMR with React Refresh.
-
#vite_stylesheet_tag(*names, integrity: false, **options) ⇒ Object
Public: Renders a <link> tag for the specified Vite entrypoints.
-
#vite_typescript_tag(*names, **options) ⇒ Object
Public: Renders a <script> tag for the specified Vite entrypoints.
Instance Method Details
#vite_asset_path(name, **options) ⇒ Object
Public: Resolves the path for the specified Vite asset.
Example:
<%= vite_asset_path 'calendar.css' %> # => "/vite/assets/calendar-1016838bab065ae1e122.css"
21 22 23 |
# File 'lib/vite_rails/tag_helpers.rb', line 21 def vite_asset_path(name, **) path_to_asset vite_manifest.path_for(name, **) end |
#vite_client_tag ⇒ Object
Public: Renders a script tag for vite/client to enable HMR in development.
6 7 8 9 10 |
# File 'lib/vite_rails/tag_helpers.rb', line 6 def vite_client_tag return unless src = vite_manifest.vite_client_src tag.script(src: src, type: 'module') end |
#vite_image_tag(name, **options) ⇒ Object
Public: Renders an <img> tag for the specified Vite asset.
68 69 70 71 72 73 74 75 76 |
# File 'lib/vite_rails/tag_helpers.rb', line 68 def vite_image_tag(name, **) if [:srcset] && ![:srcset].is_a?(String) [:srcset] = [:srcset].map do |src_name, size| "#{ vite_asset_path(src_name) } #{ size }" end.join(', ') end image_tag(vite_asset_path(name), ) end |
#vite_javascript_tag(*names, type: 'module', asset_type: :javascript, integrity: false, skip_preload_tags: false, skip_style_tags: false, crossorigin: 'anonymous', media: 'screen', **options) ⇒ Object
Public: Renders a <script> tag for the specified Vite entrypoints.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/vite_rails/tag_helpers.rb', line 26 def vite_javascript_tag(*names, type: 'module', asset_type: :javascript, integrity: false, skip_preload_tags: false, skip_style_tags: false, crossorigin: 'anonymous', media: 'screen', **) entries = vite_manifest.resolve_entries(*names, type: asset_type) = ''.html_safe entries.fetch(:main).each do |src, attrs| << javascript_include_tag(src, crossorigin: crossorigin, type: type, extname: false, **attrs, **) end unless entries.fetch(:imports).each do |href, attrs| << vite_preload_tag(href, crossorigin: crossorigin, **attrs, **) end end << stylesheet_link_tag(*entries.fetch(:stylesheets), media: media, crossorigin: crossorigin, **) unless end |
#vite_react_refresh_tag ⇒ Object
Public: Renders a script tag to enable HMR with React Refresh.
13 14 15 |
# File 'lib/vite_rails/tag_helpers.rb', line 13 def vite_react_refresh_tag vite_manifest.react_refresh_preamble&.html_safe end |
#vite_stylesheet_tag(*names, integrity: false, **options) ⇒ Object
Public: Renders a <link> tag for the specified Vite entrypoints.
59 60 61 62 63 64 65 |
# File 'lib/vite_rails/tag_helpers.rb', line 59 def vite_stylesheet_tag(*names, integrity: false, **) ''.html_safe.tap do || vite_manifest.resolve_entries(*names, type: :stylesheet).fetch(:main).each do |href, attrs| << stylesheet_link_tag(href, **attrs, **) end end end |
#vite_typescript_tag(*names, **options) ⇒ Object
Public: Renders a <script> tag for the specified Vite entrypoints.
54 55 56 |
# File 'lib/vite_rails/tag_helpers.rb', line 54 def vite_typescript_tag(*names, **) vite_javascript_tag(*names, asset_type: :typescript, **) end |