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

Imports added outside Vue <script> block #419

Open
aklinker1 opened this issue Jan 20, 2025 · 2 comments
Open

Imports added outside Vue <script> block #419

aklinker1 opened this issue Jan 20, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@aklinker1
Copy link

aklinker1 commented Jan 20, 2025

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

https://github.com/aklinker1/unimport-vue-bug

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>
@aklinker1 aklinker1 added the bug Something isn't working label Jan 20, 2025
@aiktb
Copy link

aiktb commented Jan 20, 2025

This seems to be expected behavior

const positives = import.meta.glob('./cases/positive/*', { as: 'raw' })

resolve(cwd, file, '../..', 'output', name, basename(file)),

const divRef = ref(null)

import { ref } from 'vue';
<template>

@aklinker1
Copy link
Author

aklinker1 commented Jan 21, 2025

Hmm if that's the case, then how does the vite plugin work with Vue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants