Open
Description
Describe the bug
I want to make some updates to the code after hot update, but @vitejs/plugin-vue still reads the original code, causing the scoped index to not find the corresponding style
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
myPlugin() as any,
],
});
function myPlugin() {
return {
name: 'myplugin',
enforce: 'pre',
transform(code, id) {
if (!/\.vue$/.test(id))
return
const css = `${code}\n<style scoped>div{color:red;}</style>`
if (css) {
return {
code: css,
map: null,
}
}
},
handleHotUpdate(ctx) {
const read = ctx.read
if (/\.vue$/.test(ctx.file)) {
ctx.read = async () => {
const code = await read()
return `${code}\n<style scoped>div{color:red;}</style>`
}
}
},
}
}
Reproduction
https://stackblitz.com/edit/vitejs-vite-nmyrdmto?file=vite.config.ts,src%2FApp.vue
Steps to reproduce
npm run dev
update class and save
System Info
System:
OS: macOS 14.5
CPU: (10) arm64 Apple M1 Pro
Memory: 136.30 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.16.0 - ~/.local/state/fnm_multishells/31127_1737095175498/bin/node
Yarn: 1.22.22 - ~/.local/state/fnm_multishells/31127_1737095175498/bin/yarn
npm: 10.8.1 - ~/.local/state/fnm_multishells/31127_1737095175498/bin/npm
pnpm: 9.6.0 - ~/.local/state/fnm_multishells/31127_1737095175498/bin/pnpm
bun: 1.1.21 - ~/.bun/bin/bun
Watchman: 2024.09.09.00 - /opt/homebrew/bin/watchman
Browsers:
Chrome: 131.0.6778.266
Safari: 17.5
npmPackages:
@vitejs/plugin-vue: ^5.2.1 => 5.2.1
vite: ^6.0.7 => 6.0.7
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.