Repository navigation
Description
Environment
System:
OS: Linux 6.6 Arch Linux
CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
Memory: 54.02 GB / 62.74 GB
Container: Yes
Shell: 5.9 - /usr/bin/zsh
Binaries:
Node: 23.4.0 - /usr/bin/node
pnpm: 9.15.1 - /usr/bin/pnpm
npmPackages:
unimport: ^3.14.6 => 3.14.6
Reproduction
import { createUnimport } from "unimport";
const id = "/path/to/example.vue";
const code = `<script>
import { ref } from 'vue';
const count = ref(someImport);
</script>
<template>
<button @click="() => count++">{{ count }}</button>
</template>
`;
const unimport = createUnimport({
imports: [{ from: "example", name: "someImport" }],
});
const res = await unimport.injectImports(code, id);
console.log(res.code);Describe the bug
The import is added outside the script block (see logs below).
Now, at first I assumed it was up to the me to process the vue file and only transform the <script> block instead of transforming the whole file... but when I looked for examples of how to do that (in the included Vite plugin and unplugin-auto-imports source codes), I couldn't find anything that filtered down to the script block... So I think this is a bug? But it works for Nuxt and sometimes for WXT, so this is probably my fault lol. I'd appreciate being pointed in the right direction if I'm missing something obvious.
Additional context
Originally found here in WXT: wxt-dev/wxt#1356 (reply in thread)
Just for the sake of it, I also tried setting addons.vueTemplate: true, but this is the script block, not the template, so it didn't have any effect like I assumed.
Logs
$ node example.js
import { someImport } from 'example';
<script>
import { ref } from 'vue';
const count = ref(someImport);
</script>
<template>
<button @click="() => count++">{{ count }}</button>
</template>
Activity
aiktb commentedon Jan 20, 2025
This seems to be expected behavior
unimport/test/cases.test.ts
Line 84 in 07ab0d2
unimport/test/cases.test.ts
Line 102 in 07ab0d2
unimport/test/cases/positive/template-ref.vue
Line 6 in 07ab0d2
unimport/test/cases/output/default/template-ref.vue
Lines 1 to 2 in 07ab0d2
aklinker1 commentedon Jan 21, 2025
Hmm if that's the case, then how does the vite plugin work with Vue?
aklinker1 commentedon Feb 8, 2025
Fixed on my side in: wxt-dev/wxt#1412
Not a bug