antfu / unplugin-auto-import

Auto import APIs on-demand for Vite, Webpack and Rollup

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReferenceError in template literal

replygirl opened this issue · comments

Found something 🧐

<script setup lang="ts">
const _foo = autoImportedFn()
const foo = `foo-${_foo}` // works

const bar = `bar-${autoImportedFn()}` // ReferenceError
</script>

I can see why it happens, but in case you didn't notice before

Vitesse reproduction

pnpm i nanoid
// /vite.config.ts
...

export default defineConfig({
  ...
  plugins: [
    ...
    AutoImport({
      imports: [
        ...
        {
          nanoid: ['nanoid'],
        },
      ],
    }),
    ...
  ],
  ...
})
// /src/components/Footer.vue
<script setup lang="ts">
...
const _foo = nanoid()
const foo = `foo-${_foo}`
const bar = `bar-${nanoid()}`
</script>

<template>
  <span>{{ foo }}, {{ bar }}</span>
</template>

🙌