vite基本常見的配置講解
前言
最近在搗鼓一下vite,因為自己一直在使用react,就選擇vite、react來體驗一下vite。
使用最簡單的方法創(chuàng)建一個應(yīng)用:yarn create vite,然后選擇react框架。
vite默認配置是使用了defineConfig工具函數(shù):
import { defineConfig } from 'vite'
export default defineConfig({
// ...
})
不管是js還是ts,都可以直接使用defineConfig工具函數(shù),如果需要基于dev、serve或者build命令來選擇不同選項,那就選擇導(dǎo)出一個函數(shù),比如:
export default defineConfig(({ command, mode, ssrBuild }) => {
if (command === 'serve') {
return {
// dev 獨有配置
}
} else {
// command === 'build'
return {
// build 獨有配置
}
}
})
共享選項
root
這是項目根目錄【index.html的位置】,可以根據(jù)自己項目的規(guī)范來配置。比如:
const root: string = process.cwd();
export default defineConfig(({ command, mode, ssrBuild }) => {
return {
root,
plugins: [react()]
};
});
base
開發(fā)或者生產(chǎn)環(huán)境服務(wù)的公共基礎(chǔ)路徑:

mode
mode就是指明模式,比如:development或者production,如果在vite.config.ts中配置的話,那么就會把serve和build模式下覆蓋掉
plugin
應(yīng)用需用用到的插件,是一個數(shù)組,因為應(yīng)用中可能使用到很多插件。vite+react中插件就是react,比如:
import react from "@vitejs/plugin-react";
const root: string = process.cwd();
export default defineConfig(({ command, mode, ssrBuild }) => {
const { VITE_PUBLIC_PATH, VITE_PORT } = loadEnv(mode, process.cwd(), "");
return {
base: VITE_PUBLIC_PATH,
root,
plugins: [react()]
};
});
resolve.alias
設(shè)置別名,比如:完整配置
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
const root: string = process.cwd();
// 查找路徑
const pathResolve = (dir: string): string => {
return resolve(__dirname, ".", dir);
};
// 別名
const alias: Record<string, string> = {
"@": pathResolve("src"),
"@build": pathResolve("build"),
};
// https://vitejs.dev/config/
export default defineConfig(({ command, mode, ssrBuild }) => {
const { VITE_PUBLIC_PATH, VITE_PORT } = loadEnv(mode, process.cwd(), "");
return {
base: VITE_PUBLIC_PATH,
root,
plugins: [react()],
resolve: {
alias,
},
};
});
server
開發(fā)服務(wù)器配置選項。
- host,指定開發(fā)服務(wù)器監(jiān)聽的某個IP地址,如果是設(shè)置為
0.0.0.0或者true,那就是默認監(jiān)聽所有的地址。 - port,開發(fā)服務(wù)器端口號
- strictPort,設(shè)置為true的時候,遇到端口號被占用了,就會直接退出,
- https,是否開啟HTTPS
- open,自動在瀏覽器中開啟應(yīng)用程序
- proxy,請求路徑的代理,比如
export default defineConfig({
server: {
proxy: {
// 字符串簡寫寫法:http://localhost:5173/foo -> http://localhost:4567/foo
'/foo': 'http://localhost:4567',
// 帶選項寫法:http://localhost:5173/api/bar -> http://jsonplaceholder.typicode.com/bar
'/api': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
// 正則表達式寫法:http://localhost:5173/fallback/ -> http://jsonplaceholder.typicode.com/
'^/fallback/.*': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/fallback/, ''),
},
// 使用 proxy 實例
'/api': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
configure: (proxy, options) => {
// proxy 是 'http-proxy' 的實例
}
},
// 代理 websockets 或 socket.io 寫法:ws://localhost:5173/socket.io -> ws://localhost:5174/socket.io
'/socket.io': {
target: 'ws://localhost:5174',
ws: true,
},
},
},
})總結(jié)
到此這篇關(guān)于vite基本常見的配置講解的文章就介紹到這了,更多相關(guān)vite常見配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ant?菜單組件報錯Cannot?read?property?‘isRootMenu‘?of?undefin
這篇文章主要介紹了ant?菜單組件報錯Cannot?read?property?‘isRootMenu‘?of?undefined解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
vue實現(xiàn)用戶長時間不操作自動退出登錄功能的實現(xiàn)代碼
這篇文章主要介紹了vue實現(xiàn)用戶長時間不操作自動退出登錄功能的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
vue-cli創(chuàng)建的項目,配置多頁面的實現(xiàn)方法
下面小編就為大家分享一篇vue-cli創(chuàng)建的項目,配置多頁面的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue-router history模式服務(wù)器端配置過程記錄
vue路由有hash和history兩種模式,這篇文章主要給大家介紹了關(guān)于vue-router history模式服務(wù)器端配置的相關(guān)資料,需要的朋友可以參考下2021-06-06

