Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FS-211325]: React Rails integration gem changes to support dynamic node switch #1

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
15 changes: 9 additions & 6 deletions vite_rails/lib/vite_rails/tag_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> tag for the specified Vite entrypoints.
Expand Down
10 changes: 7 additions & 3 deletions vite_ruby/lib/vite_ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -97,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)
Expand Down Expand Up @@ -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',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not possible for us to pass as a configuration or some input, to skip hard coding here ?

}.select { |_, value| value }
end

Expand Down
9 changes: 7 additions & 2 deletions vite_ruby/lib/vite_ruby/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ def run(argv, exec: false)
def command_for(args)
[config.to_env(env)].tap do |cmd|
exec_args, vite_args = args.partition { |arg| arg.start_with?('--node-options') }
cmd.push(*vite_executable(*exec_args))
cmd.push(*vite_args)
if(File.file?('.nvmrc'))
cmd.push('sh', '-c')
cmd.push("source ~/.nvm/nvm.sh && nvm use && #{vite_executable(*exec_args).join(' ')} #{vite_args.join(' ')}")
else
cmd.push(*vite_executable(*exec_args))
cmd.push(*vite_args)
end
cmd.push('--mode', config.mode) unless args.include?('--mode') || args.include?('-m')
end
end
Expand Down