Skip to content

fix: Allow new Vite v8 outputOptions callback shape#604

Open
raybrownco wants to merge 1 commit intoElMassimo:mainfrom
raybrownco:patch-2
Open

fix: Allow new Vite v8 outputOptions callback shape#604
raybrownco wants to merge 1 commit intoElMassimo:mainfrom
raybrownco:patch-2

Conversation

@raybrownco
Copy link
Copy Markdown
Contributor

Description 📖

Fix a build issue related to Vite 8's handling of the assetFileNames callback, which is manually typed in the outputFileName() function.

Background 📜

Vite v8 (which uses Rolldown) changed the object shape passed to the assetFileNames callback.

In Vite v7 (which uses Rollup), the callback received a PreRenderedAsset object with a singular name: string | undefined field.

In Vite v8, Vite's internal vite:css-post plugin calls the same assetFileNames function with a plural names: string[] field instead. This matches the Rolldown PreRenderedAsset interface. See: https://github.com/rolldown/rolldown/blob/a71934bf/packages/rolldown/src/options/output-options.ts#L66-L77

The Fix 🔨

First, we define a PreRenderedAsset interface that handles both shapes:

export interface PreRenderedAsset {
  name?: string // Vite v7, deprecated
  names?: string[] // Vite v8
  // ...
}

Then, determine which value to use within the outputFileName() function:

const outputFileName = (ext: string) => (asset: PreRenderedAsset) => {
    // Vite v8 uses `names`, earlier versions use `name`
    const resolvedName = asset.names?.[0] ?? asset.name ?? '[name]'
    const shortName = basename(resolvedName).split('.')[0]
    return posix.join(assetsDir, `${shortName}-[hash].${ext}`)
  }

Screenshots 📷

A Rails project with vite_rails (3.0.19) and vite_ruby (3.9.2) began to fail after updating vite from 7.3.2 to 8.0.10:

Screenshot 2026-04-23 at 11 42 10 AM

Vite v8 (which uses Rolldown) changed the object shape passed to the `assetFileNames` callback.

In Vite v7 (which uses Rollup), the callback received a `PreRenderedAsset` object with a singular `name: string | undefined` field.

In Vite v8, Vite's internal `vite:css-post` plugin calls the same `assetFileNames` function with a plural `names: string[]` field instead. This matches the Rolldown `PreRenderedAsset` interface. See: https://github.com/rolldown/rolldown/blob/a71934bf/packages/rolldown/src/options/output-options.ts#L66-L77
@raybrownco raybrownco marked this pull request as ready for review April 23, 2026 15:53
@raybrownco
Copy link
Copy Markdown
Contributor Author

@ElMassimo Ready for your review: this PR fixes a bug that we ran into when attempting to upgrade Vite from v7 to v8 in one of our Rails projects. Thanks in advance for your consideration! Let me know if you object to any of these changes - I'm more than happy to accept feedback.

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.

1 participant