aleclarson / vite-tsconfig-paths

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

Broken in Vite 5 #129

Closed tervay closed 5 months ago

tervay commented 7 months ago

https://stackblitz.com/edit/vitejs-vite-2pijh9?file=package.json

This stackblitz resembles a project structure I'm using that no longer works with Vite 5. In Vite 4, this project will (intentionally, in this Stackblitz case) fail due to a top-level await not being allowed in the configured environment (which is fine, expected, and desired in this case).

With Vite 5.0.2, this fails with the following stack trace:

❯ yarn build
yarn run v1.22.19
$ tsc && vite build
vite v5.0.2 building for production...
✓ 20 modules transformed.
[vite:worker] Cannot read properties of undefined (reading '/home/projects/vitejs-vite-2pijh9/src')
file: /home/projects/vitejs-vite-2pijh9/src/worker.ts?worker
error during build:
TypeError: Cannot read properties of undefined (reading '/home/projects/vitejs-vite-2pijh9/src')
    at Object.resolveId (file:///home/projects/vitejs-vite-2pijh9/node_modules/vite-tsconfig-paths/dist/index.mjs:175:45)
    at eval (file:///home/projects/vitejs-vite-2pijh9/node_modules/rollup/dist/es/shared/node-entry.js:18631:40)
    at async PluginDriver.hookFirstAndGetPlugin (file:///home/projects/vitejs-vite-2pijh9/node_modules/rollup/dist/es/shared/node-entry.js:18531:28)
    at async resolveId (file:///home/projects/vitejs-vite-2pijh9/node_modules/rollup/dist/es/shared/node-entry.js:17200:26)
    at async ModuleLoader.resolveId (file:///home/projects/vitejs-vite-2pijh9/node_modules/rollup/dist/es/shared/node-entry.js:17614:15)
    at async Object.resolveId (file:///home/projects/vitejs-vite-2pijh9/node_modules/vite/dist/node/chunks/dep-ErEj4WmL.js:8150:10)
    at async PluginDriver.hookFirstAndGetPlugin (file:///home/projects/vitejs-vite-2pijh9/node_modules/rollup/dist/es/shared/node-entry.js:18531:28)
    at async resolveId (file:///home/projects/vitejs-vite-2pijh9/node_modules/rollup/dist/es/shared/node-entry.js:17200:26)
    at async ModuleLoader.resolveId (file:///home/projects/vitejs-vite-2pijh9/node_modules/rollup/dist/es/shared/node-entry.js:17614:15)
    at async eval (file:///home/projects/vitejs-vite-2pijh9/node_modules/rollup/dist/es/shared/node-entry.js:17921:50)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I'm not familiar enough with Vite to further debug this.

wangpin34 commented 6 months ago

yep, I also encounted with this issue on vite with version v5.0.10.

divmgl commented 6 months ago

Unfortunately can confirm that it seems to be broken in Vite 5.

TypeScript error:

No overload matches this call.
  The last overload gave the following error.
    Type 'Plugin<any>' is not assignable to type 'PluginOption'.
dineug commented 6 months ago

vite-tsconfig-paths v3.6.0 downgrades will normal operation.

aleclarson commented 6 months ago

I could not reproduce with the given StackBlitz, @tervay. I also tried reproducing manually, but no luck. Please try clearing your node_modules, deleting your lockfile, and re-installing to see if that fixes it for you. Otherwise, please provide a proper reproduction, thanks.

tervay commented 6 months ago

@aleclarson The stackblitz will fail when running yarn build. I'm not sure how to make it run that by default instead of yarn run dev, but yarn build will fail.

davbrito commented 6 months ago

I'm having the same issue. It fails only when used as a worker plugin. Just like in the provided StackBlitz.

I could not reproduce with the given StackBlitz, @tervay. I also tried reproducing manually, but no luck. Please try clearing your node_modules, deleting your lockfile, and re-installing to see if that fixes it for you. Otherwise, please provide a proper reproduction, thanks.

The error only shows when you run the build command: image

davbrito commented 6 months ago

The error happens at this line: https://github.com/aleclarson/vite-tsconfig-paths/blob/dd3fc7e9ef353b0f53dc9720c00e722ad35f5104/src/index.ts#L164

My insight on this is that resolveId is being called before configResolved, or configResolved is not being called at all on the worker plugin. That way this line is not executed https://github.com/aleclarson/vite-tsconfig-paths/blob/dd3fc7e9ef353b0f53dc9720c00e722ad35f5104/src/index.ts#L125 and it ends up in the error since it's undefined.


I was able to workaround it by passing the same plugin object to both arrays, since it uses the same tsconfig for workers and main project. That way the resolversByDir gets prepopulated on the configResolved of the main plugin.

const tspaths = tsconfigPaths()

export default defineConfig({
  plugins: [tspaths],
  worker { plugins: () => [tspaths] },
})
aleclarson commented 5 months ago

This is a bug in Vite. I've opened this PR to fix it. Thanks for investigating, @davbrito. Neither of your suspicions were correct, but it helped save me some time. 👍

aleclarson commented 5 months ago

Closing this. The Vite PR has been merged. Not sure what the release schedule is these days. If you have any other problems with Vite 5 and this plugin, open a new issue.