Module: ViteHelper

Included in:
ApplicationHelper, WebpackHelper
Defined in:
app/helpers/vite_helper.rb

Instance Method Summary collapse

Instance Method Details

#universal_path_to_stylesheet(path, **options) ⇒ Object



49
50
51
52
53
# File 'app/helpers/vite_helper.rb', line 49

def universal_path_to_stylesheet(path, **options)
  return ActionController::Base.helpers.stylesheet_path(path, **options) unless vite_enabled?

  ViteRuby.instance.manifest.path_for("stylesheets/styles.#{path}.scss", **options)
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/vite_helper.rb', line 32

def universal_stylesheet_link_tag(path, **options)
  return stylesheet_link_tag(path, **options) unless vite_enabled?

  if Rails.env.test? && config.asset_host
    # Link directly to Vite server when running tests because for unit and integration tests, there
    # won't be a Rails server to proxy these requests to the Vite server.
    options[:host] = URI::HTTP.build(host: ViteRuby.config.host, port: ViteRuby.config.port).to_s
  end

  options[:extname] = false

  stylesheet_link_tag(
    ViteRuby.instance.manifest.path_for("stylesheets/styles.#{path}.scss", type: :stylesheet),
    **options
  )
end

#vite_enabled?Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
# File 'app/helpers/vite_helper.rb', line 4

def vite_enabled?
  # vite is not production ready yet
  return false if Rails.env.production?

  Gitlab::Utils.to_boolean(ViteRuby.env['VITE_ENABLED'], default: false)
end

#vite_hmr_http_urlObject



15
16
17
# File 'app/helpers/vite_helper.rb', line 15

def vite_hmr_http_url
  ViteRuby.env['VITE_HMR_HTTP_URL']
end

#vite_hmr_websocket_urlObject



11
12
13
# File 'app/helpers/vite_helper.rb', line 11

def vite_hmr_websocket_url
  ViteRuby.env['VITE_HMR_WS_URL']
end

#vite_page_entrypoint_pathsObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/vite_helper.rb', line 19

def vite_page_entrypoint_paths
  action = case controller.action_name
           when 'create' then 'new'
           when 'update' then 'edit'
           else controller.action_name
           end

  parts = (controller.controller_path.split('/') << action)

  parts.map
       .with_index { |part, idx| "pages.#{(parts[0, idx] << part).join('.')}.js" }
end