文件中使用相对路径,相对不友好, 代码格式化提交的时候消息不明确
vite.config.js
import { defineConfig } from 'vite'
import aliasPath from './aliasPath'
export default defineConfig({
resolve: {
// alias: aliasPath.resolve.alias // Object
alias: aliasPath // Array
},
})
alias Arrray
解决 less @~ 路径报错
const path = require('path');
const resolve = (url) => path.resolve(__dirname, url);
module.exports = [
{ find: /^~/, replacement: '' },
{
find: '@',
replacement: resolve('src'),
},
{
find: '@components',
replacement: resolve('src/components'),
},
{
find: '@utils',
replacement: resolve('src/utils'),
},
{
find: '@layouts',
replacement: resolve('src/layouts'),
},
{
find: '@routes',
replacement: resolve('src/routes'),
},
{
find: '@constants',
replacement: resolve('src/constants'),
},
{
find: '@models',
replacement: resolve('src/models'),
},
{
find: '@locales',
replacement: resolve('src/locales'),
}
]
alias Object
const path = require('path');
const resolve = (url) => path.resolve(__dirname, url);
module.exports = {
context: resolve('./'),
resolve: {
'alias': {
// 如果报错__dirname找不到,需要安装 @types/node
// "@": path.resolve(__dirname, "src"),
'@': resolve('src'),
'@const': resolve('src/const'),
'@components': resolve('src/components'),
'@models': resolve('src/models'),
'@routes': resolve('src/routes'),
'@utils': resolve('src/utils'),
'@layouts': resolve('src/layouts'),
'@locales': resolve('src/locales'),
},
},
};
如果报错__dirname找不到,需要安装 @types/node
- ts开发场景下,会报这个错,本地识别 node环境
- 使用 import path from ‘path’ 进行引入
- 不要再使用 const path = require(‘path’)
yarn add @types/node --save-dev