Skip to content

Imports added outside Vue <script> block #419

Closed
wxt-dev/wxt
#1412

Description

@aklinker1

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>

Activity

  1. added
    bug
    on Jan 20, 2025
  2. aiktb commented on Jan 20, 2025

    @aiktb

    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>

  3. aklinker1 commented on Jan 21, 2025

    @aklinker1
    Author

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

  4. aklinker1 commented on Feb 8, 2025

    @aklinker1
    Author

    Fixed on my side in: wxt-dev/wxt#1412

    Not a bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions