Update 2023
Here is what worked for me (finally):
ViteConfig:
import { fileURLToPath, URL } from "url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: [
{ find: '@', replacement: fileURLToPath(new URL('./src', import.meta.url)) },
{ find: '@assets', replacement: fileURLToPath(new URL('./src/shared/assets', import.meta.url)) },
{ find: '@cmp', replacement: fileURLToPath(new URL('./src/shared/cmp', import.meta.url)) },
{ find: '@stores', replacement: fileURLToPath(new URL('./src/shared/stores', import.meta.url)) },
{ find: '@use', replacement: fileURLToPath(new URL('./src/shared/use', import.meta.url)) },
],
},
});
TSConfig:
{
...
"compilerOptions": {
...
"paths": {
"@/*": ["./src/*"],
"@assets/*": ["./src/shared/assets/*"],
"@cmp/*": ["./src/shared/cmp/*"],
"@stores/*": ["./src/shared/stores/*"],
"@use/*": ["./src/shared/use/*"]
}
}
}
Update 2022
This solution comes by default from when creating a new project with npm init vue@latest
import { fileURLToPath, URL } from "url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
});
In components use @/
:
<script setup>
import HelloWorld from "@/components/HelloWorld.vue";
</script>
This is what worked for me:
import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@/': `${path.resolve(__dirname, 'src')}/`
}
}
})
Then in components:
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<HelloWorld msg="Hello Vue 3 + Vite" />
</template>
<script>
import HelloWorld from '@/components/HelloWorld.vue'
export default {
name: 'App',
components: {
HelloWorld
}
}
</script>