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

Can't get the plugin to work with Nuxt and Vitest #123

Open
datsenkoboos opened this issue Jul 30, 2023 · 8 comments
Open

Can't get the plugin to work with Nuxt and Vitest #123

datsenkoboos opened this issue Jul 30, 2023 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@datsenkoboos
Copy link

Hi, i'm trying to use Vitest and Nuxt 3 with its "explicit" imports from '#imports' which is declared in tsconfig.json:
tsconfig.json in the root of the project

{
  // https://nuxt.com/docs/guide/concepts/typescript
  "extends": "./.nuxt/tsconfig.json"
}

"./.nuxt/tsconfig.json"

{
  "compilerOptions": {
    ...
    "paths": {
      ...
      "#imports": [
        "C:/path/to/project/nuxt-build/.nuxt/imports"
      ...
    }
   ...
  },

Vitest can't resolve the import even with vite-tsconfig-paths:

изображение

vite.config.ts

import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
  plugins: [tsconfigPaths()],
})

Am i doing anything wrong?

@aleclarson
Copy link
Owner

I'm not familiar with Nuxt. Why do they use #imports specifically? I suppose this plugin is tripping on the # character somewhere, but haven't tested it myself. PR welcome

@aleclarson aleclarson added the help wanted Extra attention is needed label Sep 9, 2023
@akasection
Copy link

akasection commented Oct 10, 2023

Hello!
I also encountered this issue and seems to find the root cause (though I'm unsure how to fix that)

A bit more explanation:

So basically Nuxt dev used custom paths like ~/, #imports etc. to allow importing some nuxt framework functions into Vue files.
To make the intellisense working with this behaviour, the Nuxt adds the resolve alias and generate custom tsconfig with relative paths under <rootDir>/.nuxt/tsconfig.json

So in most common Nuxt project, we will have two tsconfig:

  1. <rootDir>/tsconfig.json (which extends below)
  2. <rootDir/.nuxt/tsconfig.json

Later in nuxt update, every path aliases on the .nuxt tsconfig now has relative paths with added ../ (up a folder), which previously it was absolute paths. Though in actual development, there is no behaviour change at all. Most Vue files can import things from #import just fine.

Now the problem is on Vitest, using vite-tsconfig-paths, somehow, the plugin will resolve the tsconfig alias #import into.. let's say, ../node_modules/nuxt/dist/app , but it doesn't account that the tsconfig position was on .nuxt, but rather resolved relatively to rootDir instead.

So, absolutely, vite-tsconfig-paths will resolve any path aliases of .nuxt/tsconfig.json into like <rootDir>/../<things> which is parent folder of the project itself.

Overall, it's not just the #imports alias that broken, but all aliases (including the ~ alias, @ and any relative aliases. The absolute pathes are unaffected.

in summary, currently any tsconfig that has relative paths AND placed inside subfolders of root project will misbehave:

// This is broken
"paths": {
      "~": [
        ".."
      ],
      "~/*": [
        "../*"
      ],
     "assets": [
        "../assets"
      ],
      "public": [
        "../public"
      ],
      "public/*": [
        "../public/*"
      ],
}

@JasonLandbridge
Copy link

@datsenkoboos or @akasection, did you guys ever managed to find a solution for this?

@JasonLandbridge
Copy link

This might be related to this issue here: nuxt/nuxt#26512

It seems nuxt changed the way their tsconfig.json are generated which might lead to non-working extends

@akasection
Copy link

@JasonLandbridge not a solution though; just a workaround.

For nuxt >= 3.8, adding baseUrl temporarily fixes the problem, though it may leads undesirable effects later on (im not sure too)

{
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./.nuxt/",
  }

@LiNCH35
Copy link

LiNCH35 commented Apr 5, 2024

For me it also helped to downgrade the vite version to 4.3.9

@matthew-dean
Copy link

@akasection

For nuxt >= 3.8, adding baseUrl temporarily fixes the problem, though it may leads undesirable effects later on (im not sure too)

This definitely did not work and broke the most things.

@LiNCH35

For me it also helped to downgrade the vite version to 4.3.9

This had no effect. I think ultimately this plugin doesn't actually support tsconfig paths.

@danielroe
Copy link

Here's an example of how to do the resolution:

https://github.com/nuxt/module-builder/blob/main/src/commands/build.ts#L279-L295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants