Raw
1 export const metadata = {
2 title: `${pageNumber} Using TypeScript Aliases`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you'll learn how to use TypeScript aliases in your Medusa application.
8
9 ## Support for TypeScript Aliases
10
11 By default, Medusa doesn't support TypeScript aliases in production. That means you may get build errors in production if you use them in your development.
12
13 If you prefer using TypeScript aliases, this section will guide you through the steps to enable them in your Medusa application.
14
15 ### Step 1: Install Required Dependencies
16
17 Start by installing the following development dependencies:
18
19 ```bash npm2yarn
20 npm install --save-dev tsc-alias rimraf
21 ```
22
23 Where:
24
25 - `tsc-alias` resolves TypeScript aliases.
26 - `rimraf` removes files and directories.
27
28 ### Step 2: Update `package.json`
29
30 Then, add a new `resolve:aliases` script to your `package.json` and update the existing `build` script:
31
32 ```json title="package.json"
33 {
34 "scripts": {
35 // other scripts...
36 "resolve:aliases": "tsc --showConfig -p tsconfig.json > tsconfig.resolved.json && tsc-alias -p tsconfig.resolved.json && rimraf tsconfig.resolved.json",
37 "build": "medusa build && npm run resolve:aliases"
38 }
39 }
40 ```
41
42 ### Step 3: Update `tsconfig.json`
43
44 Next, configure the TypeScript aliases you want to use in your `tsconfig.json` file by adding a `paths` property under `compilerOptions`.
45
46 For example, to import anything under the `src` directory using type aliases, add the following in `tsconfig.json`:
47
48 ```json title="tsconfig.json"
49 {
50 "compilerOptions": {
51 // ...
52 "paths": {
53 "@/*": ["./src/*"]
54 }
55 }
56 }
57 ```
58
59 ### Step 4: Use TypeScript Aliases
60
61 Then, you can use the `@` alias in your application code.
62
63 For example, if you have a service in `src/modules/brand/service.ts`, you can import it like this:
64
65 ```ts
66 import { BrandModuleService } from "@/modules/brand/service"
67 ```
68
69 ---
70
71 ## Support TypeScript Aliases for Admin Customizations
72
73 Medusa also doesn't support TypeScript aliases in the admin customizations by default. However, you can also configure your Medusa application to use TypeScript aliases in your admin customizations.
74
75 ### Step 1: Update `src/admin/tsconfig.json`
76
77 Update `src/admin/tsconfig.json` to include `baseUrl` and `paths` configuration:
78
79 ```json title="src/admin/tsconfig.json"
80 {
81 "compilerOptions": {
82 "baseUrl": ".",
83 "paths": {
84 "@/*": ["./*"]
85 }
86
87 // other options...
88 }
89 }
90 ```
91
92 The `baseUrl` option sets the base directory to `src/admin`, and the `paths` option defines the `@` alias to allow importing files from the `src/admin` directory using aliases.
93
94 ### Step 2: Update `medusa-config.ts`
95
96 Next, update the `vite` configuration in `medusa-config.ts` to include the `resolve.alias` configuration:
97
98 ```ts title="medusa-config.ts"
99 import path from "path"
100
101 module.exports = defineConfig({
102 // ...
103 admin: {
104 vite: () => ({
105 resolve: {
106 alias: {
107 "@": path.resolve(__dirname, "./src/admin"),
108 },
109 },
110 }),
111 },
112 })
113 ```
114
115 <Note title="Tip">
116
117 Learn more about the `vite` configuration in the [Medusa configuration](../medusa-config/page.mdx) chapter.
118
119 </Note>
120
121 ### Step 3: Use TypeScript Aliases in Admin Customizations
122
123 You can now use the `@` alias in your admin customizations, just like you do in your main application code.
124
125 For example, if you have a component in `src/admin/components/Container.tsx`, you can import it in a widget like this:
126
127 ```ts
128 import Container from "@/components/Container"
129 ```
130
131 ### Match TSConfig and Vite Alias Configuration
132
133 Make sure that the `@` alias points to the same path as in your `src/admin/tsconfig.json`.
134
135 For example, if you set the `@/*` alias to point to `./components/*`:
136
137 ```json title="src/admin/tsconfig.json"
138 {
139 "compilerOptions": {
140 "baseUrl": ".",
141 "paths": {
142 "@/*": ["./components/*"]
143 }
144 }
145 }
146 ```
147
148 Then, the `vite` alias configuration would be:
149
150 ```ts title="medusa-config.ts"
151 import path from "path"
152
153 module.exports = defineConfig({
154 // ...
155 admin: {
156 vite: () => ({
157 resolve: {
158 alias: {
159 "@": path.resolve(__dirname, "./src/admin/components"),
160 },
161 },
162 }),
163 },
164 })
165 ```