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_asset_url(name, **options) ⇒ Object
Public: Resolves the url for the specified Vite asset.
-
#vite_client_tag(crossorigin: 'anonymous', **options) ⇒ 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, 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_picture_tag(*sources, &block) ⇒ Object
Public: Renders a <picture> tag with one or more Vite asset sources.
-
#vite_react_refresh_tag(**options) ⇒ Object
Public: Renders a script tag to enable HMR with React Refresh.
-
#vite_stylesheet_tag(*names, **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"
25 26 27 |
# File 'lib/vite_rails/tag_helpers.rb', line 25 def vite_asset_path(name, **) path_to_asset vite_manifest.path_for(name, **) end |
#vite_asset_url(name, **options) ⇒ Object
Public: Resolves the url for the specified Vite asset.
Example:
<%= vite_asset_url 'calendar.css' %> # => "https://example.com/vite/assets/calendar-1016838bab065ae1e122.css"
33 34 35 |
# File 'lib/vite_rails/tag_helpers.rb', line 33 def vite_asset_url(name, **) url_to_asset vite_manifest.path_for(name, **) end |
#vite_client_tag(crossorigin: 'anonymous', **options) ⇒ 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(crossorigin: 'anonymous', **) return unless src = vite_manifest.vite_client_src javascript_include_tag(src, type: 'module', extname: false, crossorigin: crossorigin, **) end |
#vite_image_tag(name, **options) ⇒ Object
Public: Renders an <img> tag for the specified Vite asset.
72 73 74 75 76 77 78 79 80 |
# File 'lib/vite_rails/tag_helpers.rb', line 72 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, skip_preload_tags: false, skip_style_tags: false, crossorigin: 'anonymous', media: 'screen', **options) ⇒ Object
Public: Renders a <script> tag for the specified Vite entrypoints.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vite_rails/tag_helpers.rb', line 38 def vite_javascript_tag(*names, type: 'module', asset_type: :javascript, skip_preload_tags: false, skip_style_tags: false, crossorigin: 'anonymous', media: 'screen', **) entries = vite_manifest.resolve_entries(*names, type: asset_type) = javascript_include_tag(*entries.fetch(:scripts), crossorigin: crossorigin, type: type, extname: false, **) << vite_preload_tag(*entries.fetch(:imports), crossorigin: crossorigin, **) unless [:extname] = false if Rails::VERSION::MAJOR >= 7 << stylesheet_link_tag(*entries.fetch(:stylesheets), media: media, **) unless end |
#vite_picture_tag(*sources, &block) ⇒ Object
Public: Renders a <picture> tag with one or more Vite asset sources.
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vite_rails/tag_helpers.rb', line 83 def vite_picture_tag(*sources, &block) unless Rails.gem_version >= Gem::Version.new('7.1.0') raise NotImplementedError, '`vite_picture_tag` is only available for Rails 7.1 or above.' end sources.flatten! = sources. vite_sources = sources.map { |src| vite_asset_path(src) } picture_tag(*vite_sources, , &block) end |
#vite_react_refresh_tag(**options) ⇒ Object
Public: Renders a script tag to enable HMR with React Refresh.
13 14 15 16 17 18 19 |
# File 'lib/vite_rails/tag_helpers.rb', line 13 def vite_react_refresh_tag(**) return unless react_preamble_code = vite_manifest.react_preamble_code [:nonce] = true if Rails::VERSION::MAJOR >= 6 && !.key?(:nonce) javascript_tag(react_preamble_code.html_safe, type: :module, **) end |
#vite_stylesheet_tag(*names, **options) ⇒ Object
Public: Renders a <link> tag for the specified Vite entrypoints.
63 64 65 66 67 68 69 |
# File 'lib/vite_rails/tag_helpers.rb', line 63 def vite_stylesheet_tag(*names, **) style_paths = names.map { |name| vite_asset_path(name, type: :stylesheet) } [:extname] = false if Rails::VERSION::MAJOR >= 7 stylesheet_link_tag(*style_paths, **) end |
#vite_typescript_tag(*names, **options) ⇒ Object
Public: Renders a <script> tag for the specified Vite entrypoints.
58 59 60 |
# File 'lib/vite_rails/tag_helpers.rb', line 58 def vite_typescript_tag(*names, **) vite_javascript_tag(*names, asset_type: :typescript, **) end |