Module: Opal::Vite::Rails::Helper
- Defined in:
- lib/opal/vite/rails/helper.rb
Instance Method Summary collapse
-
#opal_asset_path(name) ⇒ Object
Get asset path from Vite manifest.
-
#opal_javascript_tag(name, **options) ⇒ Object
Generate script tag for Opal JavaScript.
-
#opal_javascript_tags(*names, **options) ⇒ Object
Generate multiple script tags for Opal JavaScript files.
-
#opal_runtime_tag(**options) ⇒ Object
Include Opal runtime This is automatically included when using opal_javascript_tag, but can be called explicitly if needed.
-
#vite_running? ⇒ Boolean
Check if Vite dev server is running.
Instance Method Details
#opal_asset_path(name) ⇒ Object
Get asset path from Vite manifest
NOTE: the manifest lives on the ViteRuby instance — ViteRuby.manifest
is not a class-level delegator (it raises NoMethodError), so we go
through ViteRuby.instance. Use ViteRuby::Manifest#path_for (public)
rather than #lookup: #lookup is protected and returns the raw manifest
entry Hash ({ "file" => "..." }), so lookup(name).to_s would emit the
Hash's inspect string instead of a URL. #path_for returns the resolved
asset URL string.
44 45 46 47 48 49 50 51 |
# File 'lib/opal/vite/rails/helper.rb', line 44 def opal_asset_path(name) if defined?(ViteRuby) ViteRuby.instance.manifest.path_for(name) else # Fallback to standard asset path "/#{Opal::Vite::Rails.config.public_output_path}/#{name}" end end |
#opal_javascript_tag(name, **options) ⇒ Object
Generate script tag for Opal JavaScript
Usage in views:
<%= opal_javascript_tag "application" %>
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/opal/vite/rails/helper.rb', line 10 def opal_javascript_tag(name, **) if vite_running? # Development: load from Vite dev server vite_javascript_tag("#{name}.js", **) else # Production: load from manifest asset_path = opal_asset_path("#{name}.js") javascript_include_tag(asset_path, **) end end |
#opal_javascript_tags(*names, **options) ⇒ Object
Generate multiple script tags for Opal JavaScript files
Usage:
<%= opal_javascript_tags "application", "components/widget" %>
26 27 28 |
# File 'lib/opal/vite/rails/helper.rb', line 26 def (*names, **) safe_join(names.map { |name| opal_javascript_tag(name, **) }, "\n") end |
#opal_runtime_tag(**options) ⇒ Object
Include Opal runtime This is automatically included when using opal_javascript_tag, but can be called explicitly if needed
56 57 58 59 60 61 62 63 |
# File 'lib/opal/vite/rails/helper.rb', line 56 def opal_runtime_tag(**) if vite_running? vite_javascript_tag("@opal-runtime", **) else asset_path = opal_asset_path("opal-runtime.js") javascript_include_tag(asset_path, **) end end |
#vite_running? ⇒ Boolean
Check if Vite dev server is running
31 32 33 |
# File 'lib/opal/vite/rails/helper.rb', line 31 def vite_running? defined?(ViteRuby) && ViteRuby.instance.dev_server_running? end |