Skip to content

Instantly share code, notes, and snippets.

@M-Gonzalo
Created May 31, 2022 04:03
Show Gist options
  • Save M-Gonzalo/214b826d3b0465f0f9c582895bc96517 to your computer and use it in GitHub Desktop.
Save M-Gonzalo/214b826d3b0465f0f9c582895bc96517 to your computer and use it in GitHub Desktop.
How to configure absolute imports on a react app created using Vite
// #######################################################################
// En jsconfig.json:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@/*": ["*"]
}
}
}
// #######################################################################
// #######################################################################
// En consola:
npm i @vitejs/plugin-react-refresh
// #######################################################################
// #######################################################################
// En vite.config.js:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
const path = require('path')
import reactRefresh from '@vitejs/plugin-react-refresh'
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: [{ find: '@', replacement: path.resolve(__dirname, '/src') }],
},
plugins: [
react(),
reactRefresh()
]
})
// #######################################################################
// #######################################################################
// Para hacer los imports de los componentes desde cualquier carpeta:
import TestComponent from '@/components/test_folder/TestComponent.jsx'
// #######################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment