Skip to content

transformAssetUrls should transform to imports with ?url query param set #544

Open
@brc-dd

Description

@brc-dd

Related plugins

Describe the bug

    template: {
      transformAssetUrls: {
        video: ['src', 'poster'],
        source: ['src'],
        img: ['src'],
        image: ['xlink:href', 'href'],
        use: ['xlink:href', 'href'],
        Foo: ['src'],
      },
    },
<script setup lang="ts">
import Foo from './Foo.vue'
</script>

<template>
  <Foo src="./foo.json" />
</template>

Foo.vue:

<script setup lang="ts">
defineProps<{
  src: string;
}>();
</script>

<template>
  {{ src }}
</template>

On dev this shows:

/foo.json

On build this shows:

{ "foo": "bar" }

Which is wrong because it's being transformed to something like this:

import src from './foo.json'

<Foo :src="src" />

While it should be transformed to this: (depending on whether any conflicting query parameter is already set)

import src from './foo.json?url'

<Foo :src="src" />

So that src remains fetch-able.

On build it should something like this (depending on build.assetsInlineLimit):

data:application/json;base64,ewogICJmb28iOiAiYmFyIgp9Cg==

Downstream issue - vuejs/vitepress#4619 (reply in thread)

Reproduction

https://stackblitz.com/edit/vitejs-vite-khtsiaws?file=src/App.vue&terminal=dev

Steps to reproduce

Run vite build && vite preview in above playground.

System Info

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: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-vue: ^5.2.1 => 5.2.1 
    vite: ^6.2.1 => 6.2.1

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions