phoenix-ru / fervid

All-in-One Vue compiler written in Rust
https://phoenix-ru.github.io/fervid/
Apache License 2.0
371 stars 9 forks source link

Auto-imports for known `vue` symbols #15

Open phoenix-ru opened 7 months ago

phoenix-ru commented 7 months ago

Writing SFCs using the default JS compiler and https://github.com/unplugin/unplugin-auto-import produces not-so-optimal code.

Take this for example (playground):

<script setup>
const msg = ref('Hello World!')
</script>

<template>
  <h1>{{ msg }}</h1>
  <input v-model="msg" />
</template>

Correct binding type of msg cannot be properly inferred, which leads to compiler generating unref(msg), which in turn has runtime cost. In addition to that, extra build step is needed to properly auto-import the ref.

A better approach would be to add a ref/computed/etc. vue-specific imports whenever their usage is detected. It is much cheaper and more beneficial to do so in the SFC compiler than in any other tool.

Steps: