unplugin / unplugin/unplugin-auto-import

Declaration of files with suffixes is incorrect

Open
#458 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.8k
Forks
217
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

Hi, I'm facing an issue with the auto-import.d.ts file when generating files with the suffixes as: foo.composable.ts or bar.helper.ts, etc.

The problem is that the file removes the suffix, which causes an "any" type for my files, specifically within the declare global {} block. I've tried investigating the issue on my own but can't seem to understand the problem. Below, I'll list what I have attempted.

Problem:
image

What solved my problem was changing this line in the unplugin/unimport library:

function toTypeDeclarationItems(imports, options) {
  return imports.map((i) => {
+    const from = options?.resolvePath?.(i) || (i.typeFrom || i.from).replace(/\.[a-zA-Z]+$/, "");
-    const from = (options?.resolvePath?.(i) || i.typeFrom || i.from).replace(/\.[a-zA-Z]+$/, "");
    return `const ${i.as}: typeof import('${from}')${i.name !== "*" ? `['${i.name}']` : ""}`;
  }).sort();
}

However, when I forked the unplugin/unimport repository to create a pull request, it broke all tests hahaha. So, the issue might not be related to this change, or it could be happening due to how I am using the library.

So, I forked the unplugin-auto-import repository and tried to reproduce my problem. Surprisingly, everything worked well in the playground folder.

Lastly, I created a project on StackBlitz and another on my computer using bun create vite, and both exhibited the same problem I'm facing in my project.


To reproduce the error, run rm auto-import.d.ts; npm run build

Observe how the file is generated: foo.composable.ts is written as foo instead of foo.composable within the declare global {} block.

Reproduction

https://stackblitz.com/edit/vue3-vite-starter-srm5lm

System Info
System:
    OS: Linux 6.6 Arch Linux
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
    Memory: 19.51 GB / 31.25 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 18.12.0 - ~/.asdf/installs/nodejs/18.12.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/18.12.0/bin/yarn
    npm: 10.2.0 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 8.12.0 - ~/.asdf/installs/nodejs/18.12.0/bin/pnpm
    bun: 1.0.11 - ~/.bun/bin/bun
  Browsers:
    Chromium: 120.0.6099.71
Used Package Manager

pnpm

Validations
  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the generated auto-import.d.ts with rm auto-import.d.ts; npm run build, then compare the declaration output for foo.composable.ts with the expected suffix-preserving name. Trace the declaration generation through the mentioned unplugin/unimport toTypeDeclarationItems function and the playground, and run the existing tests to determine why the proposed change breaks them. Done means the generated declare global {} block retains the full filename suffix and the tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vite
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.