Skip to content

.vue?repro with exclude: /\?repro/ works on dev but fails on build (How to use custom query for .vue file?) #677

@hi-ogawa

Description

@hi-ogawa

Related plugins

Describe the bug

With the following plugin:

// [vite.config.ts]
export default defineConfig({
  plugins: [
    vue({
      exclude: /\?repro/,
    }),
    {
      name: 'repro',
      load(id) {
        if (id.endsWith('?repro')) {
          return `export default "ok"`;
        }
      },
    },
  ],
});

the following code works during dev but fails on build:

// [main.js]
import repro from './App.vue?repro';
document.querySelector('#app').textContent = repro;

Reproduction

https://stackblitz.com/edit/vitejs-vite-ab4rvgly?file=vite.config.ts

Steps to reproduce

❯ vite build
vite v7.1.9 building for production...
✓ 3 modules transformed.
✗ Build failed in 154ms
error during build:
[vite:vue] [plugin vite:vue] src/App.vue?repro: At least one <template> or <script> is required in a single file component. /home/projects/vitejs-vite-ab4rvgly/src/App.vue
file: /home/projects/vitejs-vite-ab4rvgly/src/App.vue?repro
SyntaxError: [plugin vite:vue] src/App.vue?repro: At least one <template> or <script> is required in a single file component. /home/projects/vitejs-vite-ab4rvgly/src/App.vue
    at Object.parse$1 [as parse] (file:///home/projects/vitejs-vite-ab4rvgly/node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js#cjs:1871:7)
    at createDescriptor (file:///home/projects/vitejs-vite-ab4rvgly/node_modules/@vitejs/plugin-vue/dist/index.js:96:42)
    at transformMain (file:///home/projects/vitejs-vite-ab4rvgly/node_modules/@vitejs/plugin-vue/dist/index.js:1650:33)
    at Object.handler (file:///home/projects/vitejs-vite-ab4rvgly/node_modules/@vitejs/plugin-vue/dist/index.js:2062:27)
    at Object.handler (file:///home/projects/vitejs-vite-ab4rvgly/node_modules/vite/dist/node/chunks/dep-Chhhsdoe.js:34186:13)
    at eval (file:///home/projects/vitejs-vite-ab4rvgly/node_modules/rollup/dist/es/shared/node-entry.js:22451:40)
    at https://vitejsviteab4rvgly-djvk.w-credentialless-staticblitz.com/blitz.96435430.js:31:29397

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: 20.19.1 - /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:
    @vitejs/plugin-vue: ^6.0.1 => 6.0.1 
    vite: ^7.1.7 => 7.1.9

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions