module ViteRails::Installation
Internal: Extends the base installation script from Vite Ruby to work for a typical Rails app.
Constants
- RAILS_TEMPLATES
Public Instance Methods
install_sample_files()
click to toggle source
Override: Create a sample JS file and attempt to inject it in an HTML template.
# File lib/vite_rails/installation.rb, line 39 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_files()
click to toggle source
Override: Setup a typical apps/web Hanami app to use Vite.
# File lib/vite_rails/installation.rb, line 11 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)
click to toggle source
Internal: Configure CSP rules that allow to load @vite/client correctly.
# File lib/vite_rails/installation.rb, line 21 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 changes in development # policy.script_src *policy.script_src, :unsafe_eval, "http://\#{ ViteRuby.config.host_with_port }" if Rails.env.development? CSP end