aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.34k stars 50 forks source link

v4 fails on resolving common libraries outside root #87

Open janakg opened 1 year ago

janakg commented 1 year ago

v3.6.0 works for a module outside the root, but v4 fails.

example:

tsconfigPathsPlugin({
  projects: [
    resolve('tsconfig.json'),
    resolve('../common-libs/color-lib/tsconfig.json'),
  ],
}) 
resolve: {
    alias: { '@sivi/color-lib': resolve(__dirname, '../common-libs/color-lib/src'),
}

tsconfig::

{
    "baseUrl": ".",
    "paths": {
      "*": ["./src/*"],
      "~/*": ["./*"],
      "@sivi/color-lib": ["../common-libs/color-lib/src/*"],
    }
}
aleclarson commented 1 year ago

Use DEBUG="vite-tsconfig-paths" vite dev and paste the logs here

janakg commented 1 year ago

Thanks! for quick response. 3.6.0 also has same logs but bundling works, in case of 4.0.3 bundling fails.

o/p of DEBUG="vite-tsconfig-paths" yarn vite dev

VITE v4.0.1  ready in 96 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
The following dependencies are imported but could not be resolved:

@sivi/color-lib (imported by /Users/janakg/cosmos/hub/templates/electron-app-main-template/src/renderer/app/index.tsx)
  resources/styles/globals.sass (imported by /Users/janakg/cosmos/hub/templates/electron-app-main-template/src/renderer/app/index.tsx)
  mathwasm (imported by /Users/janakg/cosmos/hub/templates/electron-app-main-template/src/renderer/app/index.tsx)
  @app/screens (imported by /Users/janakg/cosmos/hub/templates/electron-app-main-template/src/renderer/app/routes.tsx)
aleclarson commented 1 year ago

Seems like vite-tsconfig-paths isn't being used (or DEBUG isn't being applied correctly), as none of its logs are visible there. Could you try running DEBUG="vite-tsconfig-paths" node_modules/.bin/vite dev instead?

janakg commented 1 year ago

@aleclarson it is used for sure, else I am getting error

import tsconfigPathsPlugin from 'vite-tsconfig-paths'
const tsconfigPaths = tsconfigPathsPlugin({
  projects: [
    resolve('tsconfig.json'),
    resolve('../common-libs/color-lib/tsconfig.json'),
  ],
})
defineConfig({
    plugins: [tsconfigPaths]
}

Debug is not working for both commands I am using it inside electron-vite config , which inturn uses vite

I'll send you a test repo, if that helps

aleclarson commented 1 year ago

That would help

Ludea commented 1 year ago

get same issue: Running tsconfig-paths in DEBUG mode gives:

[4/4] Building fresh packages...
Done in 27.32s.
2022-12-16T06:31:21.127Z vite-tsconfig-paths options.root   == undefined
2022-12-16T06:31:21.128Z vite-tsconfig-paths project root   == /home/runner/work/tauri-mobile/tauri-mobile
2022-12-16T06:31:21.128Z vite-tsconfig-paths workspace root == /home/runner/work/tauri-mobile/tauri-mobile
2022-12-16T06:31:21.240Z vite-tsconfig-paths projects: [
  '/home/runner/work/tauri-mobile/tauri-mobile/tauri/examples/web/tsconfig.json',
  '/home/runner/work/tauri-mobile/tauri-mobile/tauri/tooling/api/tsconfig.json',
  '/home/runner/work/tauri-mobile/tauri-mobile/tauri/tooling/cli/templates/plugin/with-api/examples/svelte-app/tsconfig.json',
  '/home/runner/work/tauri-mobile/tauri-mobile/tauri/tooling/cli/templates/plugin/with-api/webview-src/tsconfig.json',
  '/home/runner/work/tauri-mobile/tauri-mobile/tsconfig.json'
]
error when starting dev server:
TSConfckParseError: failed to resolve "extends":"@tsconfig/svelte/tsconfig.json" in /home/runner/work/tauri-mobile/tauri-mobile/tauri/tooling/cli/templates/plugin/with-api/examples/svelte-app/tsconfig.json
    at resolveExtends (file:///home/runner/work/tauri-mobile/tauri-mobile/node_modules/tsconfck/dist/index.js:459:9)
    at parseExtends (file:///home/runner/work/tauri-mobile/tauri-mobile/node_modules/tsconfck/dist/index.js:432:34)
    at Module.parse (file:///home/runner/work/tauri-mobile/tauri-mobile/node_modules/tsconfck/dist/index.js:383:24)
    at async Promise.all (index 2)
    at async configResolved (file:///home/runner/work/tauri-mobile/tauri-mobile/node_modules/vite-tsconfig-paths/dist/index.mjs:94:9)
    at async Promise.all (index 1)
    at async resolveConfig (file:///home/runner/work/tauri-mobile/tauri-mobile/node_modules/vite/dist/node/chunks/dep-2285ba4f.js:62801:5)
    at async createServer (file:///home/runner/work/tauri-mobile/tauri-mobile/node_modules/vite/dist/node/chunks/dep-2285ba4f.js:61878:20)
    at async CAC.<anonymous> (file:///home/runner/work/tauri-mobile/tauri-mobile/node_modules/vite/dist/node/cli.js:729:24)
Error: Process completed with exit code 1.
aleclarson commented 1 year ago

@Ludea That seems to be a different issue (not a similar error message).

Ludea commented 1 year ago

Should I open a new issue to track this ?

aleclarson commented 1 year ago

@Ludea First, check that @tsconfig/svelte exists in node_modules. If it does, I'd recommend opening an in issue in tsconfck about it.

Ludea commented 1 year ago

The folder doesn't exists into node_modules

aleclarson commented 1 year ago

Then it's not an issue with this plugin or tsconfck.

janakg commented 1 year ago

@aleclarson sorry for the delay.

please find the rep : https://github.com/janakg/tsconfig-path-lib-path-issue

Just switch between version 3.6.0 and 4.0.3 of vite-tsconfig-paths , currently it works only in 3.6.0. Also, i don't know how to get the debug log working in this setup

As I have mentioned before this is via electron-vite (it is a thin wrapper library on vite for ease of use for electron apps)

aleclarson commented 1 year ago

Thanks for the reproduction. No estimates on when I'll be able to take a look though. Hopefully someone else will step in and investigate!

janakg commented 1 year ago

sure, thanks for the update. we will stick to 3.6.0 for now.

ylc395 commented 6 months ago

I believe that this bug is introduced by this commit:

https://github.com/aleclarson/vite-tsconfig-paths/commit/2361b2ec9f7241ec5f7be77d12092767e68e89e5

Since this commit, this plugin doesn't resolve any import path when the importer is not a descanter of the tsconfig file specified by project field.

How can we fix this? I can contribute a PR about this.

aleclarson commented 5 days ago

If anyone wants to investigate this, I've set up a test case. I couldn't reproduce the reported issue, so it may already be fixed. I suggest you try editing that test case to match your scenario as best as possible (while keeping it minimal). If you can reproduce a failure, open a PR with the changes you've made to the test case. Thanks!