Module: MiniviteRails::TagHelpers
- Defined in:
- lib/minivite_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, id: nil, **options) ⇒ Object
Public: Resolves the path for the specified Vite asset.
-
#vite_asset_url(name, id: nil, **options) ⇒ Object
Public: Resolves the url for the specified Vite asset.
-
#vite_client_tag(id: nil, crossorigin: 'anonymous', **options) ⇒ Object
Public: Renders a script tag for vite/client to enable HMR in development.
-
#vite_image_tag(name, id: nil, **options) ⇒ Object
Public: Renders an <img> tag for the specified Vite asset.
-
#vite_javascript_tag(*names, id: nil, 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_public_asset_path(name, id: nil) ⇒ Object
Public: Resolves the path for Vite’s public assets.
-
#vite_react_refresh_tag(id: nil, **options) ⇒ Object
Public: Renders a script tag to enable HMR with React Refresh.
-
#vite_stylesheet_tag(*names, id: nil, **options) ⇒ Object
Public: Renders a <link> tag for the specified Vite entrypoints.
-
#vite_typescript_tag(*names, id: nil, **options) ⇒ Object
Public: Renders a <script> tag for the specified Vite entrypoints.
Instance Method Details
#vite_asset_path(name, id: nil, **options) ⇒ Object
Public: Resolves the path for the specified Vite asset.
Example:
<%= vite_asset_path 'calendar.css' %> # => "/vite/assets/calendar-1016838bab065ae1e122.css"
33 34 35 |
# File 'lib/minivite_rails/tag_helpers.rb', line 33 def vite_asset_path(name, id: nil, **) path_to_asset vite_manifest(id:).path_for(name, **) end |
#vite_asset_url(name, id: nil, **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"
41 42 43 |
# File 'lib/minivite_rails/tag_helpers.rb', line 41 def vite_asset_url(name, id: nil, **) url_to_asset vite_manifest(id:).path_for(name, **) end |
#vite_client_tag(id: nil, crossorigin: 'anonymous', **options) ⇒ Object
Public: Renders a script tag for vite/client to enable HMR in development.
12 13 14 15 16 17 |
# File 'lib/minivite_rails/tag_helpers.rb', line 12 def vite_client_tag(id: nil, crossorigin: 'anonymous', **) src = vite_manifest(id:).vite_client_src return unless src javascript_include_tag(src, type: 'module', extname: false, crossorigin:, **) end |
#vite_image_tag(name, id: nil, **options) ⇒ Object
Public: Renders an <img> tag for the specified Vite asset.
90 91 92 93 94 95 96 97 98 |
# File 'lib/minivite_rails/tag_helpers.rb', line 90 def vite_image_tag(name, id: nil, **) if [:srcset] && ![:srcset].is_a?(String) [:srcset] = [:srcset].map do |src_name, size| "#{vite_asset_path(src_name, id:)} #{size}" end.join(', ') end image_tag(vite_asset_path(name, id:), ) end |
#vite_javascript_tag(*names, id: nil, 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/minivite_rails/tag_helpers.rb', line 54 def vite_javascript_tag(*names, # rubocop:disable Metrics/ParameterLists id: nil, type: 'module', asset_type: :javascript, skip_preload_tags: false, skip_style_tags: false, crossorigin: 'anonymous', media: 'screen', **) entries = vite_manifest(id:).resolve_entries(*names, type: asset_type) = javascript_include_tag(*entries.fetch(:scripts), crossorigin:, type:, extname: false, **) << vite_preload_tag(*entries.fetch(:imports), crossorigin:, **) unless [:extname] = false if Rails::VERSION::MAJOR >= 7 << stylesheet_link_tag(*entries.fetch(:stylesheets), media:, **) unless end |
#vite_public_asset_path(name, id: nil) ⇒ Object
Public: Resolves the path for Vite’s public assets
Example:
<%= vite_public_asset_path 'logo.svg' %> # => "/vite/logo.svg"
49 50 51 |
# File 'lib/minivite_rails/tag_helpers.rb', line 49 def vite_public_asset_path(name, id: nil) path_to_asset vite_manifest(id:).public_path_for(name) end |
#vite_react_refresh_tag(id: nil, **options) ⇒ Object
Public: Renders a script tag to enable HMR with React Refresh.
20 21 22 23 24 25 26 27 |
# File 'lib/minivite_rails/tag_helpers.rb', line 20 def vite_react_refresh_tag(id: nil, **) react_preamble_code = vite_manifest(id:).react_preamble_code return unless 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, id: nil, **options) ⇒ Object
Public: Renders a <link> tag for the specified Vite entrypoints.
81 82 83 84 85 86 87 |
# File 'lib/minivite_rails/tag_helpers.rb', line 81 def vite_stylesheet_tag(*names, id: nil, **) style_paths = names.map { |name| vite_asset_path(name, id:, type: :stylesheet) } [:extname] = false if Rails::VERSION::MAJOR >= 7 stylesheet_link_tag(*style_paths, **) end |
#vite_typescript_tag(*names, id: nil, **options) ⇒ Object
Public: Renders a <script> tag for the specified Vite entrypoints.
76 77 78 |
# File 'lib/minivite_rails/tag_helpers.rb', line 76 def vite_typescript_tag(*names, id: nil, **) vite_javascript_tag(*names, id:, asset_type: :typescript, **) end |