Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): allow imports from local workspace tsconfig paths (#5370) #17286

Closed

Conversation

garydex
Copy link

@garydex garydex commented May 23, 2024

Description

Fixes #5370 nrwl/nx#17019 remix-run/remix#9171

Uses existing tsconfck dependency to loads the nearest tsconfig file if it exists, grabs any paths from compilerOptions and then makes sure these are not caught by the externalise-deps plugin when resolving imports in the config file.

This functionality is isolated to loadConfigFromFile so does not need to replicate functionality from tsconfig-paths or similar, nor does it duplicate any existing plugins like vite-tsconfig-paths or @esbuild-plugins/tsconfig-paths.

Copy link

stackblitz bot commented May 23, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@garydex
Copy link
Author

garydex commented May 23, 2024

I'm not entirely sure why the windows-latest build is failing; doesn't seem to be related to my changes.

Edit to add: It's running out of memory in the unit tests. I've run the tests on a Windows 10 machine and Node 20, and all tests pass.

@garydex garydex force-pushed the vite-config-resolve-tsconfig-paths branch 2 times, most recently from 41431a7 to 8c947cb Compare May 24, 2024 15:33
@garydex
Copy link
Author

garydex commented May 24, 2024

That's better - all tests passing now 🥳

It's worth pointing out that I have created an Nx workspace with my patched version of Vite, to demonstrate how a shared config file can be imported and extended - https://github.com/garydex/nx-vite-tsconfig-paths

@bluwy
Copy link
Member

bluwy commented Jun 6, 2024

Vite doesn't respect the tsconfig paths today, hence I don't think we should fix the problem this way. Similarly in #6828 where we had discussed and decided that we don't want to have builtin support for it. Tsconfig paths describes the (Vite) runtime and doesn't define it. Sorry for not reviewing this earlier before.

@garydex
Copy link
Author

garydex commented Jun 6, 2024

I appreciate the response. A few points:

Vite doesn't respect the tsconfig paths today, hence I don't think we should fix the problem this way. Similarly in #6828 where we had discussed and decided that we don't want to have builtin support for it.

The issue you mentioned is about resolving tsconfig paths across the entirety of Vite. This isn't trying to do that, or replicate/replace vite-tsconfig-paths or similar plugins. It's specific to config, and only executes if there's a tsconfig file that has paths in the codebase.

The other advantage of using the tsconfig paths in this context is that it returns earlier in the build.onResolve - before making any of the resolveByViteResolver calls unnecessarily.

I actually think we may need both this and #17323 - while that PR may fix imports with npm/yarn workspaces, it still doesn't work in a repo that uses tsconfig paths, such as an Nx workspace.

@bluwy
Copy link
Member

bluwy commented Jun 6, 2024

The issue you mentioned is about resolving tsconfig paths across the entirety of Vite. This isn't trying to do that, or replicate/replace vite-tsconfig-paths or similar plugins. It's specific to config, and only executes if there's a tsconfig file that has paths in the codebase.

The config loading is part of the "experience" of using Vite, so that issue is relevant here too. We can't have tsconfig paths sometimes work here but not there when they work in a Vite project.

@garydex
Copy link
Author

garydex commented Jun 6, 2024

Fair point, though I do think it's an equally jarring experience for users who can easily get tsconfig paths to work everywhere in their code except the config file. I understand it's via plugins and out of the scope of Vite itself - but ideally one would be able to at least opt-in for something like this.

resolvedPath: string,
): Promise<string[]> {
const { tsconfig } = await parse(resolvedPath)
const paths = tsconfig?.compilerOptions?.paths || {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using the ?? operator instead of ||?
I think ?? would be more appropriate in this context.

Comment on lines +1133 to +1135
if (!tsConfigPaths.length) {
return false
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Array.prototype.some always returns false when called on an empty array, the tsConfigPaths.length check is unnecessary.

@bluwy
Copy link
Member

bluwy commented Jun 17, 2024

Related #16718

Closing this as it's not something we want to support in the config.

@bluwy bluwy closed this Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vite.config.ts can't import untranspiled ts files from other packages in the same monorepo
3 participants