fix(vite-plugin): properly remove css file from bundle when file is below inlineThreshold#299
fix(vite-plugin): properly remove css file from bundle when file is below inlineThreshold#299mywill wants to merge 2 commits into
Conversation
…elow inlineThreshold
WalkthroughThe pull request fixes a bug where CSS files were not fully removed when Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/vite-plugin-beasties/src/index.ts`:
- Around line 86-89: The current condition in
beastiesInstance.checkInlineThreshold uses "if (isStyleInlined ||
!sheet.length)" which causes assets to be deleted when the stylesheet is empty
even if originalCheckInline did not inline it; change the guard to only check
the inlining result by replacing that condition with "if (isStyleInlined)" so
that deletion logic runs only when originalCheckInline returned true (refer to
beastiesInstance.checkInlineThreshold and originalCheckInline in your diff).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1da23d58-42a9-486f-84d8-341dcc176422
📒 Files selected for processing (2)
packages/vite-plugin-beasties/src/index.tspackages/vite-plugin-beasties/test/index.test.ts
| beastiesInstance.checkInlineThreshold = function checkInlineThreshold(link, style, sheet) { | ||
| const isStyleInlined = originalCheckInline(link, style, sheet) | ||
|
|
||
| if (isStyleInlined || !sheetInverse.length) { | ||
| if (isStyleInlined || !sheet.length) { |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Inspect where and how checkInlineThreshold is called, and whether
# an empty `sheet` can occur without full inlining/link removal.
rg -n -C6 '\bcheckInlineThreshold\s*\(' packages/beasties/src/index.ts packages/vite-plugin-beasties/src/index.ts
rg -n -C6 '\binlineThreshold\b|\blink\.remove\s*\(' packages/beasties/src/index.tsRepository: danielroe/beasties
Length of output: 3484
Condition at line 89 is unsafe and causes broken references when stylesheets are empty but not inlined.
The || !sheet.length clause deletes bundle assets unconditionally when the sheet is empty, even when inlineThreshold did not trigger inlining. This leaves the HTML with a <link> tag pointing to a deleted asset.
The condition must be narrowed to:
if (isStyleInlined) {
Only delete the asset when the original checkInlineThreshold actually inlined the stylesheet (returned true).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/vite-plugin-beasties/src/index.ts` around lines 86 - 89, The current
condition in beastiesInstance.checkInlineThreshold uses "if (isStyleInlined ||
!sheet.length)" which causes assets to be deleted when the stylesheet is empty
even if originalCheckInline did not inline it; change the guard to only check
the inlining result by replacing that condition with "if (isStyleInlined)" so
that deletion logic runs only when originalCheckInline returned true (refer to
beastiesInstance.checkInlineThreshold and originalCheckInline in your diff).
There was a problem hiding this comment.
So i can look to change this but i was keeping the logic the same as that was not the issue i was fixing. I don't think this would delete anything important as the sheet length would have to be of length 0 which would be an empty resource so would be something that shouldn't get bundled anyway
There was a problem hiding this comment.
@mywill, you make a valid point. Looking more carefully at the diff, the || !sheet.length clause is pre-existing behavior — the old code had || !sheetInverse.length (checking the third parameter), and the new code has || !sheet.length (still checking the third parameter, just with the corrected name). This PR only renames the parameters to match the actual checkInlineThreshold(link, style, sheet) signature; the logic itself is unchanged.
Your reasoning is also sound: if sheet.length === 0, the stylesheet is empty, and removing an empty CSS asset from the bundle is a safe cleanup — there's nothing meaningful left to serve. My concern was overstated for this PR. I'll withdraw it.
( ˶ᵔ ᵕ ᵔ˶ )
Summary:
Solution:
Resolves: #298