Subwaytime / vite-aliases

Alias auto generation for Vite
https://www.npmjs.com/package/vite-aliases
MIT License
210 stars 12 forks source link

Incorrect tsconfig when two folders in path have same name #34

Closed ivarlovlie closed 2 years ago

ivarlovlie commented 2 years ago

Say I have a vite project inside of ./src/app22/vite.config.js and all of my code for app22 is inside of ./src/app22/src/. The generated aliases for vite works as expected, but when writing aliases to tsconfig an alias for $pages/index for example would look like ./src/app22/src/pages/* instead of what would be expected (?) ./src/pages/*. If I rename the first src to notsrc for example this is the behaviour I get.

I have attached a repro, just install deps and build with vite.

repro.zip

Subwaytime commented 2 years ago

Hey there!

Checked the repro, getting the same issue here! Could you post a log of the generated vite-aliases here? That would be great!

The issue might be attached to the basepath, as its technically taking the directory where the config is located and then adds the relative path to the related folder.

ivarlovlie commented 2 years ago

setup:

ViteAliases({
    prefix: "$",
    useTypescript: true,
    useConfig: true,
    createLog: true
}),

logs:

[
    {
        "find": "$components",
        "replacement": "/home/ivar/s/repos/time-tracker/src/webapp/src/components"
    },
    {
        "find": "$lib",
        "replacement": "/home/ivar/s/repos/time-tracker/src/webapp/src/lib"
    },
    {
        "find": "$logs",
        "replacement": "/home/ivar/s/repos/time-tracker/src/webapp/src/logs"
    },
    {
        "find": "$pages",
        "replacement": "/home/ivar/s/repos/time-tracker/src/webapp/src/pages"
    },
    {
        "find": "$styles",
        "replacement": "/home/ivar/s/repos/time-tracker/src/webapp/src/styles"
    },
    {
        "find": "$",
        "replacement": "/home/ivar/s/repos/time-tracker/src/webapp/src"
    }
]

paths in tsconfig.json:

"paths": {
    "$components/*": [
        "./src/webapp/src/components/*"
    ],
    "$lib/*": [
        "./src/webapp/src/lib/*"
    ],
    "$pages/*": [
        "./src/webapp/src/pages/*"
    ],
    "$styles/*": [
        "./src/webapp/src/styles/*"
    ],
    "$/*": [
        "./src/webapp/src/*"
    ],
    "$logs/*": [
        "./src/webapp/src/logs/*"
    ]
}
Subwaytime commented 2 years ago

Hm, try setting the default directory for vite-aliases, this might solve the issue actually.

ViteAliases({
    dir: `src/webapp`
})

If that doesnt work, i know where the code issue actually is. As its checking for the path based on the dir, and the default dir is set to src which in your case is doubled, and it cant handle that case.

Subwaytime commented 2 years ago

@ivarlovlie Did this work for you?

Subwaytime commented 2 years ago

closed due to inactivity

tommy-mitchell commented 1 year ago

Had this same issue, solved it by setting dir to ./src:

ViteAliases({
  dir: "./src"
})