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

Bug: @vitejs/plugin-vue not read updated code

Open Simon-He95 opened this issue 11 months ago • 1 comments

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

Simon-He95 avatar Jan 17 '25 06:01 Simon-He95

Side note: It worked before v4.3.1. It stopped working after PR #227.

edison1105 avatar Jan 20 '25 07:01 edison1105