Closed au-z closed 2 years ago
This is expected behavior. There are 3 solutions that don't require a new plugin option:
./modules
to ./src/modules
modules/**/*
to the include
arraymodules/tsconfig.json
and have it extend ./tsconfig.json
Do any of these work for you?
Interesting to note that TypeScript successfully resolves @moduleA
from moduleB.ts
until I rename tsconfig.json
to src/tsconfig.json
(and make necessary changes so the paths are correct again).
In other words, the paths
resolution is only applied to transient dependencies when the tsconfig.json
exists in a parent directory of the transient dependency.
Anyway, my initial conclusion is that there exists good enough workarounds that this isn't a priority. If someone wants to submit a PR, I'll try to review it at some point, but no guarantees.
Thanks for having a look quickly @aleclarson and chiming in. 👍
Because of some organizational constraints beyond this toy example, 1 and 3 are not possible. Luckily, in my case, 2 (appending to the include array) would technically work though it's not quite ideal as I'll have to maintain both the tsconfig paths and a similar include array.
Good to get an official answer on this and to have the example around for posterity. I appreciate it!
For anyone coming from Google, if you need to workaround this, you can duplicate your path aliases into Vite's config without using any plugins:
// vite.config.js
const config: UserConfigExport = {
// ...
resolve: {
alias: {
'@moduleA': path.resolve(__dirname, "modules/moduleA"),
'@moduleB': path.resolve(__dirname, "modules/moduleB"),
}
},
plugins: [
// ...
],
// ...
};
See more details here.
Obviously it's suboptimal to duplicate your path definitions, but if they don't change very often, it's good enough IMO.
Thanks for creating such a useful package @aleclarson! During development, I've noticed what I think is a small bug affecting module resolution.
I've prepared a minimal reproduction here: https://github.com/auzmartist/vite-tsconfig-paths-recursive-resolution
Some analysis of the vite debug output...
Problem
Using
vite-tsconfig-paths
, modules placed outside the includes array are normally resolved. However, this only extends to first order dependencies - dependencies referenced directly from project source.However, if these first order dependencies, depend on other pathed modules which are similarly outside the tsconfig include array, errors are produced during module resolution.
Example Annotated Output
DEBUG=vite-tsconfig-paths vite
Here we crawl for a tsconfig.json. Notably, two paths
@moduleA
and@moduleB
are configured which point to TS modules in ./modules.Here, we see that the tsconfig only includes TS modules within ./src.
Here we start up the dev server and begin resolving modules.
Here we resolve @moduleA.
Here we resolve @moduleB.
Here we see that we cannot resolve @moduleA depended on by @moduleB:
I am not sure if this is the intended behavior of the plugin but it certainly seems unexpected that the tsconfig paths would only apply to dependencies imported from modules within the
include
array. Perhaps this could be expanded to apply to all modules by default and the current "strict" implementation offered as a configuration option? What do you think?