Level Up Your Imports: Using Path Aliases for Cleaner React + TypeScript Projects ๐Ÿš€

in HiveDevs โ€ข 2 months ago

Hello Hive Community Members! ๐Ÿ‘‹

Hope you're all doing fantastic! I'm back with Day 5 of my ReactJS learning series ๐Ÿš€
This journey has been quite the rollercoaster, and today we're diving into something every dev struggles with โ€” those ugly relative imports ๐Ÿ˜ฉ

But before that, if you missed my earlier episodes, here's your cheat sheet ๐Ÿ‘‡


๐Ÿ“š Catch Up on My React Adventures


๐Ÿš€ Why Path Aliases?

alias based import reference image

Letโ€™s face it โ€”

import Button from "../../../../components/ui/Button"

is just plain ugly and hard to maintain ๐Ÿ˜ตโ€๐Ÿ’ซ

While pairing up with another dev (shoutout to AI-generated code โœจ), I noticed a cleaner import style โ€” so I had to figure it out myself! Hereโ€™s how you can too ๐Ÿ‘‡


๐Ÿช„ Step-by-Step: Setting Up Path Aliases

๐Ÿง  Step 1: Update tsconfig.app.json

Add these compiler options to set your base path and alias:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
    // ...other stuff
  }
}

โš™๏ธ Step 2: Tweak vite.config.ts

Use path module to create an alias for @ pointing to your src folder.

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import path from "path";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(), 
    tailwindcss()
  ],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
  },
});

๐ŸŽ‰ Boom! Youโ€™re 90% done.


๐Ÿ” Step 3: Restart Everything!

Restart Typescript server

  • Stop your npm run dev server.
  • Restart TypeScript in VS Code (click on the TS version and hit "Restart TS Server").
  • Before starting again... do not skip step 4 ๐Ÿคฃ

โœ‚๏ธ Step 4: Update Your Imports!

Time to ditch the dot-dot-dot ๐Ÿ”ช

example updated import

Use beautiful imports like:

import Button from "@/components/ui/Button"

Isnโ€™t that satisfying? ๐Ÿ˜


๐Ÿ“š Sources That Helped (But Also Confused Me ๐Ÿ˜‚)

Honestly... I followed these and ended up with TypeScript errors. So, the method described in this post is what actually worked for me! ๐Ÿ™Œ


Thanks for following along!
See you in the next post where Iโ€™ll probably break something else and then fix it (again) ๐Ÿ˜…

Stay curious & keep coding ๐Ÿ’ป๐Ÿ’ช



๐Ÿ“ Final Note


๐Ÿš€ My Contributions to โ™ฆ๏ธ Hive Ecosystem

ContributionToHiveEcosystem
Hive Witness NodeHive API Node (in progress)3Speak Video Encoder Node Operator (highest number of nodes)3Speak Mobile App Developer
3Speak Podcast App Developer3Speak Shorts App Developer3Speak Support & Maintenance TeamDistriator Developer
CheckinWithXYZHive InboxHiFindHive Donate App
Contributed to HiveAuth Mobile AppEcency โ†” 3Speak IntegrationEcency โ†” InLeo IntegrationEcency โ†” Actifit Integration
Hive Stats AppVote for Witness AppHiveFlutterKitNew 3Speak App

๐Ÿ™Œ Support Back

โค๏ธ Appreciate my work? Consider supporting @threespeak & @sagarkothari88! โค๏ธ


Sort: ย 

This post has been manually curated by @bhattg from Indiaunited community. Join us on our Discord Server.

Do you know that you can earn a passive income by delegating to @indiaunited. We share more than 100 % of the curation rewards with the delegators in the form of IUC tokens. HP delegators and IUC token holders also get upto 20% additional vote weight.

Here are some handy links for delegations: 100HP, 250HP, 500HP, 1000HP.

image.png

100% of the rewards from this comment goes to the curator for their manual curation efforts. Please encourage the curator @bhattg by upvoting this comment and support the community by voting the posts made by @indiaunited.

Thank you so much @indiaunited & @bhattg for curation.

via Inbox

Stop downvoting my original content with your alt account @letusbuyhive

You have been exposed ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

Path aliases are a total game-changer! Those ../../../../ imports were a nightmare. When I set up my React-Vite project, the first thing I configure is the @ alias.

Thank you @devmamun

I feel happy that it helped you

via Inbox