vitejs / vite-plugin-vue

Vite Vue Plugins
MIT License
495 stars 153 forks source link

Code generated for empty <script setup> does not define `__name` #338

Open passionate-bram opened 10 months ago

passionate-bram commented 10 months ago

Related plugins

Describe the bug

When you create a component with reactivity the component will have a field __name that basically boils down to filename.match(/([^/\\]+)\.\w+$/ (relevant source from vuejs/vue).

But I noticed that if the <script setup> is empty or missing then the __name field is absent. However, the other meta fields __file are always present.

Understanding this is entirely internal behaviour, the inconsistency is annoying and leaves a gap that could otherwise still be useful to developers (specifically devtools and other meta tools).

As immediate remedy the __name field could be set whereever __file is provided already.

Reproduction

https://stackblitz.com/edit/vitejs-vite-6zx8jk?file=src%2FApp.vue

Steps to reproduce

  1. Have a component with an empty <script setup> section, and no <script> section.
  2. Print out the __name and __file fields of the component.
  3. Notice that __file is provided, but __name is not.

System Info

System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 534.39 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
    pnpm: 8.10.2 - /opt/homebrew/bin/pnpm
  Browsers:
    Brave Browser: 120.1.61.109
    Safari: 17.2.1
  npmPackages:
    @vitejs/plugin-vue: ^4.5.0 => 4.5.0 
    vite: ^5.0.0 => 5.0.2

Used Package Manager

npm

Logs

No response

Validations

edison1105 commented 1 month ago

the value of __name is generate from https://github.com/vuejs/core/blob/40193696b3564202173ac0367e4b3ae48c4ffb84/packages/compiler-sfc/src/compileScript.ts#L946 it requires a non-empty script.