vuejs / language-tools

⚡ High-performance Vue language tooling based-on Volar.js
https://marketplace.visualstudio.com/items?itemName=Vue.volar
MIT License
5.78k stars 390 forks source link

Cannot find module '@uni' or its corresponding type declarations. #4399

Closed giapdong closed 1 month ago

giapdong commented 4 months ago

I am building multiple apps with shared resources and here is my folder structure image

⛳ In this way, I want to use uni for both app-a and app-b.

Here is sample for app-a when using it below app-a/tsconfig.json

{
    "extends": "@vue/tsconfig/tsconfig.dom.json",
    "include": ["../uni/**/*"],
    "exclude": ["../uni/**/__tests__/*"],
    "compilerOptions": {
        "moduleResolution": "bundler",
        "baseUrl": ".",
        "paths": {
            "@uni": ["../uni/uni"],
        }
    }
}

app-a/vite.config.ts

import path from 'node:path';
import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
    ],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url)),
            '@uni': fileURLToPath(new URL('../uni/uni', import.meta.url)),
        },
    },
})

app-a/package.json

{
    "scripts": {
        "dev": "vite",
        "build": "run-p type-check \"build-only {@}\" --",
        "preview": "vite preview",
        "build-only": "vite build",
        "type-check": "vue-tsc --build --force"
    },
}

app-a/src/App.vue

<template>
    <div></div>
</template>

<script lang="ts" setup>
import uni from '@uni'
</script>

With this way, I see minor error in app-a/src/App.vue: image

It works well for both the dev and build-only. It only a typescript check, do not affect or crash app

But when i using command 'type-check' it make really error as this issue title image

So, how i can resolve it? I really want to using type-check for safe check but with this way i must to ignore to finish build process :(( btw, if this issue done, it will affect to Volar? I don't want to see error during development time :( Thank you

RayGuo-ergou commented 4 months ago

You should add the alias to the tsconfig that includes your app.

giapdong commented 4 months ago

You should add the alias to the tsconfig that includes your app.

@RayGuo-ergou What do you mean? Because I included in tsconfig/include

RayGuo-ergou commented 4 months ago

You only include ../uni/**/*, so typescript server dose not know that app-a and app-b can also use @uni alias.

giapdong commented 4 months ago

You only include ../uni/**/*, so typescript server dose not know that app-a and app-b can also use @uni alias.

I edited my issue to be clearer. My problem appears at app-a (for example, app-b also). And I registered @uni for both tsconfig and vite.config.ts (you can re-see in my issue description).

I know that in that way it was tell to typescript that using @uni for app-a. So what is the wrong here?

RayGuo-ergou commented 4 months ago

create tsconfig.json under app-a does not automatically include files in app-a you have to explicitly add to the tsconfig.

e.g.

-   "include": ["../uni/**/*"],
+   "include": ["../uni/**/*", "./src/**/*"],

Both IDE error and typecheck error due to this. Your build works because vite does not require typescript at all which mean it does not read tsconfig.json.

giapdong commented 4 months ago

create tsconfig.json under app-a does not automatically include files in app-a you have to explicitly add to the tsconfig.

e.g.

-     "include": ["../uni/**/*"],
+     "include": ["../uni/**/*", "./src/**/*"],

Both IDE error and typecheck error due to this. Your build works because vite does not require typescript at all which mean it does not read tsconfig.json.

Ah sure :D, I cutting include "./src/*/" for reduce noise and only focus on uni module. Because I still using "dev" and "build-only" without error so it must include :D

davidmatter commented 1 month ago

Hey there, as there have been many fixes in the last 2.x versions: Please let us know if you're still encountering this issue. Otherwise kindly close this one, thanks!