1、vite + vue3 + ts 如何使用别名
只要修改以下两个文件 tsconfig.json + vite.config.js 就可以了!
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": [
"src/*"
],
"@/components/*": [
"src/components/*"
],
"/@style/*": [
"src/style/*"
],
"/@views/*": [
"src/views/*"
],
"/@lib/*": [
"src/lib/*"
]
}
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}
import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import pxtorem from "postcss-pxtorem"
import path from "path";
const { resolve } = require("path");
export default defineConfig({
base: "./", // 开发或生产环境服务的 公共基础路径
resolve: {
alias: {
"/@": resolve(__dirname, "./src"),
"/@style": resolve(__dirname, "./src/style"),
"/@views": resolve(__dirname, "./src/views"),
"/@components": resolve(__dirname, "./src/components"),
"/@lib": resolve(__dirname, "./src/libs"),
},
},
css: {
//css预处理
preprocessorOptions: {
scss: {
charset: false,
//引入var.scss全局预定义变量
additionalData:
'@import "./src/style/mixins.scss";@import "./src/style/variable.scss";',
},
},
},
plugins: [
vue(),
pxtorem({
rootValue: 37.5,
unitPrecision: 4, // 保留小数位
propList: ["*"],
// selectorBlackList: [''], //过滤的类名
replace: true, // 默认直接替换属性
mediaQuery: false,
minPixelValue: 6, // 所有小于设置的样式都不被转换
}),
],
server: {
host: "0.0.0.0",
fs: {
// Allow serving files from one level up to the project root
allow: [".."],
},
port: 3000,
},
})