項(xiàng)目代碼同步至碼云 weiz-vue3-template
關(guān)于vite的詳細(xì)配置可查看 vite官方文檔,本文簡單介紹vite的常用配置。
初始內(nèi)容
項(xiàng)目初建后,vite.config.ts
的默認(rèn)內(nèi)容如下:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
})
配置別名
1. 安裝 @types/node
npm i @types/node -D
2. 修改 vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
// 路徑查找
const pathResolve = (dir: string): string => {
return resolve(__dirname, ".", dir);
};
// 設(shè)置別名,還可以添加其他路徑
const alias: Record<string, string> = {
"@": pathResolve("src"),
"@views": pathResolve("src/views"),
"@store": pathResolve("src/store/modules")
};
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias
},
})
3. 使用
比如,修改 App.vue
:
import HelloWorld from '@/components/HelloWorld.vue'
配置環(huán)境變量
1. 新建env文件
根目錄下新建 .env
、.env.development
、.env.production
三個(gè)文件。.env
文件內(nèi)新增內(nèi)容:
# 本地運(yùn)行端口號(hào)
VITE_PORT = 8686
.env.development
文件內(nèi)新增內(nèi)容:
# 本地環(huán)境
VITE_USER_NODE_ENV = development
# 公共基礎(chǔ)路徑
VITE_PUBLIC_PATH = /
.env.production
文件內(nèi)新增內(nèi)容:
# 線上環(huán)境
VITE_USER_NODE_ENV = production
# 公共基礎(chǔ)路徑
VITE_PUBLIC_PATH = /
2. 環(huán)境變量統(tǒng)一處理
根目錄下新建 build
文件夾,其目錄下新建 index.ts
,內(nèi)容如下:
// 環(huán)境變量處理方法
export function wrapperEnv(envConf: Recordable): ViteEnv {
const ret: any = {};
for (const envName of Object.keys(envConf)) {
let realName = envConf[envName].replace(/\\n/g, "\n");
realName = realName === "true" ? true : realName === "false" ? false : realName;
if (envName === "VITE_PORT") realName = Number(realName);
ret[envName] = realName;
if (typeof realName === "string") {
process.env[envName] = realName;
} else if (typeof realName === "object") {
process.env[envName] = JSON.stringify(realName);
}
}
return ret;
}
3. 環(huán)境類型定義
在 types\index.d.ts
文件里新增對(duì) Recordable
和 ViteEnv
的類型定義:
type Recordable<T = any> = Record<string, T>;
interface ViteEnv {
VITE_USER_NODE_ENV: "development" | "production";
VITE_PUBLIC_PATH: string;
VITE_PORT: number;
}
修改 tsconfig.json
文件,將 build
文件夾內(nèi)的文件包含進(jìn)去:
"include": [ // 需要檢測的文件
"src/**/*.ts",
"build/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.vue",
"mock/*.ts",
"types/*.d.ts",
"vite.config.ts"
],
同理,修改 tsconfig.node.json
文件:
"include": [
"build/*.ts",
"types/*.d.ts",
"vite.config.ts"
]
4. 使用
修改 vite.config.ts
:文章來源:http://www.zghlxwxcb.cn/news/detail-746131.html
import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite"
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { wrapperEnv } from './build'
// 路徑查找
const pathResolve = (dir: string): string => {
return resolve(__dirname, ".", dir);
};
// 設(shè)置別名,還可以添加其他路徑
const alias: Record<string, string> = {
"@": pathResolve("src"),
"@views": pathResolve("src/views"),
"@store": pathResolve("src/store")
};
// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
const root = process.cwd()
const env = loadEnv(mode, root)
const viteEnv = wrapperEnv(env)
return {
base: viteEnv.VITE_PUBLIC_PATH,
plugins: [vue()],
resolve: {
alias
},
server: {
host: "0.0.0.0",
port: viteEnv.VITE_PORT,
https: false,
open: true,
// 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
proxy: {
"^/api": {
target: "http://192.168.1.4:8688",
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, "")
}
}
},
}
})
目錄更新
當(dāng)前項(xiàng)目目錄如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-746131.html
| .env
| .env.development
| .env.production
| .gitignore
| index.html
| package-lock.json
| package.json
| README.md
| tree.txt
| tsconfig.json
| tsconfig.node.json
| vite.config.ts
|
+---.vscode
| extensions.json
|
+---build
| index.ts
|
+---node_modules
+---public
| vite.svg
|
+---src
| | App.vue
| | main.ts
| | style.css
| | vite-env.d.ts
| |
| +---assets
| | vue.svg
| |
| \---components
| HelloWorld.vue
|
\---types
index.d.ts
到了這里,關(guān)于Vite4+Typescript+Vue3+Pinia 從零搭建(3) - vite配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!