vite-plugin-vue icon indicating copy to clipboard operation
vite-plugin-vue copied to clipboard

transformAssetUrls should transform to imports with `?url` query param set

Open brc-dd opened this issue 9 months ago • 1 comments

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 - https://github.com/vuejs/vitepress/discussions/4619#discussioncomment-12472148

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

brc-dd avatar Mar 12 '25 09:03 brc-dd

a workaround

export default defineConfig({
  plugins: [
    vue(),
  ],
  build:{
    assetsInclude: ['**/*.json'] // Treat the JSON file as a resource file
  }
});

edison1105 avatar Mar 17 '25 02:03 edison1105