Skip to content

fix(vite-plugin): properly remove css file from bundle when file is below inlineThreshold#299

Open
mywill wants to merge 2 commits into
danielroe:mainfrom
mywill:fix/vite-plugin-inline-threshold
Open

fix(vite-plugin): properly remove css file from bundle when file is below inlineThreshold#299
mywill wants to merge 2 commits into
danielroe:mainfrom
mywill:fix/vite-plugin-inline-threshold

Conversation

@mywill

@mywill mywill commented Apr 13, 2026

Copy link
Copy Markdown

Summary:

  • checkInlineThreshold in the vite-plugin-beasties has a parameter mismatch causing an error when the inlineThreshold is set preventing css from inlining

Solution:

  • added a test showing the issue
  • Updated the parameters for checkInlineThreshold to fix the mismatch

Resolves: #298

@coderabbitai

coderabbitai Bot commented Apr 13, 2026

Copy link
Copy Markdown

Walkthrough

The pull request fixes a bug where CSS files were not fully removed when inlineThreshold was configured and the CSS file size was below the threshold. The fix updates the checkInlineThreshold wrapper method signature from (style, before, sheetInverse) to (link, style, sheet) and corrects the emptiness check from !sheetInverse.length to !sheet.length.

Changes

Cohort / File(s) Summary
Plugin implementation
packages/vite-plugin-beasties/src/index.ts
Updated beastiesInstance.checkInlineThreshold wrapper to accept the correct parameter signature (link, style, sheet) and changed the conditional logic from !sheetInverse.length to !sheet.length to properly determine when to remove inlined CSS assets from the bundle.
Test coverage
packages/vite-plugin-beasties/test/index.test.ts
Added test case verifying that CSS files below inlineThreshold are fully inlined into HTML and no external stylesheet remains in the bundle. Validates presence of inline <style> block with CSS rules and absence of <link rel="stylesheet"> tags.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • danielroe
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main fix: correcting parameter mismatch in checkInlineThreshold to properly remove CSS files when below the inlineThreshold.
Description check ✅ Passed The description clearly relates to the changeset, explaining the parameter mismatch issue, the test addition, and the parameter fix applied.
Linked Issues check ✅ Passed The PR successfully addresses issue #298 by fixing the parameter mismatch in checkInlineThreshold (from (style, before, sheetInverse) to (link, style, sheet)) and adds a test case verifying CSS inlining and file removal work correctly.
Out of Scope Changes check ✅ Passed All changes are directly scoped to resolving the linked issue: parameter fix in index.ts and test case addition in index.test.ts with no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between b3254e6 and 3cc634d.

📒 Files selected for processing (2)
  • packages/vite-plugin-beasties/src/index.ts
  • packages/vite-plugin-beasties/test/index.test.ts

Comment on lines +86 to +89
beastiesInstance.checkInlineThreshold = function checkInlineThreshold(link, style, sheet) {
const isStyleInlined = originalCheckInline(link, style, sheet)

if (isStyleInlined || !sheetInverse.length) {
if (isStyleInlined || !sheet.length) {

@coderabbitai coderabbitai Bot Apr 13, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 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.ts

Repository: 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).

@mywill mywill Apr 13, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@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.

( ˶ᵔ ᵕ ᵔ˶ )

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.

Bug: Vite plugin Css is not fully removed when inlineThreshold is set and the css is under the threshold

1 participant