-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Support tsconfig.json & jsconfig.json aliases #1747
Support tsconfig.json & jsconfig.json aliases #1747
Conversation
|
@jonathantneal Just took a look at WMR and they have a really nice plugin for this! Much more straightforward. Thoughts on adopting this plugin? https://github.com/preactjs/wmr/blob/main/packages/wmr/src/plugins/tsconfig-paths-plugin.js |
@natemoo-re, well, not particularly, but only because WMR moves the resolution complexity to I ran into this when I was first researching a solution. :( |
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I had a few small nits about comment clarity, but nothing blocking. Merge when ready.
aliases.push({ | ||
find: /^(?!\.*\/)(.+)$/, | ||
replacement: `${baseUrl | ||
.split('') | ||
.map((segment) => (segment === '$' ? '$$' : segment)) | ||
.join('')}/$1`, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to look this up because I wasn't sure it was correct, but it is!
I would possibly add some more context to the comment above, and possibly include this link: https://www.typescriptlang.org/tsconfig#baseUrl
The baseUrl
option changes the way non-relative specifiers are resolved, so if baseUrl
exists, all non-relative specifiers will be resolved relative to the baseUrl
.
|
||
// compile any alias expressions and push them to the list | ||
for (let [alias, values] of Object.entries(Object(compilerOptions.paths) as { [key: string]: string[] })) { | ||
values = [].concat(values as never); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this is needed, can you elaborate?
/** Resolved source id from existing resolvers. */ | ||
const resolvedId = await this.resolve(source, importer, { skipSelf: true, ...options }); | ||
|
||
// if the existing resolvers find the file, return that resolution |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This gives priority to other resolvers (like the built-in Node resolution), correct? Might clarify in the comment.
* Resolve paths from tsconfig or jsconfig https://code.visualstudio.com/docs/languages/jsconfig https://nextjs.org/docs/advanced-features/module-path-aliases * edit: rename plugin to `@astrojs/vite-plugin-tsconfig-alias` Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * edit: switch from `ps` to `path.posix` * edit: move sanitization of paths to loop * edit: rename `resolveConfigPaths` to `configAliasVitePlugin` * edit: update implementation based on feedback * prettier * edit: rename `matchTailingAsterisk` to `matchTrailingAsterisk` * edit: cleanup with comments * edit: spellcheck `condition` to `conditionally` * edit: refactor based on feedback * edit: Update README.md * edit: cleanup baseUrl transformation and add explainer comments * edit: cleanup resolutions and add commenting * yarn lint Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Changes
This PR adds support for path aliasing from
tsconfig.json
orjsconfig.json
, matching the behavior of the Astro VSCode extension and NextJS.Resolves #1749
This work can supersede #1749, as this provides a stronger developer experience, and creates less confusion between the existing support in
*config.json
files or thruvite.resolve.alias
inastro.config.js
.Testing
This was tested manually.
Docs
Documentation in #1751.