One of the most important things that will make your life a lot simpler is to set up an import alias for your files.
My favorite import alias is set to the src folder because almost everything we need is in there. Then I can import components and layouts like this.
---import Component from '@/components/basename.astro'import Layout from '@/layouts/basename.astro'---Setting up your import alias
You need to set up your import alias in two places:
- In
jsconfig.json(ortsconfig.jsonif you use TypeScript) - In
astro.config.mjs
The jsconfig.json makes IntelliSense work in your code editor so it is pretty important. It looks like this:
{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./src/*"] } }}Just having this in jsconfig.json is not enough because Vite (the build tool behind Astro) needs to know the alias as well. So you need to set up the alias in astro.config.mjs as well.
It looks like this:
import { URL, fileURLToPath } from 'node:url'
export default defineConfig({ vite: { resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)), }, }, },})That’s all you need to set up your import alias!
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson