Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-imports for known vue symbols #15

Open
3 tasks
phoenix-ru opened this issue Feb 16, 2024 · 0 comments
Open
3 tasks

Auto-imports for known vue symbols #15

phoenix-ru opened this issue Feb 16, 2024 · 0 comments
Labels
experimental An experimental feature not available in official compiler

Comments

@phoenix-ru
Copy link
Owner

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:

  • Detect stray known symbols usage. This can happen during regular transform pass;
  • Inject non-prefixed imports (e.g. import { ref } from 'vue' instead of ref as _ref);
  • Respect user-defined imports (e.g. import { ref } from './custom-path' should not auto-import).
@phoenix-ru phoenix-ru added the experimental An experimental feature not available in official compiler label Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental An experimental feature not available in official compiler
Projects
None yet
Development

No branches or pull requests

1 participant