vite.config.js配置入門詳解
1 如何創(chuàng)建vite項(xiàng)目?
step 1 : ?npm init vite@latest ?yarn create vite step2 : npm init vite@latest my-vue-app --template vue ? npm 7+, 需要額外的雙橫線: npm init vite@latest my-vue-app -- --template vue ? # yarn yarn create vite my-vue-app --template vue
2 如何讓vite項(xiàng)目啟動(dòng)時(shí)自動(dòng)打開瀏覽器?
注:vite針對(duì)開發(fā)環(huán)境,打包環(huán)境和預(yù)覽環(huán)境分別定義了三個(gè)選項(xiàng): server、build、preview。 開發(fā)環(huán)境server類似于webpack中的devServer。
export default ({mode})=>{
return defineConfig({
? server:{
? ? open:true, //vite項(xiàng)目啟動(dòng)時(shí)自動(dòng)打開瀏覽器
? },
}
}3vite啟動(dòng)默認(rèn)端口為3000?如何更改默認(rèn)端口?
export default ({mode})=>{
return defineConfig({
? server:{
? ? port:8080, //vite項(xiàng)目啟動(dòng)時(shí)自定義端口
? },
}
}4 vite如何設(shè)置熱更新?
vite默認(rèn)開發(fā)環(huán)境關(guān)閉了熱更新。代碼更改需要手動(dòng)更新,設(shè)置更改代碼自動(dòng)刷新頁面需要設(shè)置hmr:true
export default ({mode})=>{
return defineConfig({
? server:{
? ? hmr:true, //開啟熱更新
? },
}
}5vite中如何配置別名路徑?
設(shè)置resolver選項(xiàng)
import { resolve } from 'path';
?
export default ({mode})=>{
return defineConfig({
? resolve:{
? ? ? alias:{
? ? ? ? "@":resolve(__dirname,"src"),
? ? ? ? "@c":resolve(__dirname,"src/components"),
? ? ? }
? },
}
}6 vite中如何設(shè)置便捷圖片路徑引用?
比如圖片資源都在src/assets/image目錄下,不想在項(xiàng)目中每次都通過require("../assets/image/1.jpg")這樣寫一長串去引用。能否通過 類似nuxt中的快速引用?
<img src="/images/1.png" alt="" /> 這里直接引用
export default ({mode})=>{
return defineConfig({
? resolve:{
? ? ? alias:{
? ? ? "/images":"src/assets/images/"
? ? ? //這里不能通過path模塊解析路徑的寫法
? ? ? }
? },
}
}7如何把vite打包以后的js,css和img資源分別分門別類在js/css/img文件夾中?
//由于是處理打包以后的資源,所以需要配置build選項(xiàng)
export default ({mode})=>{
return defineConfig({
? ?build:{
? ? assetsDir:"static",
? ? rollupOptions:{
? ? ??
? ? ? input:{
? ? ? ? index:resolve(__dirname,"index.html"),
? ? ? ? project:resolve(__dirname,"project.html")
? ? ? },
? ? ? output:{
? ? ? ? chunkFileNames:'static/js/[name]-[hash].js',
? ? ? ? entryFileNames:"static/js/[name]-[hash].js",
? ? ? ? assetFileNames:"static/[ext]/name-[hash].[ext]"
? ? ? }
? ? },
? },
?
}
}8 如何通過vite給項(xiàng)目配置多個(gè)環(huán)境?
以開發(fā)、測試和生產(chǎn)環(huán)境為例
(1)在項(xiàng)目根目錄下分別新建.env.development,.env.test,.env.production文件
//.env.devopment文件內(nèi)容 NODE_ENV="development" VITE_APP_BASEAPI="https://www.dev.com" //.env.test文件內(nèi)容 NODE_ENV="test" VITE_APP_BASEAPI="https://www.test.com" //.env.production文件內(nèi)容 NODE_ENV="production" VITE_APP_BASEAPI="https://www.production.com"
(2) package.json文件做如下修改
?"scripts": {
? ? "dev": "vite --mode development",
? ? "build": "vite build --mode production",
? ? "test": "vite build --mode test",
? ? "preview": "vite preview"
? },(3)項(xiàng)目中通過Import.meta.env.VITE_APP_BASEAPI來獲取對(duì)應(yīng)環(huán)境的值
<template>
? <div>
? ? <Item></Item>
? </div>
?
</template>
<script setup>
? ? import { defineComponent, onMounted, ref } from 'vue'
? ? import Item from "@c/item.vue"
? ? console.log("env", import.meta.env.VITE_APP_BASEAPI)
? ? console.log("可選鏈", obj?.gender || "male")
})
</script>9 vite中如何配置多入口,進(jìn)行多頁面開發(fā)?
step1:在根目錄新建一個(gè)入口頁面以project.html為例,同時(shí)在根目錄下新建一個(gè)project文件夾,在此文件夾新建一個(gè)main.js,App.vue

step2:vite.config.js作如下修改:
import { defineConfig,loadEnv ?} from 'vite'
import {resolve} from "path";
export default ({mode})=>{
return defineConfig({
? build:{
? ? rollupOptions:{
? ? ? input:{
? ? ? ? index:resolve(__dirname,"index.html"),
? ? ? ? project:resolve(__dirname,"project.html")
? ? ? },
? ? ?//output:{
? ? ? ?// chunkFileNames:'static/js/[name]-[hash].js',
? ? ? ? //entryFileNames:"static/js/[name]-[hash].js",
? ? ? ? //assetFileNames:"static/[ext]/name-[hash].[ext]"
? ? ? }
? ? },
? },
? plugins: [
? ? vue(),
? ]
})
}?step3:vite run dev 啟動(dòng)以后在url加上project.html查看project項(xiàng)目 localhost:3000/project.html
10 如何設(shè)置開啟生產(chǎn)打包分析文件大小功能?類似webpack-bundle-analyzer?
//1 安裝rollup-plugin-visualizer 插件
npm i rollup-plugin-visualizer
//2 vite.config.js中引入插件
import {visualizer} from "rollup-plugin-visualizer"
export default ({mode:string})=>{
?
? const plugins=[?
? ? vue(),
? ? AutoImport({
? ? ? resolvers: [ElementPlusResolver()],
? ? }),
? ? Components({
? ? ? resolvers: [ElementPlusResolver()]
? ? }),
? ? visualizer({
? ? ? ? open:true, ?//注意這里要設(shè)置為true,否則無效
? ? ? ? gzipSize:true,
? ? ? ? brotliSize:true
? ? ?})
? ];
?}
?return ?defineConfig({
? ? ? ? ? ??
? ? ? ? ? ? resolve:{
? ? ? ? ? ? ? alias:{
? ? ? ? ? ? ? ? "@":resolve(__dirname,"src"),
? ? ? ? ? ? ? ? "/images":"src/assets/images/"
? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? plugins
? ? ? ? ? })11 如何解決require is not define報(bào)錯(cuò)的的問題? 場景: 比如我們assets文件夾下有一個(gè)靜態(tài)的json:
? ? ? ? list:[
? ? ? ? ? ? {
? ? ? ? ? ? ? ? shop_id:1,
? ? ? ? ? ? ? ? shop_name:'搜獵人藝術(shù)生活',
? ? ? ? ? ? ? ? products:[
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:101,
? ? ? ? ? ? ? ? ? ? ? ? text:'洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶',
? ? ? ? ? ? ? ? ? ? ? ? price:480,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:require("./images/1.png"),
? ? ? ? ? ? ? ? ? ? ? ? sum:480,
? ? ? ? ? ? ? ? ? ? ? ? checked:false//商品選中狀態(tài)
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:102,
? ? ? ? ? ? ? ? ? ? ? ? text:'花露水花露水花露水花露水花露水花露水花露水花露水',
? ? ? ? ? ? ? ? ? ? ? ? price:680,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:require('./images/2.png'),
? ? ? ? ? ? ? ? ? ? ? ? sum:680,
? ? ? ? ? ? ? ? ? ? ? ? checked:false
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:103,
? ? ? ? ? ? ? ? ? ? ? ? text:'燕麥片燕麥片燕麥片燕麥片燕麥片燕麥片燕麥片燕麥片',
? ? ? ? ? ? ? ? ? ? ? ? price:380,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:require('./images/3.png'),
? ? ? ? ? ? ? ? ? ? ? ? sum:380,
? ? ? ? ? ? ? ? ? ? ? ? checked:false
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? check:false,//店鋪選中狀態(tài)
? ? ? ? ? ? ? ? choose:0,//商品選中個(gè)數(shù)
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? ? shop_id:2,
? ? ? ? ? ? ? ? shop_name:'卷卷旗艦店',
? ? ? ? ? ? ? ? products:[
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:201,
? ? ? ? ? ? ? ? ? ? ? ? text:'剃須刀剃須刀剃須刀剃須刀剃須刀剃須刀剃須刀剃須刀',
? ? ? ? ? ? ? ? ? ? ? ? price:580,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:require('./images/4.png'),
? ? ? ? ? ? ? ? ? ? ? ? sum:580,
? ? ? ? ? ? ? ? ? ? ? ? checked:false
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:202,
? ? ? ? ? ? ? ? ? ? ? ? text:'衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙',
? ? ? ? ? ? ? ? ? ? ? ? price:780,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:require('./images/5.png'),
? ? ? ? ? ? ? ? ? ? ? ? sum:780,
? ? ? ? ? ? ? ? ? ? ? ? checked:false
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? check:false,
? ? ? ? ? ? ? ? choose:0,
? ? ? ? ? ? },
? ? ? ? ? ?
? ? ? ? ],
? ? status:false,//全選選中狀態(tài)
? ? allchoose:0,//店鋪選中個(gè)數(shù)
? ? allsum:0,//總計(jì)價(jià)格
? ? allnum:0,//總計(jì)數(shù)量
}
export default fetchData此時(shí)運(yùn)行你回發(fā)現(xiàn)報(bào)錯(cuò):require is not define? 解決辦法:
const fetchData={
? ? ? ? list:[
? ? ? ? ? ? {
? ? ? ? ? ? ? ? shop_id:1,
? ? ? ? ? ? ? ? shop_name:'搜獵人藝術(shù)生活',
? ? ? ? ? ? ? ? products:[
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:101,
? ? ? ? ? ? ? ? ? ? ? ? text:'洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶',
? ? ? ? ? ? ? ? ? ? ? ? price:480,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:new URL("./images/1.png",import.meta.url).href,
? ? ? ? ? ? ? ? ? ? ? ? sum:480,
? ? ? ? ? ? ? ? ? ? ? ? checked:false//商品選中狀態(tài)
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:102,
? ? ? ? ? ? ? ? ? ? ? ? text:'花露水花露水花露水花露水花露水花露水花露水花露水',
? ? ? ? ? ? ? ? ? ? ? ? price:680,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:new URL('./images/2.png',import.meta.url).href,
? ? ? ? ? ? ? ? ? ? ? ? sum:680,
? ? ? ? ? ? ? ? ? ? ? ? checked:false
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:103,
? ? ? ? ? ? ? ? ? ? ? ? text:'燕麥片燕麥片燕麥片燕麥片燕麥片燕麥片燕麥片燕麥片',
? ? ? ? ? ? ? ? ? ? ? ? price:380,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:new URL('./images/3.png',import.meta.url).href,
? ? ? ? ? ? ? ? ? ? ? ? sum:380,
? ? ? ? ? ? ? ? ? ? ? ? checked:false
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? check:false,//店鋪選中狀態(tài)
? ? ? ? ? ? ? ? choose:0,//商品選中個(gè)數(shù)
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? ? shop_id:2,
? ? ? ? ? ? ? ? shop_name:'卷卷旗艦店',
? ? ? ? ? ? ? ? products:[
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:201,
? ? ? ? ? ? ? ? ? ? ? ? text:'剃須刀剃須刀剃須刀剃須刀剃須刀剃須刀剃須刀剃須刀',
? ? ? ? ? ? ? ? ? ? ? ? price:580,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:new URL('./images/4.png',import.meta.url).href,
? ? ? ? ? ? ? ? ? ? ? ? sum:580,
? ? ? ? ? ? ? ? ? ? ? ? checked:false
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro_id:202,
? ? ? ? ? ? ? ? ? ? ? ? text:'衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙衛(wèi)生紙',
? ? ? ? ? ? ? ? ? ? ? ? price:780,
? ? ? ? ? ? ? ? ? ? ? ? num:1,
? ? ? ? ? ? ? ? ? ? ? ? img:new URL('./images/5.png',import.meta.url).href,
? ? ? ? ? ? ? ? ? ? ? ? sum:780,
? ? ? ? ? ? ? ? ? ? ? ? checked:false
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? check:false,
? ? ? ? ? ? ? ? choose:0,
? ? ? ? ? ? },
? ? ? ? ? ??
? ? ? ? ],
? ? status:false,//全選選中狀態(tài)
? ? allchoose:0,//店鋪選中個(gè)數(shù)
? ? allsum:0,//總計(jì)價(jià)格
? ? allnum:0,//總計(jì)數(shù)量
}
export default fetchData注意引用方式的變化:require------->new URL('./images/5.png',import.meta.url).href
到此這篇關(guān)于vite.config.js配置入門詳解的文章就介紹到這了,更多相關(guān)vite.config.js配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.js 帶下拉選項(xiàng)的輸入框(Textbox with Dropdown)組件
這篇文章主要介紹了Vue.js 帶下拉選項(xiàng)的輸入框(Textbox with Dropdown)組件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
vue elementUI 表單嵌套驗(yàn)證的實(shí)例代碼
這篇文章主要介紹了vue + elementUI 表單嵌套驗(yàn)證,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
vue無法加載文件C:\xx\AppData\Roaming\npm\vue.ps1系統(tǒng)禁止運(yùn)行腳本
這篇文章主要介紹了vue?:?無法加載文件?C:\xx\AppData\Roaming\npm\vue.ps1...系統(tǒng)上禁止運(yùn)行腳本問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Vue3封裝ant-design-vue表格的詳細(xì)代碼
這篇文章主要介紹了Vue3封裝ant-design-vue表格,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
Vuerouter的beforeEach與afterEach鉤子函數(shù)的區(qū)別
本文詳細(xì)的介紹了Vuerouter的beforeEach與afterEach鉤子函數(shù)的區(qū)別和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12

