How to configure import aliases in Vite, TypeScript and Jest

Most people have seen them, those immensely long import paths like the example below:

import module from “../../../../../modules/module.ts”;

To improve this, you can use import aliases and make it look like the example
below:

import module …


This content originally appeared on DEV Community and was authored by sand4rt

Most people have seen them, those immensely long import paths like the example below:

import module from "../../../../../modules/module.ts";

To improve this, you can use import aliases and make it look like the example
below:

import module from "@/modules/module.ts";

The benefit of this is readability and that you can move files and folders to sub or parent directories without changing the import paths.

Tools like Vue CLI are supporting this out-of-the-box, but if you want to use the new blazing-fast build tool Vite, you'll need to (at the time of writing)
configure it manually. I've included TypeScript and Jest because they are often used in combination.

For this to work, all tools need to know that import aliases are used by modifying each tool's configuration file.

Vite has a configuration file called vite.config.ts and by adding the resolve object Vite will know that import aliases are being used:

// vite.config.ts
import { defineConfig } from "vite";
import path from "path";

export default defineConfig({
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});

By adding a paths object to the compilerOptions inside the tsconfig.json like the example below TypeScript will also know that import aliases are being used:

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

At last Jest knows that aliasses are being used by adding the moduleNameMapper to the jest.config.ts configuration file like the code below:

// jest.config.ts
module.exports = {
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1",
  },
};


This content originally appeared on DEV Community and was authored by sand4rt


Print Share Comment Cite Upload Translate Updates
APA

sand4rt | Sciencx (2021-11-04T17:06:04+00:00) How to configure import aliases in Vite, TypeScript and Jest. Retrieved from https://www.scien.cx/2021/11/04/how-to-configure-import-aliases-in-vite-typescript-and-jest/

MLA
" » How to configure import aliases in Vite, TypeScript and Jest." sand4rt | Sciencx - Thursday November 4, 2021, https://www.scien.cx/2021/11/04/how-to-configure-import-aliases-in-vite-typescript-and-jest/
HARVARD
sand4rt | Sciencx Thursday November 4, 2021 » How to configure import aliases in Vite, TypeScript and Jest., viewed ,<https://www.scien.cx/2021/11/04/how-to-configure-import-aliases-in-vite-typescript-and-jest/>
VANCOUVER
sand4rt | Sciencx - » How to configure import aliases in Vite, TypeScript and Jest. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/04/how-to-configure-import-aliases-in-vite-typescript-and-jest/
CHICAGO
" » How to configure import aliases in Vite, TypeScript and Jest." sand4rt | Sciencx - Accessed . https://www.scien.cx/2021/11/04/how-to-configure-import-aliases-in-vite-typescript-and-jest/
IEEE
" » How to configure import aliases in Vite, TypeScript and Jest." sand4rt | Sciencx [Online]. Available: https://www.scien.cx/2021/11/04/how-to-configure-import-aliases-in-vite-typescript-and-jest/. [Accessed: ]
rf:citation
» How to configure import aliases in Vite, TypeScript and Jest | sand4rt | Sciencx | https://www.scien.cx/2021/11/04/how-to-configure-import-aliases-in-vite-typescript-and-jest/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.