Skip to content

Commit

Permalink
notes: typescript path aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie committed Apr 9, 2024
1 parent 4226f17 commit dfd30a8
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions content/notes/typescript path aliases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: TypeScript import path aliases
date: 2024-04-09
tags:
- tauri
- typescript
---
I've been working on some more frontend code recently, and got quite tired of my import paths looking like this

```
../../foo/bar/etc
```

How horrible! If I ever change directory structure, things are liable to break. That won't do!

The internet suggested adding the following to my `tsconfig.json`

```json
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
},

```

This resolved the squiggly lines in my editor, but my build was still failing. For some context - I'm using Tauri and Vite here.

It turns out, the issue was due to Vite. In order to support these aliases, we also need to add some config to `vite.config.ts`

First, install a plugin

```
npm install --save-dev vite-tsconfig-paths
```

Then, add it to your config

```typescript
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig(async () => ({
plugins: [tsconfigPaths()], // I imagine you have other plugins

// you probably have a bunch more here too
});
```
All done! My import paths are nice now

0 comments on commit dfd30a8

Please sign in to comment.