From d8278f3a48b6e7a2e5278994005f0901bcfc09a5 Mon Sep 17 00:00:00 2001 From: Vishwasraj Date: Sun, 1 Sep 2024 01:10:00 +0530 Subject: [PATCH 1/6] Fix to make react + rails work and have complete setup inside frontend folder --- vite_ruby/lib/vite_ruby/config.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vite_ruby/lib/vite_ruby/config.rb b/vite_ruby/lib/vite_ruby/config.rb index 7ef4afc0..c4795ad8 100644 --- a/vite_ruby/lib/vite_ruby/config.rb +++ b/vite_ruby/lib/vite_ruby/config.rb @@ -38,8 +38,11 @@ def manifest_paths end # Public: The directory where Vite will store the built assets. + # this can also be avoided if we can configure rails to server frontend public path for local def build_output_dir - root.join(public_dir, public_output_dir) + # as the final build is present in public folder, rails need to lookup manifest file from here + # as root will not point upto to frontend, the path will be frontend/public which will result in 404 + Pathname.new(Dir.pwd).join(public_dir, public_output_dir) end # Public: The directory where the entries are located. @@ -145,12 +148,13 @@ def resolve_config(**attrs) } # Internal: Default values for a Ruby application. + # Not able to load env via config/vite.rb or override via ViteRuby.configure, need to check to avoid this change def config_defaults(asset_host: nil, mode: ENV.fetch('RACK_ENV', 'development'), root: Dir.pwd) { 'asset_host' => option_from_env('asset_host') || asset_host, 'config_path' => option_from_env('config_path') || DEFAULT_CONFIG.fetch('config_path'), 'mode' => option_from_env('mode') || mode, - 'root' => option_from_env('root') || root, + 'root' => option_from_env('root') || Dir.pwd + '/frontend-react', }.select { |_, value| value } end From 81cbd7024c5fa7306195f14b1e2c540afd502116 Mon Sep 17 00:00:00 2001 From: Vishwasraj Date: Tue, 3 Sep 2024 16:50:46 +0530 Subject: [PATCH 2/6] Prevented unwanted rebuild if no file changes --- vite_ruby/lib/vite_ruby/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vite_ruby/lib/vite_ruby/config.rb b/vite_ruby/lib/vite_ruby/config.rb index c4795ad8..c37f7bf7 100644 --- a/vite_ruby/lib/vite_ruby/config.rb +++ b/vite_ruby/lib/vite_ruby/config.rb @@ -100,7 +100,7 @@ def coerce_values(config) config['mode'] = config['mode'].to_s config['port'] = config['port'].to_i config['root'] = root = Pathname.new(config['root']) - config['build_cache_dir'] = root.join(config['build_cache_dir']) + config['build_cache_dir'] = Pathname.new(Dir.pwd).join(config['build_cache_dir']) config['ssr_output_dir'] = root.join(config['ssr_output_dir']) coerce_booleans(config, 'auto_build', 'hide_build_console_output', 'https', 'skip_compatibility_check', 'skip_proxy') config['package_manager'] ||= detect_package_manager(root) From 9599217dcc9abd01cb2ecd47964479046eb36c0a Mon Sep 17 00:00:00 2001 From: Vishwasraj Date: Wed, 4 Sep 2024 10:08:42 +0530 Subject: [PATCH 3/6] Manual tag building --- vite_rails/lib/vite_rails/tag_helpers.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/vite_rails/lib/vite_rails/tag_helpers.rb b/vite_rails/lib/vite_rails/tag_helpers.rb index f6f5e9cc..f4f88998 100644 --- a/vite_rails/lib/vite_rails/tag_helpers.rb +++ b/vite_rails/lib/vite_rails/tag_helpers.rb @@ -44,14 +44,17 @@ def vite_javascript_tag(*names, media: 'screen', **options) entries = vite_manifest.resolve_entries(*names, type: asset_type) - tags = javascript_include_tag(*entries.fetch(:scripts), crossorigin: crossorigin, type: type, extname: false, **options) - tags << vite_preload_tag(*entries.fetch(:imports), crossorigin: crossorigin, **options) unless skip_preload_tags - options[:extname] = false if Rails::VERSION::MAJOR >= 7 - - tags << stylesheet_link_tag(*entries.fetch(:stylesheets), media: media, **options) unless skip_style_tags + options[:extname] = false if Rails::VERSION::MAJOR >= 7 - tags + if(options[:skip_tags] == true) + entries + else + tags = javascript_include_tag(*entries.fetch(:scripts), crossorigin: crossorigin, type: type, extname: false, **options) + tags << vite_preload_tag(*entries.fetch(:imports), crossorigin: crossorigin, **options) unless skip_preload_tags + tags << stylesheet_link_tag(*entries.fetch(:stylesheets), media: media, **options) unless skip_style_tags + tags + end end # Public: Renders a