Skip to content

incorrect asset path replacement when the path is passed to typeof and using relative base #22304

Description

@sapphi-red

Describe the bug

When the asset path is passed to typeof and using relative base, Vite replaces the value incorrectly due to the operator precedence.

For example,

if (typeof "VITE_ASSET_HASH" === "string") {
  console.log("is string")
}

is replaced with

if (typeof ""+new URL("./foo.png", import.meta.url)+"" === "string") {
  console.log("is string")
}

I guess this was a bug that existed before v8, but started to happen more because of the Rolldown's optimization.

Reproduction

https://stackblitz.com/edit/vitejs-vite-39hbtupj?file=src%2Fmain.js,vite.config.js&terminal=dev

Steps to reproduce

In the build output you can see

`string` + new URL(`hero-CLDdwZDr.png`,import.meta.url).href == `string` && console.log(n)

but this should be

typeof new URL(`hero-CLDdwZDr.png`,import.meta.url).href == `string` && console.log(n)

System Info

StackBlitz

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 22.22.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^8.0.9 => 8.0.9

Used Package Manager

npm

Logs

No response

Validations

Activity

  1. added theissue type on Apr 23, 2026
  2. mvanhorn commented on Apr 26, 2026

    @mvanhorn

    Submitted #22330 - the fix expands the rewrite range to include the surrounding matching quotes and emits a parenthesized (""+runtime+"") so a leading typeof (or other unary operator) binds across the whole concatenated expression. Same change applied to the publicAssetUrlRE branch.

  3. sapphi-red commented on Aug 21, 2026

    @sapphi-red
    MemberAuthor

    Fixed in #22888, #22894.

  4. locked and limited conversation to collaborators on Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-downstream-blockerBlocking the downstream ecosystem to work properly (priority)

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions