Skip to content

fix: use emitted CSS in vite_stylesheet_tag for Rolldown CSS entries (close #615) - #634

Open
breken-ai wants to merge 1 commit into
ElMassimo:mainfrom
breken-ai:fix/stylesheet-tag-rolldown-css-wrapper
Open

breken-ai wants to merge 1 commit into
ElMassimo:mainfrom
breken-ai:fix/stylesheet-tag-rolldown-css-wrapper

Conversation

@breken-ai

Copy link
Copy Markdown

Description 📖

This pull request fixes #615. vite_stylesheet_tag now links the CSS that Vite emitted for a CSS-only entrypoint, including the Vite 8 / Rolldown case where the manifest file for that entry is a JavaScript wrapper.

Background 📜

This was happening because vite_stylesheet_tag used path_for, which always returns the entry's file. For some CSS-only entrypoints Rolldown writes a .js wrapper to file and puts the real stylesheet in css[], like this:

"entrypoints/print.css": {
  "file": "assets/print.4b6e2a1f.js",
  "src": "entrypoints/print.css",
  "isEntry": true,
  "css": ["assets/print.9c0d7e35.css"]
}

So in production the helper rendered <link rel="stylesheet" href="/vite/assets/print.4b6e2a1f.js"> and the page lost its styles. Development is not affected, because the dev server serves the source path directly.

The Fix 🔨

By changing vite_stylesheet_tag to resolve each name with resolve_entries(name, type: :stylesheet), which is the same approach as the workaround in the issue:

  • when file is a .css file, it is kept (normal CSS entries render exactly as before);
  • the css[] paths are added and deduplicated;
  • when there are no css[] paths (always the case while the dev server runs), file is used, so dev paths such as entrypoints/sassy.scss are unchanged.

Tests:

  • New test_vite_stylesheet_tag_with_javascript_wrapper_entry in test/helper_test.rb, with a wrapper entry added to the manifest-assets.json fixture. On main it fails with href="/vite-production/assets/print.4b6e2a1f.js"; with this change it renders print.9c0d7e35.css, and the dev-server path stays /vite-dev/entrypoints/print.css.
  • bin/rake test (Ruby 4.0.3, Rails 8.1 from the root Gemfile): 121 tests, 0 failures (120 tests, 0 failures on main).
  • rubocop on the changed Ruby files: no offenses. git diff --check: clean.
  • I did not run the Rails 7.2 / 8.0 gemfiles locally; CI covers those.

An AI coding agent (Claude Code, run by breken-ai) found this bug and wrote this change; I checked the red/green test above before opening the PR.

Screenshots 📷

Not applicable: this changes the generated stylesheet URL and has no visual UI.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSS-only entrypoint sometimes resolves to a .js file in the manifest under Vite 8 / Rolldown

1 participant