解決vue3+vite配置unplugin-vue-component找不到Vant組件
使用 vue3 + vite + Vant 搭建移動(dòng)端項(xiàng)目
使用 vue3 + vite + Vant 搭建移動(dòng)端項(xiàng)目,為了避免全量引入 vant 導(dǎo)致打包體積過大,又不想一個(gè)一個(gè)組件手動(dòng)導(dǎo)入,所以就選擇了 vant 官方推薦的方法,使用 unplugin-vue-components 插件自動(dòng)引入組件,并按需引入組件的樣式。
但是運(yùn)行過程中遇到了報(bào)錯(cuò):
[vite] Internal server error: Failed to resolve import "vant/es" from "xxx"
vue3 + vite
項(xiàng)目依賴
- package.json
{
"name": "vue3-demo",
"private": true,
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "vue-tsc && vite build",
"preview": "vite preview",
"git": "tive git -c tive.git.config.cjs",
"lint": "eslint --ext .js,.jsx,.ts,.tsx --fix --quiet ./src",
"lint:stylelint": "stylelint --cache --fix \"src/**/*.{less,css,scss}\" --cache --cache-location node_modules/.cache/stylelint/",
"prepare": "husky install"
},
"dependencies": {
"amfe-flexible": "^2.2.1",
"axios": "^1.4.0",
"lib-flexible": "^0.3.2",
"pinia": "^2.0.35",
"vant": "^4.3.1",
"vue": "^3.2.47",
"vue-router": "4.0.1"
},
"devDependencies": {
"@types/node": "^20.1.0",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"@vitejs/plugin-vue": "^4.1.0",
"typescript": "^5.0.2",
"unplugin-vue-components": "^0.24.1",
"vite": "^4.3.2",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-eslint": "^1.8.1",
"vite-plugin-style-import": "^2.0.0",
"vue-eslint-parser": "^9.2.1",
"vue-tsc": "^1.4.2"
},
"lint-staged": {
"*.{js,jsx,tsx,ts}": [
"npm run lint",
"npm run lint:stylelint"
]
}
}完整報(bào)錯(cuò)
Plugin: vite-plugin-eslint
File: /Users/tiven/Desktop/dev/yc-chat-mbi/src/components/Footer.vue
2:47:05 PM [vite] Internal server error: Failed to resolve import "vant/es" from "src/components/Footer.vue". Does the file exist?
Plugin: vite:import-analysis
File: /Users/tiven/Desktop/dev/yc-chat-mbi/src/components/Footer.vue:1:89
1 | /* unplugin-vue-components disabled */import { Field as __unplugin_components_1 } from 'vant/es';import 'vant/es/field/style/index';
| ^
2 | import { Button as __unplugin_components_0 } from 'vant/es';import 'vant/es/button/style/index';
3 | import { defineComponent as _defineComponent } from "vue";- vite.config.ts 配置如下:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint'
import path from 'node:path'
import viteCompression from 'vite-plugin-compression'
import postCssPxToRem from 'postcss-pxtorem'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
// 在導(dǎo)入模塊時(shí),如果模塊路徑不包含文件擴(kuò)展名,則會(huì)嘗試添加下面這些擴(kuò)展名
extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
// 在導(dǎo)入模塊時(shí),如果模塊路徑以 / 開頭,則會(huì)嘗試在下面這些目錄中查找該模塊
alias: {
'@': path.resolve(__dirname, './src'),
'@img': path.resolve(__dirname, './src/assets/img'),
},
},
build: {
sourcemap: false,
minify: 'terser',
assetsInlineLimit: 4096,
reportCompressedSize: false,
rollupOptions: {
output: {
// 最小化拆分包
manualChunks(id) {
if (id.includes('node_modules')) {
return id.toString().split('node_modules/')[1].split('/')[0].toString()
}
},
chunkFileNames: 'assets/js/[name].[hash].js', // 用于命名代碼拆分時(shí)創(chuàng)建的共享塊的輸出命名,[name]表示文件名,[hash]表示該文件內(nèi)容hash值
},
// external: ['antd'],
},
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
},
plugins: [
vue(),
Components({
resolvers: [VantResolver()],
}),
eslintPlugin({
include: ['src/**/*.ts', 'src/**/*.vue', 'src/*.ts', 'src/*.vue'],
}),
viteCompression({
threshold: 1024 * 4,
}),
],
css: {
postcss: {
plugins: [
postCssPxToRem({
rootValue: 37.5, //1rem的大小
propList: ['*'], //需要轉(zhuǎn)換的屬性
selectorBlackList: ['.norem'], //過濾掉不需要轉(zhuǎn)換的類名
}),
],
},
},
})解決辦法
修改 vite.config.ts 下的 resolve.extensions 參數(shù)配置,加入 .mjs 拓展名即可解決。
export default defineConfig({
resolve: {
// 在導(dǎo)入模塊時(shí),如果模塊路徑不包含文件擴(kuò)展名,則會(huì)嘗試添加下面這些擴(kuò)展名
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
// 在導(dǎo)入模塊時(shí),如果模塊路徑以 / 開頭,則會(huì)嘗試在下面這些目錄中查找該模塊
alias: {
'@': path.resolve(__dirname, './src'),
'@img': path.resolve(__dirname, './src/assets/img'),
},
},
})以上就是解決vue3+vite配置unplugin-vue-component找不到Vant組件的詳細(xì)內(nèi)容,更多關(guān)于vue3 vite配置unplugin的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Vue3+Vite項(xiàng)目中引入pinia和pinia-plugin-persistedstate的方法代碼
- Vite使用unplugin-auto-import實(shí)現(xiàn)vue3中的自動(dòng)導(dǎo)入
- 3分鐘搞定vite項(xiàng)目(vue/react)使用vite-plugin-pwa配置為pwa應(yīng)用
- vue3項(xiàng)目導(dǎo)入異常Error:@vitejs/PLUGIN-vue?requires?vue?(>=3.2.13)解決辦法
- vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法
- Vite處理html模板插件之vite-plugin-html插件使用
- vite打包優(yōu)化vite-plugin-compression的使用示例詳解
- vue3+vite多項(xiàng)目多模塊打包(基于vite-plugin-html插件)
- 在?Vite項(xiàng)目中使用插件?@rollup/plugin-inject?注入全局?jQuery的過程詳解
- vue3+vite引入插件unplugin-auto-import的方法
- Vite Plugin 開發(fā)完全指南
相關(guān)文章
axios對(duì)請(qǐng)求各種異常情況處理的封裝方法
今天小編就為大家分享一篇axios對(duì)請(qǐng)求各種異常情況處理的封裝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue使用html2canvas實(shí)現(xiàn)截取圖片并保存
html2canvas是一個(gè)JavaScript庫,它可以將HTML元素轉(zhuǎn)換為Canvas元素本文將介紹一下Vue如何使用html2canvas實(shí)現(xiàn)截取圖片并保存功能,需要的可以參考下2023-12-12
vue實(shí)現(xiàn)下拉框篩選表格數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)下拉框篩選表格數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
vue使用$attrs和$listeners多級(jí)組件嵌套傳遞數(shù)據(jù)
這篇文章主要為大家介紹了vue使用$attrs和$listeners多級(jí)組件嵌套傳遞數(shù)據(jù)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Vue3實(shí)現(xiàn)Message消息組件示例
在大多數(shù) web 產(chǎn)品中,全局的 Message 組件占有較大的使用場(chǎng)景,本文主要介紹了Vue3實(shí)現(xiàn)Message消息組件示例,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
vue 解決uglifyjs-webpack-plugin打包出現(xiàn)報(bào)錯(cuò)的問題
這篇文章主要介紹了vue 解決uglifyjs-webpack-plugin打包出現(xiàn)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié)
這篇文章主要介紹了vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04

