Module: ViteRails::CLI::Install

Defined in:
lib/vite_rails/cli.rb

Overview

Internal: Extends the base installation script from Vite Ruby to work for a typical Rails app.

Constant Summary collapse

RAILS_TEMPLATES =
Pathname.new(File.expand_path('../../templates', __dir__))

Instance Method Summary collapse

Instance Method Details

#install_sample_filesObject

Override: Create a sample JS file and attempt to inject it in an HTML template.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vite_rails/cli.rb', line 60

def install_sample_files
  unless config.resolved_entrypoints_dir.join('application.js').exist?
    cp RAILS_TEMPLATES.join('entrypoints/application.js'), config.resolved_entrypoints_dir.join('application.js')
  end

  if (layout_file = root.join('app/views/layouts/application.html.erb')).exist?
    inject_line_before layout_file, '</head>', <<-HTML
  <%= vite_client_tag %>
  <%= vite_javascript_tag 'application' %>
  <!--
    If using a TypeScript entrypoint file:
      vite_typescript_tag 'application'

    If using a .jsx or .tsx entrypoint, add the extension:
      vite_javascript_tag 'application.jsx'

    Visit the guide for more information: https://vite-ruby.netlify.app/guide/rails
  -->
    HTML
  end
end

#setup_app_filesObject

Override: Setup a typical apps/web Hanami app to use Vite.



28
29
30
31
32
33
34
35
# File 'lib/vite_rails/cli.rb', line 28

def setup_app_files
  cp RAILS_TEMPLATES.join('config/rails-vite.json'), config.config_path
  if dir = %w[app/javascript app/packs].find { |path| root.join(path).exist? }
    replace_first_line config.config_path, 'app/frontend', %(    "sourceCodeDir": "#{ dir }",)
  end
  setup_content_security_policy root.join('config/initializers/content_security_policy.rb')
  append root.join('Procfile.dev'), 'web: bin/rails s'
end

#setup_content_security_policy(csp_file) ⇒ Object

Internal: Configure CSP rules that allow to load @vite/client correctly.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vite_rails/cli.rb', line 38

def setup_content_security_policy(csp_file)
  return unless csp_file.exist?

  inject_line_after csp_file, 'policy.script_src', <<~CSP
        # You may need to enable this in production as well depending on your setup.
    #    policy.script_src *policy.script_src, :blob if Rails.env.test?
  CSP
  inject_line_after csp_file, 'policy.connect_src', <<~CSP
        # Allow @vite/client to hot reload changes in development
    #    policy.connect_src *policy.connect_src, "ws://\#{ ViteRuby.config.host_with_port }" if Rails.env.development?
  CSP
  inject_line_after csp_file, 'policy.script_src', <<~CSP
        # Allow @vite/client to hot reload javascript changes in development
    #    policy.script_src *policy.script_src, :unsafe_eval, "http://\#{ ViteRuby.config.host_with_port }" if Rails.env.development?
  CSP
  inject_line_after csp_file, 'policy.style_src', <<~CSP
        # Allow @vite/client to hot reload style changes in development
    #    policy.style_src *policy.style_src, :unsafe_inline if Rails.env.development?
  CSP
end