diff --git a/.github/workflows/js.yml b/.github/workflows/js.yml index 1bc18a96..00834b69 100644 --- a/.github/workflows/js.yml +++ b/.github/workflows/js.yml @@ -18,7 +18,7 @@ jobs: - uses: pnpm/action-setup@v4 with: - version: 9 + version: 10 - uses: actions/setup-node@v4 with: diff --git a/Gemfile.lock b/Gemfile.lock index 9ee071d9..9fa131af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -101,7 +101,7 @@ GEM ast (2.4.2) base64 (0.3.0) benchmark-ips (2.14.0) - bigdecimal (4.1.0) + bigdecimal (4.1.2) builder (3.3.0) byebug (11.1.3) coderay (1.1.3) @@ -112,14 +112,14 @@ GEM docile (1.4.1) drb (2.2.3) dry-cli (1.4.1) - erb (6.0.2) + erb (6.0.4) erubi (1.13.1) globalid (1.2.1) activesupport (>= 6.1) i18n (1.14.8) concurrent-ruby (~> 1.0) io-console (0.8.2) - irb (1.17.0) + irb (1.18.0) pp (>= 0.6.0) prism (>= 1.3.0) rdoc (>= 4.0.0) @@ -161,7 +161,7 @@ GEM net-smtp (0.4.0) net-protocol nio4r (2.5.9) - nokogiri (1.19.2) + nokogiri (1.19.3) mini_portile2 (~> 2.8.2) racc (~> 1.4) parallel (1.26.3) @@ -182,10 +182,10 @@ GEM date stringio racc (1.8.1) - rack (3.2.5) + rack (3.2.6) rack-proxy (0.7.7) rack - rack-session (2.1.1) + rack-session (2.1.2) base64 (>= 0.1.0) rack (>= 3.0.0) rack-test (2.2.0) @@ -222,7 +222,7 @@ GEM thor (~> 1.0, >= 1.2.2) zeitwerk (~> 2.6) rainbow (3.1.1) - rake (13.3.1) + rake (13.4.2) rdoc (7.2.0) erb psych (>= 4.0.0) diff --git a/test/helper_test.rb b/test/helper_test.rb index 81ae21a2..8d8d8c11 100644 --- a/test/helper_test.rb +++ b/test/helper_test.rb @@ -42,6 +42,11 @@ def with_dev_server_running(&block) refresh_config(mode: "development") super end + + def with_skip_proxy_dev_server_running(&block) + refresh_config(mode: "development", skip_proxy: true) + ViteRuby.instance.stub(:dev_server_running?, true, &block) + end end class LegacyHelperTest < HelperTestCase @@ -190,6 +195,88 @@ def test_vite_image_tag } end + # skipProxy tests: validate that all tag helpers emit absolute URLs pointing + # directly to the Vite dev server when skipProxy is enabled. + + def test_vite_client_tag_with_skip_proxy + assert_nil vite_client_tag + with_skip_proxy_dev_server_running { + origin = ViteRuby.config.origin + + assert_equal %(), vite_client_tag + } + end + + def test_vite_asset_path_with_skip_proxy + with_skip_proxy_dev_server_running { + origin = ViteRuby.config.origin + + assert_equal "#{origin}/vite-dev/entrypoints/main.ts", vite_asset_path("main.ts") + assert_equal "#{origin}/vite-dev/entrypoints/app.css", vite_asset_path("app.css") + assert_equal "#{origin}/vite-dev/images/logo.png", vite_asset_path("images/logo.png") + } + end + + def test_vite_javascript_tag_with_skip_proxy + with_skip_proxy_dev_server_running { + origin = ViteRuby.config.origin + + assert_equal %(), + vite_typescript_tag("main") + + assert_equal %(), + vite_javascript_tag("entrypoints/frameworks/vue") + } + end + + def test_vite_stylesheet_tag_with_skip_proxy + with_skip_proxy_dev_server_running { + origin = ViteRuby.config.origin + + assert_similar link(href: "#{origin}/vite-dev/entrypoints/app.css"), vite_stylesheet_tag("app") + assert_equal vite_stylesheet_tag("app"), vite_stylesheet_tag("app.css") + + if Rails::VERSION::MAJOR >= 7 + assert_similar link(href: "#{origin}/vite-dev/entrypoints/sassy.scss"), vite_stylesheet_tag("sassy.scss") + else + # Rails 6 appends .css to non-.css extensions. Without the proxy to + # normalize .scss.css → .scss, Vite cannot serve this URL. + assert_similar link(href: "#{origin}/vite-dev/entrypoints/sassy.scss.css"), vite_stylesheet_tag("sassy.scss") + end + } + end + + def test_vite_image_tag_with_skip_proxy + with_skip_proxy_dev_server_running { + origin = ViteRuby.config.origin + + assert_equal %(Logo), + vite_image_tag("images/logo.png", alt: "Logo") + + assert_equal %(Logo), + vite_image_tag("images/logo.png", srcset: {"images/logo-2x.png" => "2x"}, alt: "Logo") + } + end + + def test_vite_react_refresh_tag_with_skip_proxy + with_skip_proxy_dev_server_running { + origin = ViteRuby.config.origin + + assert_equal <<~HTML.chomp, vite_react_refresh_tag(nonce: nil) + + HTML + } + end + def test_vite_picture_tag if Rails.gem_version >= Gem::Version.new("7.1.0") assert_equal <<~HTML.gsub(/\n\s*/, ""), vite_picture_tag("images/logo.svg", "images/logo.png", class: "test", image: {alt: "Logo"}) diff --git a/test/manifest_test.rb b/test/manifest_test.rb index ddda3af5..516196b6 100644 --- a/test/manifest_test.rb +++ b/test/manifest_test.rb @@ -195,6 +195,74 @@ def test_vite_client_src } end + # NOTE: skipProxy (experimental since v3.2.12) causes asset URLs to point + # directly to the Vite dev server. Known caveats: + # + # 1. CORS: Browser makes cross-origin requests to Vite. Vite sets permissive + # CORS headers by default, but custom middleware may interfere. + # 2. Rails 6 .scss.css: Without the proxy to normalize .scss.css → .scss, + # Rails 6's stylesheet_link_tag produces URLs Vite can't serve. + # 3. Cookies: Asset requests to a different origin won't carry same-origin + # cookies. Usually not an issue since assets don't require auth. + # 4. SSL: Both Rails and Vite need valid certs when using HTTPS. + # 5. Docker/VM: "localhost:3036" from the browser may not reach Vite inside + # a container. Must configure host to a reachable address. + # 6. vite_asset_url may produce double-origin URLs since path_for already + # returns an absolute URL when skipProxy is enabled. + + def test_lookup_success_with_skip_proxy_and_dev_server_running + refresh_config(mode: "development", skip_proxy: true) + with_dev_server_running { + origin = ViteRuby.config.origin # "https://localhost:3535" + + entry = {"file" => "#{origin}/vite-dev/entrypoints/application.js"} + + assert_equal entry, lookup!("application.js", type: :javascript) + assert_equal entry, lookup!("entrypoints/application.js") + + assert_equal "#{origin}/vite-dev/entrypoints/application.ts", + path_for("application", type: :typescript) + + assert_equal "#{origin}/vite-dev/entrypoints/styles.css", + path_for("styles", type: :stylesheet) + + assert_equal "#{origin}/vite-dev/image/logo.png", + path_for("image/logo.png") + + assert_equal "#{origin}/vite-dev/logo.png", + path_for("~/logo.png") + + assert_equal "#{origin}/vite-dev/@fs#{ViteRuby.config.root}/app/assets/theme.css", + path_for("/app/assets/theme", type: :stylesheet) + } + end + + def test_vite_client_src_with_skip_proxy + refresh_config(mode: "development", skip_proxy: true) + + assert_nil vite_client_src + + with_dev_server_running { + assert_equal "#{ViteRuby.config.origin}/vite-dev/@vite/client", vite_client_src + } + + # Origin from skip_proxy takes precedence over asset_host + refresh_config(asset_host: "http://example.com", mode: "development", skip_proxy: true) + + with_dev_server_running { + assert_equal "#{ViteRuby.config.origin}/vite-dev/@vite/client", vite_client_src + } + end + + def test_skip_proxy_has_no_effect_without_dev_server + refresh_config(skip_proxy: true) + + # Production paths are unchanged — skipProxy only matters when dev server runs + assert_equal prefixed("main.9dcad042.js"), path_for("main", type: :typescript) + assert_equal prefixed("app.517bf154.css"), path_for("app", type: :stylesheet) + assert_equal prefixed("logo.f42fb7ea.png"), path_for("images/logo.png") + end + def test_lookup_nil assert_nil lookup("foo.js") end