当前位置:网站首页>Viteconfigure project path alias

Viteconfigure project path alias

2022-06-26 17:03:00 Landejin

webpack Through resolve.alias Define project path alias , In this way, when importing files , No longer need to use relative paths , Unified root path (/src/) As a starting point .

vite It also supports the definition of path alias :

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

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': join(__dirname, "src"),
    }
  }
})

If the project is TypeScript To write , It needs to be revised TypeScript Configuration of :

{
   // ...
  "compilerOptions": {
    // ... Other configuration 
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  // ...
}

If you've just created TypeScript project , It's possible to meet Module not found “path” Or its corresponding type declaration Error prompt for , install @types/node that will do .

npm install @types/node --save-dev
原网站

版权声明
本文为[Landejin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202162240398557.html