Module: Opal::Vite::Rails::Helper

Defined in:
lib/opal/vite/rails/helper.rb

Instance Method Summary collapse

Instance Method Details

#opal_asset_path(name) ⇒ Object

Get asset path from Vite manifest

NOTE: the manifest lives on the ViteRuby instanceViteRuby.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, **options)
  if vite_running?
    # Development: load from Vite dev server
    vite_javascript_tag("#{name}.js", **options)
  else
    # Production: load from manifest
    asset_path = opal_asset_path("#{name}.js")
    javascript_include_tag(asset_path, **options)
  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 opal_javascript_tags(*names, **options)
  safe_join(names.map { |name| opal_javascript_tag(name, **options) }, "\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(**options)
  if vite_running?
    vite_javascript_tag("@opal-runtime", **options)
  else
    asset_path = opal_asset_path("opal-runtime.js")
    javascript_include_tag(asset_path, **options)
  end
end

#vite_running?Boolean

Check if Vite dev server is running

Returns:

  • (Boolean)


31
32
33
# File 'lib/opal/vite/rails/helper.rb', line 31

def vite_running?
  defined?(ViteRuby) && ViteRuby.instance.dev_server_running?
end