Table of Contents
Introduction
The Problem
Prerequisite
Setting up the Vite React project for absolute imports
Creating a Vite React...
For further actions, you may consider blocking this person and/or reporting abuse
Thanks for the article. I used it but it took me a while to figure out why TypeScript would not recognize Vite aliases. That is, Vite would compile but TypeScript would give an error, or the other way around: TypeScript gives no error but Vite will not compile. Eventually I found out that in addition to
"baseUrl": "./src",
in tsconfig I also had to give paths for my aliases.That is, here is the combination that worked for me:
tsconfig.json:
vite.config.ts:
import path:
import Text from '@components/text/text';
Without "paths" in the options above the import had not worked.
I'm glad you found it helpful. It's nice you were able to fix the typescript aliases recognition issue you encountered. Do you mind if I reference your comment in the original article so it's easy to find for others with a similar problem?
Thanks, Andrew, great that you're maintaining the article, go ahead and use my comment. Perhaps there might be other ways to fix this problem; if so I am interested to find out.
installing the plugin vite-tsconfig-paths and adding the below in tsconfig.json
and vite.config.js with
Hey, thanks for the article, I just wanted to ask, is there a way to convert
src
alias into empty string, so instead ofsrc/components/Button
we would importcomponents/Button
? thnxIf you want to import directly from the
components
folder, all you need to do is add a new property namedcomponents
to the alias object. Take a look at the example code provided below:Alternatively, you can opt for a different approach by creating an array that contains all your file names as shown below.
Using the array reduce method, you can conveniently generate an object with the respective file paths, as demonstrated below:
Once you have the
filePaths
object, you can easily include it using the JavaScript spread operator within the alias argument. Here is a code snippet:You can use this technique to effortlessly create a new absolute import path by simply adding the desired file name to the
fileNames
array.I believe that the above code in the
.reduce()
should havecur
and notcurr
, and it should pass an empty string as the initial value for the reduce:also, you can programmatically get the list of
fileNames
with:Thanks for the correction and feedback. I learnt something new!
@gregfenton I just added a new section to the article where I aim to spotlight useful comments. Do you mind if I add your comment to it?
You are free to use what I provided however you would care to. Thanks for asking.
Awesome Tip Mate 🍻
Thank you. I’m glad you found it helpful
Good job, That was helpful.
It worked like a charm. The only thing that did not work for me was VSCode IntelliSence suggesting the absolute routes :(
Consider adding this line in
settings.json
It worked for me after hours of searching. Source: stackoverflow, question - 47330773
How did you configure eslint for this?
Is there a way to convert all existing relative imports in a huge codebase to the format you've shared in the blog? Maybe through a script or something similar?